将整数列表转换为Python中预定义字符串的列表

mom*_*ind 0 python

[0,1,2,1]当我们将0转换为'abc',1转换为'f'和2转换为'z'时,如何将列表转换为字符串列表?所以输出列表将是['abc','f','z','f'].

我做了:

x = []
for i in xrange(input):
  if input[i] == ...
    x.append ('abc')
Run Code Online (Sandbox Code Playgroud)

Rag*_*elt 5

使用字典作为翻译表:

old_l = [0, 1, 2, 1]
trans = {0: 'abc', 1: 'f', 2: 'z'}
Run Code Online (Sandbox Code Playgroud)

然后,翻译您的列表:

new_l = [trans[v] for v in old_l]
Run Code Online (Sandbox Code Playgroud)