输入:
a = [[['a','b'],3],[['b','a'],9],[['b','z'],4]]
所需的输出(重复运算,不考虑顺序并添加整数)
[[['a','b'],12],[['b','z'],4]]
这当然不起作用:
a2 = [sorted(list) for list in [i[0] for i in a]]
print(a2)
[['a', 'b'], ['a', 'b'], ['b', 'z']]
当然可以将其删除:
a3= []
for i in a2:
if i not in a3:
a3.append(i)
print(a3)
[['a', 'b'], ['b', 'z']]
但是,我当然会丢失要删除的子列表的数量。
有什么建议吗?
谢谢。
参考方案
我想这就是你想要的:
a = [[['a','b'],3],[['b','a'],9],[['b','z'],4]]
# we use a dict to do the job.
# we sort the list of char and change to tuple. So i can be use as key.
dct = {}