Python 将字典中的条目与列表进行比较并打印结果

Python 将字典中的条目与列表进行比较并打印结果,python,list,dictionary,compare,Python,List,Dictionary,Compare,我有一个问题,关于如何浏览字典并将里面的内容与参数列表进行比较 假设我有这本字典:我们把这本字典叫做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

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

假设我有这本字典:我们把这本字典叫做
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不同的参数

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

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']

列表:
[''鸟:鸟=3',猫:猫=40',狗:狗=10',蝙蝠:蝙蝠=10',老虎:老虎=19',鱼:鱼=234']


当前代码:

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

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'}]

我想你的字典和列表有问题,你能把它们贴出来吗?@Leu 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=40','Tiger':'Tiger=19','Bird':'Bird=4'}]
没有格式,这是我的字典,这是我的列表:
[''Bird:Bird=3','Cat:Cat=40','Dog:Dog=10','Bat Bat Bat 10','Tiger:Tiger=19','Fish:Fish=234']
ok,问题是您试图比较类似字符串“Bat”的字典的键与字符串列表“Bat:Bat=10”。这些字符串不是等效的,因此它不会在您提供的第一个循环中打印任何内容,这是因为您可能没有正确地拆分文件中正在读取的行。查看生成字典的代码以及尝试使用
[i.split(:“”[1]在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:如果您对字典的格式感兴趣(在值中重复键),我认为这是一种明智的方法,因为这意味着不必为了输出而重建输入行。
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'}]
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")