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()参数1必须是字符串或只读字符缓冲区,而不是列表
我想要的是,例如,如果string =" a",则返回" 20"。
我无法将INPUT和OUTPUT转换为字符串,因为它们的大小不同。
什么是最有效的替代方案或调整?
答案 0 :(得分:0)
使用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