我需要一个代码来只颠倒字母顺序,而不颠倒数字。 对于前假设我有: 3 D F 4 B 5 H 6 K(以字符串或数组)我应该产生输出: 3 K H 4 B 5 F 6 D
答案 0 :(得分:1)
尝试
def reverse_besides_number(l):
# convert number in list to a dict
# record the index
d = {k: v for k, v in enumerate(l) if v.isdigit()}
# record the character in list
c = [i for i in l if not i.isdigit()]
c.reverse()
# insert
for key, value in d.items():
c.insert(key, value)
return c