【问题标题】:Compare Entries in a dictionary with a list and print the results将字典中的条目与列表进行比较并打印结果
【发布时间】:2015-08-21 14:40:57
【问题描述】:

我有一个关于如何浏览字典并将里面的内容与参数列表进行比较的问题。

假设我有这本字典:让我们称之为字典setData[i]

<231931844151>
    Bat: Bat = 10
    Fish: Fish = 16
    Dog: Dog = 5
    Cat: Cat = 4
    Tiger: Tiger = 11
    Bird: Bird = 3

<92103884812>
    Bat: Bat = Null
    Fish: Fish = 24
    Dog: Dog = 10
    Cat: Cat = 40
    Tiger: Tiger = 19
    Bird: Bird = 4

这本字典保存了我们称之为 ID 号的数据,这些 ID 号具有带参数的数据,在本例中为 Bat、Fish、Dog、Cat、Tiger 和 Bird。

现在我想将此数据及其参数与列表进行比较,以便查看它们是否正确匹配。

我们的列表是这样的:我们称之为defaultData

<ID NUMBER>
Bird = 3
Cat = 40
Dog = 10
Bat = 10
Tiger = 19
Fish = 234

所以一种看待它的方式是:


因此,我们可以看到列表与字典中的每个条目进行比较,如果它们不同,它将打印出哪个 ID 不同的参数。

代码:

到目前为止,我一直在考虑尝试以下循环:

for k in setData[i]:
        if setData[i] in dataDefault:
            print("If this prints then something Matches")

这只是循环的开始,但是我似乎它们不匹配或列表中没有条目出现在字典中。可能是由于创建字典时添加了两次参数的问题吗?就像 Bat: Bat = 10 而不是 Bat = 10 的情况?

如果有更好的方法将字典中的条目与我想知道的列表进行比较

谢谢!

编辑:添加我的字典和列表:

字典:
[{'Bat': 'Bat = 10', 'Fish': 'Fish = 16', 'Dog': 'Dog = 5', 'Cat': 'Cat = 4', 'Tiger': 'Tiger = 11', 'Bird': 'Bird = 3'}, {'Bat': 'Bat = Null', 'Fish': 'Fish = 24', 'Dog': 'Dog = 10', 'Cat': 'Cat = 40', 'Tiger': 'Tiger = 19', 'Bird': 'Bird = 4'}]

名单:
['&lt;Correct Parameters&gt;', 'Bird: Bird = 3', 'Cat: Cat = 40', 'Dog: Dog = 10', 'Bat: Bat = 10', 'Tiger: Tiger = 19', 'Fish: Fish = 234']

当前代码:https://dpaste.de/Zvdn

【问题讨论】:

  • 我认为您的字典和列表有问题,请您发布一下吗?
  • @le_vine 我添加了一个编辑,其中包含用于创建字典和列表的代码的完整副本。
  • [{'Bat': 'Bat = 10', 'Fish': 'Fish = 16', 'Dog': 'Dog = 5', 'Cat': 'Cat = 4', 'Tiger': 'Tiger = 11', 'Bird': 'Bird = 3'}, {'Bat': 'Bat = Null', 'Fish': 'Fish = 24', 'Dog': 'Dog = 10', 'Cat': 'Cat = 40', 'Tiger': 'Tiger = 19', 'Bird': 'Bird = 4'}] 没有格式化这是我的字典,这是我的列表:['&lt;Correct Parameters&gt;', 'Bird: Bird = 3', 'Cat: Cat = 40', 'Dog: Dog = 10', 'Bat: Bat = 10', 'Tiger: Tiger = 19', 'Fish: Fish = 234']
  • 好的,问题是您正在尝试比较字典的键,即像“Bat”这样的字符串与字符串“Bat: Bat = 10”的列表。这些字符串不等价,因此它不会在您提供的第一个循环中打印任何内容
  • 那是因为您可能没有正确拆分文件中正在读取的行。查看生成字典和列表的代码会很有用

标签: python list dictionary compare


【解决方案1】:

问题似乎来自您的数据格式。我们应该准备数据来比较它

dataDefault = ['<Correct Parameters>', 'Bird: Bird = 3', 'Cat: Cat = 40', 'Dog: Dog = 10', 'Bat: Bat = 10', 'Tiger: Tiger = 19', 'Fish: Fish = 234']
setData = [{'Bat': 'Bat = 10', 'Fish': 'Fish = 16', 'Dog': 'Dog = 5', 'Cat': 'Cat = 4', 'Tiger': 'Tiger = 11', 'Bird': 'Bird = 3'}, {'Bat': 'Bat = Null', 'Fish': 'Fish = 24', 'Dog': 'Dog = 10', 'Cat': 'Cat = 40', 'Tiger': 'Tiger = 19', 'Bird': 'Bird = 4'}]

首先我们更改 dataDefault 格式,删除 ":" 之前的所有内容:

dataDefault2 = []
for i in dataDefault:
    if ": " in i:
        dataDefault2.append(i.split(": ")[1])
    else:
        dataDefault2.append(i)

for elem in setData:  # We iterate over each element of the list
    for val in elem.values() :  # We iterate over each value of the element dictionary
    # If there is any other value which is not a dictionary it will complain with an error
        if val in dataDefault2:
            print("If this prints then something Matches")

【讨论】:

  • 尝试使用[i.split(": ")[1] for i in dataDefault] 拆分列表给我一个索引不足错误,我将进行故障排除以查看导致此问题的原因
  • 那是因为我们有 "''" 没有 ":"。我改了代码
  • 好的,我很想让它工作,但它仍然不正确匹配,可能是由于我的字典的格式吗?我的字典是[{'Bat': 'Bat = 10', 'Fish': 'Fish = 16', 'Dog': 'Dog = 5', 'Cat': 'Cat = 4', 'Tiger': 'Tiger = 11', 'Bird': 'Bird = 3'}, {'Bat': 'Bat = Null', 'Fish': 'Fish = 24', 'Dog': 'Dog = 10', 'Cat': 'Cat = 40', 'Tiger': 'Tiger = 19', 'Bird': 'Bird = 4'}] ,由于'Bat': 'Bat = 10' 与列表Bat = 10 不匹配,它不会打印出来?
  • 等一下,我会在一小时(或两个小时)内回答,我还有事要做:(
  • @Llopis:如果您对字典的格式感到好奇(键在值中重复),我认为在this answer 中这是一种明智的方法,因为这意味着没有重建输入行以输出它们。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-03-24
  • 2017-06-21
  • 2020-11-07
  • 1970-01-01
  • 2011-02-15
相关资源
最近更新 更多