简体   繁体   English

如何计算项目在另一个列表的列表中出现的次数

[英]How to count the number of times an item occurs in a list base on another list

suppose I have two lists as follows: 假设我有两个列表如下:

list1 = ["a","b","a","a","b","a","b","a","b","b","b"]
list2 = ["pos","neg","pos","neu","neg","pos","pos","pos","neg","neu","pos"]

I want to count the number of times "pos" , "neg" and "neu" has occurred for each item in list1 . 我想计算list1每个项目发生"pos""neg""neu"次数。

So the number of times "pos","neg" and "neu" occurs with "a" , and for "b" . 因此"pos","neg""neu""a""b" For example, the first element in list1 , "a" has a "pos" value because list2[0] is for "pos" . 例如, list1的第一个元素"a"具有"pos"值,因为list2[0]用于"pos"

What is the best approach for this? 对此最好的方法是什么? I feel there is a much better solution out there in comparison to what I have done at the moment. 我觉得与我目前所做的相比,有更好的解决方案。 I can see that if more unique items exist in list1 my approach will not be feasible. 我可以看到,如果list1存在更多独特的项目,我的方法将不可行。

list1 = ["a","b","a","a","b","a","b","a","b","b","b"]
list2 = ["pos","neg","pos","neu","neg","pos","pos","pos","neg","neu","pos"]

a_pos = 0
a_neg = 0
a_neu = 0
b_pos = 0
b_neg = 0
b_neu = 0

for i in range(len(list1)):
    if list1[i] == "a":
        if list2[i] == "pos":
            a_pos +=1
        elif list2[i] == "neg":
            a_neg +=1
        else:
            a_neu +=1
    if list1[i] == "b":
        if list2[i] == "pos":
            b_pos +=1
        elif list2[i] == "neg":
            b_neg +=1
        else:
            b_neu +=1       

print(a_pos,a_neg,a_neu)
print(b_pos,b_neg,b_neu)

You could use Counter with zip : 你可以使用Counter with zip

from collections import Counter
Counter(zip(list1, list2))

Counter({('a', 'pos'): 4,
         ('b', 'neg'): 3,
         ('a', 'neu'): 1,
         ('b', 'pos'): 2,
         ('b', 'neu'): 1})

Where zip is creating an iterable with the elements from both lists interleaved: zip创建一个iterable,其中两个列表中的元素交错:

[('a', 'pos'), ('b', 'neg'), ('a', 'pos'),...

So the above works because zip is returning tuples, which are hashable , a necessary condition for Counter to work as its elements are stored as a dictionary 所以上面的工作是因为zip返回了可以清除的元组,这 Counter工作的必要条件,因为它的元素存储为字典

You can zip the two lists together, and then use collections.Counter to count your co-occurences 您可以将两个列表压缩在一起,然后使用collections.Counter来计算您的共同出现次数

from collections import Counter
list1 = ["a","b","a","a","b","a","b","a","b","b","b"]
list2 = ["pos","neg","pos","neu","neg","pos","pos","pos","neg","neu","pos"]

print(Counter(zip(list1, list2)))

The output will be 输出将是

{('a', 'pos'): 4, ('b', 'neg'): 3, ('a', 'neu'): 1, ('b', 'pos'): 2, ('b', 'neu'): 1}

To break it down, zip takes both of your lists, and create an iterator with elements each from each of the lists interleaved 为了将其分解, zip将获取两个列表,并创建一个迭代器,其中每个列表都交错存在元素


In [1]: from collections import Counter 
   ...: list1 = ["a","b","a","a","b","a","b","a","b","b","b"] 
   ...: list2 = ["pos","neg","pos","neu","neg","pos","pos","pos","neg","neu","pos"]                                                                                                                     

In [2]: list(zip(list1,list2))                                                                                                                                                                          
Out[2]: 
[('a', 'pos'),
 ('b', 'neg'),
 ('a', 'pos'),
 ('a', 'neu'),
 ('b', 'neg'),
 ('a', 'pos'),
 ('b', 'pos'),
 ('a', 'pos'),
 ('b', 'neg'),
 ('b', 'neu'),
 ('b', 'pos')]

We then take this output, and put in into a Counter , which calculate the frequency of each item in the iterator and provides us a dictionary, this is possible because the key of the dictionary is a tuple which is a hashable type. 然后,我们借此输出,并放入到一个Counter ,它计算每个项目的频率在迭代器,并提供我们的字典,这是可能的,因为字典的关键是一个tuple是一个哈希的类型。

In [3]: Counter(list(zip(list1,list2)))                                                                                                                                                                 
Out[3]: 
Counter({('a', 'pos'): 4,
         ('b', 'neg'): 3,
         ('a', 'neu'): 1,
         ('b', 'pos'): 2,
         ('b', 'neu'): 1})

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 计算项目在每个嵌套列表中出现的次数 - Count the number of times an item occurs in each nested list Python | 返回项目在列表中出现的次数 - Python | Return the number of times the item occurs in the list 如何使用.count计算一个列表中每个项目出现在python中另一个列表中的次数? - How to use .count to calculate number of times each item in one list appears in another list in python? 如何计算子列表中某个特定模式在列表中出现的次数,然后将该计数追加到子列表中? - How to count the number of times a certain pattern in a sublist occurs within a list and then append that count to the sublist? 我需要编写一个 function 来返回一个数字在另一个列表中出现的次数的列表 - I need to write a function that returns a list of how may times a number occurs in another list 如何列出每个项目出现不同次数的列表? - How do I make a list where each item occurs a different number of times? 计算列表中每个项目在 Pandas 数据框列中出现的次数,用逗号将值与其他列的附加聚合分开 - Count number of times each item in list occurs in a pandas dataframe column with comma separates values with additional aggregation of other columns 如何列出另一个列表中出现 integer 的时间? - How can I make a list with the times that an integer occurs in another list? 如何计算一个列表中一个数字重复多少次(以及是否基于另一个列表中的值不使用一个数字)? - How to count how many times a number is repeated in a list( and if a number is not used based on values from another list )? 如何检查列表中的任何项是否出现在另一个列表中? - How to check if any item in a list occurs in another list?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM