【问题标题】:How can I find values that are the same within a dictionary如何在字典中找到相同的值
【发布时间】:2019-04-05 06:31:44
【问题描述】:

目前我有一个代码可以创建一个字典,其中一个键和一个列表作为其值:

e.g. dct = {A: [hello, only], B: [hello, that], C: [hello, same]}

我想比较字典的不同值并确定所有键共享的值。在此示例中,“hello”由所有三个键共享。对python还是很陌生!太感谢了!

【问题讨论】:

  • 您的键和列表元素必须用引号括起来才能成为字符串

标签: python arrays list dictionary filter


【解决方案1】:

首先你应该正确地创建字典。
试试这个代码。

dct = {'A':['hello', 'only'], 'B':['hello', 'that'], 'C':['hello', 'same']}
s=set.intersection(*[set(list) for list in dct.values()])
print(s)

输出。

{'hello'}

【讨论】:

    【解决方案2】:
    set.intersection(*[set(list) for list in dct.values()])
    

    会为你工作。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-11-13
      • 2019-09-20
      • 2021-12-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-02-01
      相关资源
      最近更新 更多