简体   繁体   English

比较两个列表中的项目数

[英]Comparing count of items in two lists

I have two dictionaries as below:我有两个字典如下:

{'a': 2, 'd': 1}

{'a': 2, 'n': 1, 'd': 1, 'ç': 1}

I need to print "true" if the total count of each item in the second dictionary is equal or greater than count of the same item(s) in the first dictionary.如果第二个字典中每个项目的总计数等于或大于第一个字典中相同项目的计数,我需要打印“true”。

What I mean is:我的意思是:

If total count of "a" in second dictionary >= total count of "a" in first dictionary如果第二个词典中“a”的总数 >= 第一个词典中“a”的总数

AND

if total count of "d" in second dictionary >= total count of "d" in first dictionary如果第二个词典中“d”的总数 >= 第一个词典中“d”的总数

OUTPUT will be TRUE. OUTPUT 将为真。

I used below statement but somehow it did not work:我使用了以下语句,但不知何故它不起作用:

kelime_harf_sayisi[i] <= harfler_harf_sayisi[i]

Those are dictionaries ( dict ), not lists.这些是字典( dict ),而不是列表。

d1 = {'a': 2, 'd': 1}
d2 = {'a': 2, 'n': 1, 'd': 1, 'ç': 1}
answer = all(d2[k] >= d1[k] for k in d1)

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

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