计算不同列表中的相同元素

时间:2019-01-04 17:30:10

标签: python list dictionary

我想知道如何计算相同元素出现在Python不同列表中的次数。列表数量不确定

例如:

[house,table,house]
[desk,computer,table] 

将返回带有

的字典
{house:[2,0], table:[1,1], desk:[0,1]}

4 个答案:

答案 0 :(得分:1)

如果您不想导入其他工具,一种简单的方法是使用dictionary来保持计数。

SELECT a.orderID,MAX(a.DeliveryDate) DeliveryDate,SUM(a.Quantity) Quantity,b.ReceivedDate,b.ReceivedQuantity
FROM Orders a,
(SELECT orderID,MAX(ReceivedDate) ReceivedDate, SUM(ReceivedQuantity) ReceivedQuantity 
FROM Received
GROUP BY orderID) b
WHERE a.OrderID = b.OrderID 
GROUP BY a.orderID,b.ReceivedDate,b.ReceivedQuantity
HAVING SUM(a.Quantity)<=b.ReceivedQuantity

答案 1 :(得分:0)

它看起来很有趣,所以我尝试了一下。它将与任意数量的列表一起使用

@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = MainApplication.class, webEnvironment = WebEnvironment.RANDOM_PORT)

Voila,您应该得到如下输出

a= [['house','table','house'],['desk','computer','table']]
from itertools import chain
d={}
for key in set(chain(*a)): #chain to get all the unique keys possible
    d.setdefault(key,[]) # set default value of every key as an empty list

for lis in a:           # Iterate over the list of lists

    for j in set(lis):  #convert the list to set as we do not want to count the items multiple times
        d[j].append(lis.count(j)) #append the count to the list associated with the key

    for key in set(d.keys())-set(lis): #keys which are present in power set but not in subset means item is not present
        d[key].append(0)    #append count as 0

print(d)

答案 2 :(得分:0)

您可以创建一个函数,该函数接受任意数量的列表,将遍历该列表,对于每个存在的元素,您将计算每个列表的出现次数:

list1 = ['house','table','house']
list2 = ['desk','computer','table']

def countElements(*lists):
    elements = set([elem for l in lists for elem in l])
    return dict((element,[l.count(element) for l in lists]) for element in elements)

print(countElements(list1,list2))

结果:

{'table': [1, 1], 'computer': [0, 1], 'house': [2, 0], 'desk': [0, 1]}

或者,您可以将所有这些列表存储在另一个列表中,然后以相同的方式再次遍历它们:

list1 = ['house','table','house']
list2 = ['desk','computer','table']

def countElements(lists):
    elements = set([elem for l in lists for elem in l])
    return dict((element,[l.count(element) for l in lists]) for element in elements)

print(countElements([list1,list2]))

请注意函数签名和函数调用的不同(参数是列表的列表)。

答案 3 :(得分:0)

更详细但又易于理解。

首先将列表放置在包含的sueperlist中:

superlist = [l1, l2]

然后使用所需的键(每个单词出现在列表中)初始化字典(word_count):

word_count={}
for lst in superlist:
  for w in set(lst):
    word_count.setdefault(w, list())

最后,遍历word_count以获取遇到的单词的数量:

for word in word_count.keys():
  for lst in superlist:
    word_count[word].append(lst.count(word))

现在word_count包含:

#=> {'house': [2, 0], 'table': [1, 1], 'desk': [0, 1], 'computer': [0, 1]}


所有方法可用于任意数量的列表:

def count_words(*superlist):
  word_count={}
  for lst in superlist:
    for w in set(lst):
      word_count.setdefault(w, list())
  for word in word_count.keys():
    for lst in superlist:
      word_count[word].append(lst.count(word))
  return word_count

print(count_words(l1, l2))
#=> {'table': [1, 1], 'house': [2, 0], 'desk': [0, 1], 'computer': [0, 1]}