比较2个列表并返回List2中存在的List1元素中的事件计数后返回列表2

发布于 2025-01-27 01:09:18 字数 880 浏览 2 评论 0原文

如果我想返回list1中的值的列表与list2相比,该怎么办?

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

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

我期望0出现1,0发生2,0发生3,0发生4,1出现5;要拥有下面的新列表,

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]

What if I want to return the list of occurrence of the values in list1 as compared to list2 as below?

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

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

I expect to get 0 occurrence of 1, 0 occurrence of 2, 0 occurrence of 3, 0 occurrence of 4, and 1 occurrence of 5; to have a new list as below,

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

See below by implementation. What am I doing wrong?

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)

Output

Expected output

[1, 0, 0, 0, 0]

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

一紙繁鸢 2025-02-03 01:09:18

直接将一组需要值传递给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> Counter vs 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)}")

计数器解决方案的复杂性是2n,而list.count.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

Directly pass a set of he needed values to the Counter then iterate on the list1 to get their count or 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]

Benchmark of Counter vs 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)}")

Complexity of Counter solution is 2n whereas list.count solution is

Here's a few numbers

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
浊酒尽余欢 2025-02-03 01:09:18

一个小的更正修复了:

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)

A small correction fixes that:

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)
烏雲後面有陽光 2025-02-03 01:09:18

尝试使用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]

Try using the count method.

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)

OUTPUT:

[1, 0, 0, 0, 0]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文