假设我有这种格式的数据(假设制表符分隔)
1 10,11,15
2 12
3 12,11
4 10,11
如何遍历列表并计算第二列中最流行的对象对?假设第二列可以有无限数量的项目。
理想的输出将返回类似
pairs count
10,11 (2)
10,15 (1)
11,15 (1)
11,12 (1)
假设我有这种格式的数据(假设制表符分隔)
1 10,11,15
2 12
3 12,11
4 10,11
如何遍历列表并计算第二列中最流行的对象对?假设第二列可以有无限数量的项目。
理想的输出将返回类似
pairs count
10,11 (2)
10,15 (1)
11,15 (1)
11,12 (1)
这些都假设您可以将输入放入列表列表中:
如果您有 Python 2.7,请尝试Counter
结合使用itertools
:
>>> from collections import Counter
>>> from itertools import combinations
>>> l = [[10, 11, 15], [12], [12, 11], [10, 11]]
>>> c = Counter(x for sub in l for x in combinations(sub, 2))
>>> for k, v in c.iteritems():
... print k, v
...
(10, 15) 1
(11, 15) 1
(10, 11) 2
(12, 11) 1
如果你有 Python < 2.6,你可以defaultdict
结合使用 a itertools
(我敢肯定其中一位大师会提供更简洁的解决方案)。
In [1]: from collections import defaultdict
In [2]: from itertools import combinations
In [3]: l = [[10, 11, 15], [12], [12, 11], [10, 11]]
In [4]: counts = defaultdict(int)
In [5]: for x in l:
...: for item in combinations(x, 2):
...: counts[item] += 1
...:
...:
In [6]: for k, v in counts.iteritems():
...: print k, v
...:
...:
(10, 15) 1
(11, 15) 1
(10, 11) 2
(12, 11) 1
In [7]: with open("data1.txt") as f:
lis=[map(int,x.split(",")) for x in f]
...:
In [8]: Counter(chain(*[combinations(x,2) for x in lis]))
Out[8]: Counter({(10, 11): 2, (10, 15): 1, (11, 15): 1, (12, 11): 1})
你可以使用combinations
和一个Counter
.
from itertools import combinations
import collections
newinput = []
# Removes the tabs
for line in oldinput:
newinput.append(line.partition("\t")[2])
# set up the counter
c = collections.Counter()
for line in newinput:
# Split by comma
a = line.split(',')
# make into integers from string
a = map(int, a)
# add to counter
c.update(combinations(a, 2))
然后,你最终得到一个Counter
包含所有计数的:`(10,15): 1) 等。