Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
计算python中耦合列表的出现次数,并将结果与第一个列表中的所有元素一起追加到新列表中_Python_List_Counter_Find Occurrences - Fatal编程技术网

计算python中耦合列表的出现次数,并将结果与第一个列表中的所有元素一起追加到新列表中

计算python中耦合列表的出现次数,并将结果与第一个列表中的所有元素一起追加到新列表中,python,list,counter,find-occurrences,Python,List,Counter,Find Occurrences,我有这个: list = [ (('hash1', 'hash2'), (436, 1403)), (('hash1', 'hash2'), (299, 1282)), (('hash2', 'hash3'), (1244, 30)), (('hash1', 'hash3'), (436, 1403)), (('hash3', 'hash4'), (299, 1282)), (('hash5', 'hash4'), (1244, 30)),

我有这个:

list = [
    (('hash1', 'hash2'), (436, 1403)),
    (('hash1', 'hash2'), (299, 1282)),
    (('hash2', 'hash3'), (1244, 30)),
    (('hash1', 'hash3'), (436, 1403)),
    (('hash3', 'hash4'), (299, 1282)),
    (('hash5', 'hash4'), (1244, 30)),
    ]
我要数一数第一对夫妇发生了多少次

所以我这样做:

out = Counter((x[0]) for x in list)
输出:

没关系,但我想要的结果是:

'hash1','hash2,(436,1403)
我需要第二个值,它可以是随机的,所以在这种情况下可以是

(436, 1403) or `(299, 1282))`
预期产出:

Couple of hash, any couple of number of the hash1,hash2, N.occurrences
((hash1,hash2),(436,1403),2
有办法做到这一点吗?

您可以使用和 及

输出:

你也可以使用


您试图统计哪些事件?你的问题不清楚,你的预期输出也不清楚,但在结果中我需要数字,例如4361403。因此,对于hash1 hash2,计数2次,但我需要在最终结果中有hash1,hash2,4361403,2,像这样。你能更新你的预期输出吗?非常感谢!
Couple of hash, any couple of number of the hash1,hash2, N.occurrences
((hash1,hash2),(436,1403),2
from itertools import groupby, chain
from random import choice

lst = [(('hash1', 'hash2'), (436, 1403)),
    (('hash1', 'hash2'), (299, 1282)),
    (('hash2', 'hash3'), (1244, 30)),
    (('hash1', 'hash3'), (436, 1403)),
    (('hash3', 'hash4'), (299, 1282)),
    (('hash5', 'hash4'), (1244, 30))]

for k, g in groupby(lst, lambda x: x[0]):
    g = list(chain.from_iterable(g))[1::2]
    print(k, choice(g), len(g))
('hash1', 'hash2') (299, 1282) 2
('hash2', 'hash3') (1244, 30) 1
('hash1', 'hash3') (436, 1403) 1
('hash3', 'hash4') (299, 1282) 1
('hash5', 'hash4') (1244, 30) 1
from random import choice
from collections import defaultdict

res = defaultdict(list)
for x in lst:
    res[x[0]].append(x[1])

for k, v in res.items():
    print(k, choice(v), len(v))