列表python2.7的maketrans替代方案

2024-05-16 10:45:54 发布

您现在位置:Python中文网/ 问答频道 /正文

INPUT = ["a","b","c","d","e","f","g","h","i","j","k"]
OUTPUT = ["20","21","22","23","24","25","26","27","28","29","30"]
TABLE = maketrans(INPUT, OUTPUT)
content = content.translate(TABLE)

TypeError: maketrans() argument 1 must be string or read-only character buffer, not list

我想要的是,例如,如果string=“a”,返回值为“20”。在

我不能将输入和输出转换成字符串,因为它们的大小不同。在

什么是最有效的选择或调整?在


Tags: oronlyreadinputoutputstringtablebe
1条回答
网友
1楼 · 发布于 2024-05-16 10:45:54

使用izip_longest(Python 2.x)或zip_longest(Python 3.x):

from itertools import izip_longest

INPUT = ["a","b","c","d","e","f","g","h","i","j","k"]
OUTPUT = ["20","21","22","23","24","25","26","27","28","29","30"]

string = 'a'   # string here

for i, o in izip_longest(INPUT, OUTPUT):
    if i == string:
        print(o)
# 20

相关问题 更多 >