我有这个:
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)
输出:
Counter({('hash1', 'hash2'): 2, ('hash2', 'hash3'): 1, ('hash1', 'hash3'): 1, ('hash3', 'hash4'): 1, ('hash5', 'hash4'): 1})
没关系,但我想要的结果是:
'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
有办法做到这一点吗
您可以使用itertools.groupby和 itertools.chain.from_iterable和random.choice
输出:
您也可以使用defaultdict
相关问题 更多 >
编程相关推荐