数据结构与算法:才知道字典可以这么用

在这里插入图片描述
在这里插入图片描述

n ,m = map(int, input().split())
money = list(map(int, input().split()))
for _ in range(m):
    a = int(input())
    print(money.count(a))

在这里插入图片描述
现在可以使用字典的方法

n ,m = map(int, input().split())
money = list(map(int, input().split()))
dic = {}
for i in money:
    c = dic.get(i,0)
    dic[i] = c + 1
for _ in range(m):
    a = int(input())
    print(dic.get(a,0))

发布了50 篇原创文章 · 获赞 2 · 访问量 1835

猜你喜欢

转载自blog.csdn.net/liuluTL/article/details/105457367