![](/img/trans.png)
[英]Python . How to sort elements of sub-list if second elements are equal sort the sub-lists by first element
[英]Python lists -- deduping and adding sub-list element
輸入:
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 = {}
for i in a:
srt = tuple(sorted(i[0]))
if srt in dct:
# the tuple of char is already existing as key, just sum int.
dct[srt]+= i[1]
else:
# if not we had a new key
dct[srt] = i[1]
# Convert the dict to list of list
new_list = [[list(k),v] for k,v in dct.items()]
print(new_list) # [[['a', 'b'], 12], [['b', 'z'], 4]]
您可以使用:
from collections import defaultdict
a = [[['a','b'],3],[['b','a'],9],[['b','z'],4]]
d = defaultdict(lambda : 0)
for e, n in a:
d[frozenset(e)] += n
a = [[list(k), v] for k, v in d.items()]
a
輸出:
[[['a', 'b'], 12], [['b', 'z'], 4]]
或者你可以使用:
from functools import reduce
from operator import add
from collections import Counter
f = ({frozenset(k): v} for k, v in a)
d = reduce(add, map(Counter, f)) # sum unique data
result = [[list(k), v] for k, v in d.items()]
result
輸出:
[[['a', 'b'], 12], [['b', 'z'], 4]]
如果您喜歡單線解決方案:
[[list(k), v] for k, v in reduce(add, map(Counter, ({frozenset(k): v} for k, v in a))).items()]
輸出:
[[['a', 'b'], 12], [['b', 'z'], 4]]
您可以先將子列表轉換為frozensets,以便您可以將它們視為鍵並使用collections.Counter
將整數添加為每個不同鍵的計數,然后在迭代結果項時將frozensets轉換回列表:
from collections import Counter
counter = Counter()
for t, c in a:
counter.update({frozenset(t): c})
print([[list(t), c] for t, c in counter.items()])
這將解決問題:
from itertools import groupby
a = [[['a','b'],3],[['b','a'],9],[['b','z'],4]]
# this will ensure a is ordered by the merging key:
a=sorted(a, key=lambda x: sorted(x[0]))
# this will group by merging key, extract the value with index 1, and sum it:
a=[[k, sum(map(lambda z: z[1], v))] for k,v in groupby(a, key=lambda x: sorted(x[0]))]
輸出:
[[['a', 'b'], 12], [['b', 'z'], 4]]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.