Python在不同列表中同时出现的两项 [英] Python co-occurrence of two items in different lists
本文介绍了Python在不同列表中同时出现的两项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个列表列表,如:
I have a list of lists like :
names = [['cat', 'fish'], ['cat'], ['fish', 'dog', 'cat'],
['cat', 'bird', 'fish'], ['fish', 'bird']]
我想计算在整个列表中在一起提到的每对名字的次数,输出结果将是:
I want to count number of times that each pair of names mentioned together in the whole list and the output would be like:
{ ['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
,但它不计算主列表中的共发生次数.
but it doesn't count co=occurrences in the main list.
推荐答案
您可以在python中使用按位与,并通过将列表列表转换为集合列表来进行比较
You could use the bitwise AND in python and compare them by converting the list of lists to a list of sets
>>> set(['cat','dog']) & set(['cat','dog','monkey','horse','fish'])
set(['dog', 'cat'])
您可以使用此属性并获得所需的计数.
You could use this property and achieve the count you've wanted.
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}
这篇关于Python在不同列表中同时出现的两项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文