在Python中计数并追加子列表元素
我想要计算一个子列表中独特元素的数量,然后把每个独特的元素写入一个新列表,同时把这个子列表的数量附加上去。list_1中的每个子列表只有两个元素,顺序不重要。
所以:
list_1 = [[a, b], [a, c], [a, c], [a, c], [b, e], [d, q], [d, q]]
变成:
new_list = [[a, b, 1], [a, c, 3], [b, e, 1], [d, q, 2]]
我在想我可能需要用到集合,但如果有人能给我指个方向,我会很感激。
3 个回答
0
这是一种有点绕的解决方案。
list_1 = [['a', 'b'], ['a', 'c'], ['a', 'c'], ['a', 'c'], ['b', 'e'], ['d', 'q'], ['d', 'q']]
new_dict={}
new_list=[]
for l in list_1:
if tuple(l) in new_dict:
new_dict[tuple(l)] += 1
else:
new_dict[tuple(l)] = 1
for key in new_dict:
entry = list(key)
entry.append(new_dict[key])
new_list.append(entry)
print new_list
6
你可以看看 collections.Counter()
;Counter
对象是多重集合(也叫做袋),它们把键和它们的计数对应起来。
不过,你需要把你的子列表转换成元组,才能作为键使用:
from collections import Counter
counts = Counter(tuple(e) for e in list_1)
new_list = [list(e) + [count] for e, count in counts.most_common()]
这样你就得到了一个按计数(从高到低)排序的 new_list
:
>>> from collections import Counter
>>> list_1 = [['a', 'b'], ['a', 'c'], ['a', 'c'], ['a', 'c'], ['b', 'e'], ['d', 'q'], ['d', 'q']]
>>> counts = Counter(tuple(e) for e in list_1)
>>> [list(e) + [count] for e, count in counts.most_common()]
[['a', 'c', 3], ['d', 'q', 2], ['a', 'b', 1], ['b', 'e', 1]]
如果你的出现次数总是连续的,那么你也可以使用 itertools.groupby()
:
from itertools import groupby
def counted_groups(it):
for entry, group in groupby(it, key=lambda x: x):
yield entry + [sum(1 for _ in group)]
new_list = [entry for entry in counted_groups(list_1)]
我在这里用了一个单独的生成器函数,不过你也可以把循环直接放到列表推导式里。
这样得到的结果是:
>>> from itertools import groupby
>>> def counted_groups(it):
... for entry, group in groupby(it, key=lambda x: x):
... yield entry + [sum(1 for _ in group)]
...
>>> [entry for entry in counted_groups(list_1)]
[['a', 'b', 1], ['a', 'c', 3], ['b', 'e', 1], ['d', 'q', 2]]
并且保持了原来的顺序。
1
如果相同的子列表是连续的:
from itertools import groupby
new_list = [sublist + [sum(1 for _ in g)] for sublist, g in groupby(list_1)]
# -> [['a', 'b', 1], ['a', 'c', 3], ['b', 'e', 1], ['d', 'q', 2]]