尝试用所有列表替换我的文本

2024-05-16 20:29:23 发布

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

我有一个文本"abc",或者我用列表key2替换了名为al的列表中的字符。假设我不知道用什么列表替换了字符,我希望程序尝试用所有列表替换我的文本

al = ['a','b','c']

key1 = ['0','1','2']

key2 = ['2','3','4']

key3 = ['4','5','6']

lk = [key1,key2,key3]

text = '234'

for j in range(2):
    for i in range(2):
        text = text.replace(str(lk[i]),al[i])
    print(text)

'''
for i in range(2):
    text = text.replace(al[i],key1[i])
    print(text)
    
for i in range(2):
    text = text.replace(al[i],key2[i])
    print(text)
    
for i in range(2):
    text = text.replace(al[i],key3[i])
    print(text)

'''

Tags: textin文本列表forrange字符replace
1条回答
网友
1楼 · 发布于 2024-05-16 20:29:23

你可以使用python字典


  

decoder_dict ={1:'a',2:'b',3:'c'}

user_input = str(input("Enter your number: "))

output = []
for num in user_input.split():
  output.append(decoder_dict[int(num)])

print(''.join(output))

相关问题 更多 >