我有一个列表,如:
{ ['cat', 'fish']: 3, ['cat', 'dog']: 1,['cat','bird']:1
['fish','dog'] : 1, ['fish','bird']:2}
我想计算每个名字在整个列表中提到的次数和输出的次数:
from collections import Counter
from collections import defaultdict
co_occurences = defaultdict(Counter)
for tags in names:
for key in tags:
co_occurences[key].update(tags)
print co_occurences
我试过了:
$oldFigure = 14;
$newFigure = 12.50;
$percentChange = (1 - $oldFigure / $newFigure) * 100;
echo $percentChange;
但它并不计算主列表中的co =出现次数。
答案 0 :(得分:3)
在这里。我使用<=
来测试一个集合是否是另一个集合的子集(集合没有顺序,每个元素只出现一次)。
import itertools
from pprint import pprint
names = [['cat', 'fish'],
['cat'],
['fish', 'dog', 'cat'],
['cat', 'bird', 'fish'],
['fish', 'bird']]
# Flatten the list and make all names unique
unique_names = set(itertools.chain.from_iterable(names))
# Get all combinations of pairs
all_pairs = list(itertools.combinations(unique_names, 2))
# Create the dictionary
result = {pair: len([x for x in names if set(pair) <= set(x)]) for pair in all_pairs}
pprint(result)
这是输出
{('bird', 'cat'): 1,
('bird', 'dog'): 0,
('bird', 'fish'): 2,
('dog', 'cat'): 1,
('fish', 'cat'): 3,
('fish', 'dog'): 1}
我建议将其放在专用函数len([x for x in names if set(pair) <= set(x)])
中以获取字典的值。
答案 1 :(得分:2)
您可以使用itertools.combinations
和itertools.chain
作为:
>>> from itertools import combinations, chain
>>> names = [['cat', 'fish'], ['cat'], ['fish', 'dog', 'cat'],
... ['cat', 'bird', 'fish'], ['fish', 'bird']]
>>> uniques = set(chain(*names))
>>> {x: sum(1 for n in names if all(i in n for i in x)) for x in combinations(uniques, 2)}
{('fish', 'dog'): 1, ('dog', 'cat'): 1, ('bird', 'fish'): 2, ('fish', 'cat'): 3, ('bird', 'dog'): 0, ('bird', 'cat'): 1}
答案 2 :(得分:2)
您可以在python中使用按位AND,并通过将列表列表转换为集合列表来比较它们
def listOccurences(item, names):
# item is the list that you want to check, eg. ['cat','fish']
# names contain the list of list you have.
set_of_items = set(item) # set(['cat','fish'])
count = 0
for value in names:
if set_of_items & set(value) == set_of_items:
count+=1
return count
names = [['cat', 'fish'], ['cat'], ['fish', 'dog', 'cat'],['cat', 'bird', 'fish'], ['fish', 'bird']]
# Now for each of your possibilities which you can generate
# Chain flattens the list, set removes duplicates, and combinations generates all possible pairs.
permuted_values = list(itertools.combinations(set(itertools.chain.from_iterable(names)), 2))
d = {}
for v in permuted_values:
d[str(v)] = listOccurences(v, names)
# The key in the dict being a list cannot be possible unless it's converted to a string.
print(d)
# {"['fish', 'dog']": 1, "['cat', 'dog']": 1, "['cat', 'fish']": 3, "['cat', 'bird']": 1, "['fish', 'bird']": 2}
您可以使用此属性并获得您想要的计数。
csrf_protection = TRUE
答案 3 :(得分:1)
首先计算单词的所有2个组合,如果两个术语出现在列表项中,则在result
字典中增加其值:( l
是整个列表中不同元素的列表):
from collections import defaultdict
from itertools import combinations, chain
names = [['cat', 'fish'], ['cat'], ['fish', 'dog', 'cat'],
['cat', 'bird', 'fish'], ['fish', 'bird']]
l = set(chain.from_iterable(names)) # {'dog', 'bird', 'cat', 'fish'}
result = defaultdict(int)
for x in (list(combinations(l, 2))):
for y in names:
if((x[0] in y) and (x[1] in y)):
result[x[0],x[1]] += 1
result # defaultdict(<class 'int'>, {('fish', 'bird'): 2, ('cat', 'dog'): 1, ('cat', 'fish'): 3, ('fish', 'dog'): 1, ('cat', 'bird'): 1})
答案 4 :(得分:0)
此处列出的解决方案对我的大型数据集(数十万个)不起作用,它们太慢了。以下解决方案更快,只需要几分之一秒。
点击此处的Counter类
https://docs.python.org/2/library/collections.html#collections.Counter
# generate combinations for each sub list seperately
lists_of_pairs = [list(itertools.combinations(sub_list, 2)) for sub_list in names]
# flatten the lists of pairs to 1 large list of pairs
all_pairs = [pair for pairs_list in lists_of_pairs for pair in pairs_list]
# let the Counter do the rest for you
co_occurences_counts = Counter(all_pairs)