[英]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.