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

时间:2019-05-16 12:07:48

标签: python algorithm

假设我有两个列表,如下所示:

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

我想计算"pos"中每个项目出现"neg""neu"list1的次数。

因此"pos","neg""neu"发生"a""b"的次数。例如,list1"a"中的第一个元素具有一个"pos"值,因为list2[0]用于"pos"

什么是最好的方法?与目前相比,我觉得有更好的解决方案。我可以看到,如果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)

2 个答案:

答案 0 :(得分:8)

您可以将Counter//inside the function updatestate(state) const state_json = {details : output} return JSON.stringify(state_json); 一起使用:

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}) 在使用两个列表中的元素交错的情况下创建可迭代对象的地方:

zip

以上操作之所以有效,是因为[('a', 'pos'), ('b', 'neg'), ('a', 'pos'),... 返回了元组,它们是可哈希的,这是zip能够工作的必要条件,因为其元素存储为字典

>

答案 1 :(得分:6)

您可以zip一起列出两个列表,然后使用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)))

输出将为

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

要分解它,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')]

然后我们将输出结果放入Counter中,它计算迭代器中每个项目的频率并为我们提供字典,这是可能的,因为字典的键是{{1 }},这是可散列的类型。

tuple