Return 比较 2 个列表并返回 list2 中存在的 list1 元素出现次数后的列表

Return a list after comparing 2 list and returning the count of occurrences in list1 elements that exist in list2

如果我想要 return 与 list2 相比 list1 中的值的出现列表,如下所示?

list1 = [1, 2, 3, 4, 5]

list2 = [5, 6, 7, 8, 9]

我希望 1 出现 0 次,2 出现 0 次,3 出现 0 次,4 出现 0 次,5 出现 1 次;有一个新的列表如下,

new_list = [1, 0, 0, 0, 0]

见下文的实施。我做错了什么?

from collections import Counter
list1 = [1, 2, 3, 4, 5]
list2 = [5, 6, 7, 8, 9]

def matchingStrings(list1, list2):
    count_all=Counter(list1)
    counts= {x: count_all[x] for x in list2 if x in list1 }
    list_output=list(counts.values())
    print(list_output)
    return list_output
    # Write your code here


if __name__ == '__main__':
    matchingStrings(list1,list2)

输出

预期输出

[1, 0, 0, 0, 0]

尝试使用 count 方法。

list1 = [1, 2, 3, 4, 5]

list2 = [5, 6, 7, 8, 9]
def matchingStrings(list1, list2):
    new_list = [list1.count(a) for a in list2]
    print(new_list)
    return new_list


if __name__ == '__main__':
    matchingStrings(list1,list2)

输出:

[1, 0, 0, 0, 0]

直接将一组他需要的值传递给 Counter 然后迭代 list1 以获得它们的计数或 0

def matchingStrings(list1, list2):
    counts = Counter(list1)
    return [counts.get(value, 0) for value in list2]

print(matchingStrings(list1, list2))  # [1, 0, 0, 0, 0]

Counter 对比 list.count

的基准
from collections import Counter, defaultdict
from datetime import datetime
import numpy as np

def matchingStrings(list1, list2):
    counts = Counter(list1)
    return [counts.get(value, 0) for value in list2]

def matchingStrings2(list1, list2):
    return [list1.count(a) for a in list2]

if __name__ == '__main__':
    nb = 5000
    times = defaultdict(list)
    for i in range(10):
        list1 = list(np.random.randint(0, 100, nb))
        list2 = list(np.random.randint(0, 100, nb))

        s = datetime.now()
        x1 = matchingStrings(list1, list2)
        times["counter"].append(datetime.now() - s)

        s = datetime.now()
        x2 = matchingStrings2(list1, list2)
        times["list"].append(datetime.now() - s)

    print(np.mean(times['list']) / np.mean(times['counter']))

    for key, values in times.items():
        print(f"{key:7s} => {np.mean(values)}")

Counter 解的复杂度为 2nlist.count 解的复杂度为

这是一些数字

nb = 1000                  # lists size
39.18844022169438          # counter 50 times faster
counter => 0:00:00.001263
list    => 0:00:00.049495

nb = 5000                  # lists size
481.512173128945           # counter 500 times faster
counter => 0:00:00.003327
list    => 0:00:01.601991

nb = 10000                 # lists size
1104.0679151061174         # counter 1000 times faster
counter => 0:00:00.004005
list    => 0:00:04.421792

一个小的更正修复了:

from collections import Counter
list1 = [1, 2, 3, 4, 5]
list2 = [5, 6, 7, 8, 9]

def matchingStrings(list1, list2):
    count_all=Counter(list2)
    counts= {x: count_all[x] for x in list1 }
    list_output=list(counts.values())
    print(list_output)
    return list_output
    # Write your code here


if __name__ == '__main__':
    matchingStrings(list1,list2)