简体   繁体   English

计算python中第二个列表中列表项的出现次数

[英]count occurrences of list items in second list in python

a = list([1, 2, 3, 4, 5, 6, 7, 8, 9, 0])
b = list([1, 3, 6, 9])

How do I count the number of times an item in list be occurs in list a?如何计算列表中的项目出现在列表 a 中的次数?

The above example should return a value of 4.上面的示例应该返回值 4。

Whilst writing this question, I thought of the following (which appears to work)在写这个问题时,我想到了以下内容(似乎有效)

a = list([1, 2, 3, 4, 5, 6, 7, 8, 9, 0])
b = list([1, 3, 6, 9])
c = 0
for n in b:
    if n in a:
        c += 1
        continue
print (c)

But there must be a neater way using list comparisons or something?但是必须有一种使用列表比较或其他东西的更简洁的方法?

You can use built-in sum :您可以使用内置sum

sum(i in b for i in a)

Output:输出:

4

If you just want to count the number of elements that are in both lists (and you don't need to know how many times they occur in the other list) you can just use:如果您只想计算两个列表中元素的数量(并且您不需要知道它们在另一个列表中出现的次数),您可以使用:

count = len(set(a).intersection(set(b)))

Or identically:或者同样:

count = len(set(a) & set(b))

Simple way using set :使用set简单方法:

>>> len(set(a) & set(b))
4

This is an old question, take a look to: How can I compare two lists in python and return matches这是一个老问题,看看: 如何比较python中的两个列表并返回匹配项

Try this just in one line:在一行中试试这个:

s = sum(a.count(i) for i in b if i in a)

s will be 4 as output. s将是4作为输出。 Also, it supports duplicate items in a .此外,它支持重复的项目a

Here are a few variations that count duplicates and ignore all values that aren't in b.以下是一些计算重复项并忽略所有不在 b 中的值的变体。

from collections import Counter
# a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0]
a = [1, 4, 3, 1, 2, 4, 4, 5, 6, 6, 7, 7, 7, 7, 8, 9, 0, 1]
b = [1, 3, 6, 9]

counts = Counter()
# make counts order match b order
for item in b:
    counts[item] = 0
for item in a:
    if item in b:
        counts[item] += 1
print("in 'b' order")
print([(k, v) for k, v in counts.items()])
print("in descending frequency order")
print(counts.most_common())
print("count all occurrences in a of elements that are also in b")
print(sum(counts.values()))



python count_b_in_a.py
in 'b' order
[(1, 3), (3, 1), (6, 2), (9, 1)]
in descending frequency order
[(1, 3), (6, 2), (3, 1), (9, 1)]
count all occurrences in a of elements that are also in b
7

Responding to your comment about performance, here's a comparison between scanning a list and scanning a set in Python:针对您对性能的评论,这里是在 Python 中扫描列表和扫描集合之间的比较:

import datetime

def timestamp():
    return datetime.datetime.now()


def time_since(t):
    return (timestamp() - t).microseconds // 1000


a = list(range(1000_000))
b = set(a)
iterations = 10
t = timestamp()
for i in range(iterations):
    c = 974_152 in a
print("Finished {iterations} iterations of list scan in {duration}ms"
      .format(iterations=iterations, duration=time_since(t)))
t = timestamp()
for i in range(iterations):
    c = 974_152 in b
print("Finished {iterations} iterations of set scan in {duration}ms"
      .format(iterations=iterations, duration=time_since(t)))

python scan.py
Finished 10 iterations of list scan in 248ms
Finished 10 iterations of set scan in 0ms

First point to note: Python's no slouch at either.要注意的第一点:Python 也毫不逊色。 1/4 second on an old laptop to scan 10 million list elements isn't bad.在旧笔记本电脑上用 1/4 秒扫描 1000 万个列表元素也不错。 But it's still a linear scan.但它仍然是线性扫描。

Python sets are in a different class. Python 集在不同的类中。 If you take the // 1000 out of time_since() , you'll see that Python scans a 1-million member set 10 times in under a microsecond.如果从time_since()取出// 1000 ,您将看到 Python 在不到一微秒的时间内扫描了 100 万个成员集 10 次。 You'll find other set operations are also lightning fast.你会发现其他设置操作也快如闪电。 Wherever sets apply in Python, use them: they're fantastic.无论集合适用于 Python 的何处,都可以使用它们:它们很棒。

And if you're contemplating applying the above code to much bigger lists, where performance matters, the first thing to do might be to convert b to a set.如果您正在考虑将上述代码应用于更大的列表,其中性能很重要,那么首先要做的可能是将b转换为集合。

This one-liner should work as well.这种单线也应该有效。 Find the count of each element and sum those counts up找到每个元素的计数并将这些计数相加

op = sum([a.count(j) for j in b])

The outputs will be输出将是

a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0]
b = [1, 3, 6, 9]
#4
a = [1, 1, 2, 3, 3, 3, 4, 5, 6, 6 , 7, 8, 9, 0]
b = [1, 3, 6, 9]
#8

While set works for unique虽然 set 适用于独特的

List comprehension considers duplicates as well列表理解也考虑重复

a = [1,1]
b = [1,1]

count = len(set(a) & set(b)) 
count2 = sum(i in b for i in a)  
print(count, count2) 
1 2

[Program finished] 

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM