在列表中自动将“ A”替换为“ B”,“ B”替换为“ A”,“ C”替换为“ D”等?

时间:2020-04-05 14:20:21

标签: python

我想知道是否有任何方法可以自动将列表中的某些元素替换为其他元素,而无需为每个元素使用if..else语句? 像这样:

# before :
aL = ['a', 'b', 'c']
# after :
aL = ['b', 'c', 'd']

1 个答案:

答案 0 :(得分:1)

一种方法,

aL = ['a', 'b', 'c','z']
expected = []
for ch in aL:
    if ch == 'z':
        expected.append(chr(ord(ch)-25))
    else:
        expected.append(chr(ord(ch) + 1))
print(expected)

演示: https://rextester.com/LVY19713