从Python中的列表中获取重复

2024-05-23 13:49:44 发布

您现在位置:Python中文网/ 问答频道 /正文

我有以下清单

[['pm', 15], ['pm', 15], ['pm', 15], ['pm', 15], ['gvt', 1], ['tools', 2], ['drm', 14], ['vgem', 12], ['template', 2], ['gem', 101], ['gem', 150], ['gem', 150], ['gem', 150], ['gem', 150], ['gem', 150], ['gem', 150], ['gem', 150], ['gem', 150], ['gem', 150], ['gem', 150], ['gem', 150], ['gem', 150], ['gem', 150], ['gem', 150], ['gem', 150], ['kms', 7], ['kms', 150], ['kms', 150], ['kms', 150], ['kms', 150], ['kms', 150], ['kms', 150], ['kms', 150], ['kms', 150], ['kms', 150], ['meta', 8], ['drv', 24], ['gen3', 5], ['sw', 18], ['syncobj', 81], ['gen7', 1], ['testdisplay', 1], ['debugfs', 3], ['perf', 27], ['core', 17], ['prime', 134]]

我正在努力使复发,例如:

从列表的这一部分

['pm', 15], ['pm', 15], ['pm', 15], ['pm', 15]

我想得到以下信息:

pm发现了4次,4个值为15

另一个例子:

['kms', 7], ['kms', 150], ['kms', 150], ['kms', 150], ['kms', 150], ['kms', 150], ['kms', 150], ['kms', 150], ['kms', 150], ['kms', 150]

kms发现了10次,其中9次为150,1次为7

到目前为止,我想知道如何才能做到这一点?你知道吗


Tags: gemtemplateswtoolsmetakmsdrvdrm
3条回答

您可以使用itertools.groupbycollections.Counter

import itertools
from collections import Counter
s = [['pm', 15], ['pm', 15], ['pm', 15], ['pm', 15], ['gvt', 1], ['tools', 2], ['drm', 14], ['vgem', 12], ['template', 2], ['gem', 101], ['gem', 150], ['gem', 150], ['gem', 150], ['gem', 150], ['gem', 150], ['gem', 150], ['gem', 150], ['gem', 150], ['gem', 150], ['gem', 150], ['gem', 150], ['gem', 150], ['gem', 150], ['gem', 150], ['gem', 150], ['kms', 7], ['kms', 150], ['kms', 150], ['kms', 150], ['kms', 150], ['kms', 150], ['kms', 150], ['kms', 150], ['kms', 150], ['kms', 150], ['meta', 8], ['drv', 24], ['gen3', 5], ['sw', 18], ['syncobj', 81], ['gen7', 1], ['testdisplay', 1], ['debugfs', 3], ['perf', 27], ['core', 17], ['prime', 134]]
new_s = [(a, list(b)) for a, b in itertools.groupby(sorted(s, key=lambda x:x[0]), key=lambda x:x[0])]
for a, b in new_s:
   print("{} found {} times, with {}".format(a, len(b), ' '.join('{} values of {}'.format(d, c) for c, d in Counter([i[-1] for i in b]).items())))

输出:

core found 1 times, with 1 values of 17
debugfs found 1 times, with 1 values of 3
drm found 1 times, with 1 values of 14
drv found 1 times, with 1 values of 24
gem found 16 times, with 1 values of 101 15 values of 150
gen3 found 1 times, with 1 values of 5
gen7 found 1 times, with 1 values of 1
gvt found 1 times, with 1 values of 1
kms found 10 times, with 9 values of 150 1 values of 7
meta found 1 times, with 1 values of 8
perf found 1 times, with 1 values of 27
pm found 4 times, with 4 values of 15
prime found 1 times, with 1 values of 134
sw found 1 times, with 1 values of 18
syncobj found 1 times, with 1 values of 81
template found 1 times, with 1 values of 2
testdisplay found 1 times, with 1 values of 1
tools found 1 times, with 1 values of 2
vgem found 1 times, with 1 values of 12

输出语法有点泛化,但我相信你能把它修好。你知道吗

如果将这些记录转换为元组对您来说不是问题,那么您可以使用Counter来解决问题。你知道吗

示例解决方案

from collections import Counter

tuples = tuple(tuple(item) for item in items) # where items is your data
c = Counter(tuples)

希望有帮助。你知道吗

您可以使用Counter和defaultdict的组合

from collections import Counter, defaultdict

l = [['pm', 15], ['pm', 15], ['pm', 15], ['pm', 15], ['gvt', 1], ['tools', 2], ['drm', 14], ['vgem', 12], ['template', 2], ['gem', 101], ['gem', 150], ['gem', 150], ['gem', 150], ['gem', 150], ['gem', 150], ['gem', 150], ['gem', 150], ['gem', 150], ['gem', 150], ['gem', 150], ['gem', 150], ['gem', 150], ['gem', 150], ['gem', 150], ['gem', 150], ['kms', 7], ['kms', 150], ['kms', 150], ['kms', 150], ['kms', 150], ['kms', 150], ['kms', 150], ['kms', 150], ['kms', 150], ['kms', 150], ['meta', 8], ['drv', 24], ['gen3', 5], ['sw', 18], ['syncobj', 81], ['gen7', 1], ['testdisplay', 1], ['debugfs', 3], ['perf', 27], ['core', 17], ['prime', 134]]

# Get counts by converting lists to tuples
d = Counter((tuple(i) for i in l))

# Create an empty defaultdict with dict
final_d = defaultdict(dict)

# Fill the dict with values from tuples
for k, v in d.items():
    final_d[k[0]][k[1]] = v

final_d.keys()

返回包含以下键的词典:

dict_keys(['perf', 'pm', 'gen3', 'testdisplay', 'drv', 'syncobj', 'drm', 'gen7', 'gvt', 'template', 'gem', 'vgem', 'tools', 'kms', 'core', 'prime', 'sw', 'meta', 'debugfs'])

现在,您可以通过以下方式获取事件:

final_d.get('pm')
final_d.get('kms')

退货:

{15: 4} #pm
{7: 1, 150: 9} #kms

更新

另一种解决方案是从一开始就使用嵌套的defaultdict:

from collections import defaultdict

l = [['pm', 15], ['pm', 15], ['pm', 15], ['pm', 15], ['gvt', 1], ['tools', 2], ['drm', 14], ['vgem', 12], ['template', 2], ['gem', 101], ['gem', 150], ['gem', 150], ['gem', 150], ['gem', 150], ['gem', 150], ['gem', 150], ['gem', 150], ['gem', 150], ['gem', 150], ['gem', 150], ['gem', 150], ['gem', 150], ['gem', 150], ['gem', 150], ['gem', 150], ['kms', 7], ['kms', 150], ['kms', 150], ['kms', 150], ['kms', 150], ['kms', 150], ['kms', 150], ['kms', 150], ['kms', 150], ['kms', 150], ['meta', 8], ['drv', 24], ['gen3', 5], ['sw', 18], ['syncobj', 81], ['gen7', 1], ['testdisplay', 1], ['debugfs', 3], ['perf', 27], ['core', 17], ['prime', 134]]

# Create an empty defaultdict with dict
final_d = defaultdict(lambda: defaultdict(int))

# Fill the dict with values from tuples
for item in l:
    final_d[item[0]][item[1]] += 1

相关问题 更多 >