我有这个清单
['a', 'c', 'g', 'w', 'Q', 'front', 'Z']
和列表清单:
[[0, 1, 1, 0,a], [1, 0, 2, 0,c], [1, 2, 0, 0,w], [0, 0, 0, 0,Q], [2, 0, 0, 15, 2, 'g'], [12, 0, 2, 3, 0, 2, 'front'], [0, 0, 0, 5, 2, 0, 'Z']]
我想根据我的列表用字母表交换项目?所以我的输出将是:
[[0, 1, 1, 0,a], [1, 0, 2, 0,c], [2, 0, 0, 15, 2, 'g'],[1, 2, 0, 0,w],[0, 0, 0, 0,Q], [12, 0, 2, 3, 0, 2, 'front'], [0, 0, 0, 5, 2, 0, 'Z']]
编辑:数字必须与字母一起移动。
答案 0 :(得分:0)
你可以试试这个:
a1 = ['a', 'c', 'g', 'w', 'Q', 'front', 'Z']
a2 = [[0, 1, 1, 0,'a'], [1, 0, 2, 0,'c'], [1, 2, 0, 0,'w'], [0, 0, 0, 0,'Q'], [2, 0, 0, 15, 2, 'g'], [12, 0, 2, 3, 0, 2, 'front'], [0, 0, 0, 5, 2, 0, 'Z']]
new_dict = {i[-1]:i for i in a2}
new_list = [new_dict[i] for i in a1]
输出:
[[0, 1, 1, 0, 'a'], [1, 0, 2, 0, 'c'], [2, 0, 0, 15, 2, 'g'], [1, 2, 0, 0, 'w'], [0, 0, 0, 0, 'Q'], [12, 0, 2, 3, 0, 2, 'front'], [0, 0, 0, 5, 2, 0, 'Z']]
答案 1 :(得分:0)
l = ['a', 'c', 'g', 'w', 'Q', 'front', 'Z']
ll = [[0, 1, 1, 0,'a'], [1, 0, 2, 0,'c'], [1, 2, 0, 0,'w'], [0, 0, 0, 0,'Q'], [2, 0, 0, 15, 2, 'g'], [12, 0, 2, 3, 0, 2, 'front'], [0, 0, 0, 5, 2, 0, 'Z']]
sorted(ll, key=lambda x: l.index(x[-1]))
输出:
[[0, 1, 1, 0, 'a'],
[1, 0, 2, 0, 'c'],
[2, 0, 0, 15, 2, 'g'],
[1, 2, 0, 0, 'w'],
[0, 0, 0, 0, 'Q'],
[12, 0, 2, 3, 0, 2, 'front'],
[0, 0, 0, 5, 2, 0, 'Z']]
答案 2 :(得分:0)
如果您愿意,可以试试这个:
list_a = ['a', 'c', 'g', 'w', 'Q', 'front', 'Z']
list_b = [[0, 1, 1, 0,'a'], [1, 0, 2, 0,'c'], [1, 2, 0, 0,'w'], [0, 0, 0, 0,'Q'], [2, 0, 0, 15, 2, 'g'], [12, 0, 2, 3, 0, 2, 'front'], [0, 0, 0, 5, 2, 0, 'Z']]
sorted_list=[]
for i in list_a:
for j in list_b:
if i==j[-1]:
sorted_list.append(j)
print(sorted_list)
在一线解决方案中:
print([j for i in list_a for j in list_b if i==j[-1]])