在python字典中查找特定值

在python字典中查找特定值,python,dictionary,Python,Dictionary,我有麻烦了。这是我的代码,我想检查字典中是否存在特定的值。这是我的密码。我认为逻辑是正确的,但语法不正确。请帮帮我。多谢各位 a = [ {'amount':200, 'currency':'php'}, {'amount':100, 'currency':'usd'} ] result1 = 200 in a result2 = 'php' in a result = result1 and result2 print result 我希望

我有麻烦了。这是我的代码,我想检查字典中是否存在特定的值。这是我的密码。我认为逻辑是正确的,但语法不正确。请帮帮我。多谢各位

a = [
        {'amount':200, 'currency':'php'},
        {'amount':100, 'currency':'usd'}
        ]

result1 = 200 in a
result2 = 'php' in a
result = result1 and result2

print result

我希望得到“真”的结果

您可以执行以下操作

a = [
    {'amount':200, 'currency':'php'},
    {'amount':100, 'currency':'usd'}
    ]

for i in a:
    if 200 in i.values():
        result1=True

    if "php" in i.values():
        result2=True

result = result1 and result2
print result

你可以这样做

a = [
    {'amount':200, 'currency':'php'},
    {'amount':100, 'currency':'usd'}
    ]

for i in a:
    if 200 in i.values():
        result1=True

    if "php" in i.values():
        result2=True

result = result1 and result2
print result
线路

result1 = 200 in a
查找值为
200
的列表元素。 但是列表元素是字典。所以你的期望是不可能实现的

因此,假设您的目标是检查list
a
的任何元素(即字典)中是否包含特定值,您应该编写

result1 = any(200 in el.values() for el in a)
result2 = any('php' in el.values() for el in a)

result = result1 and result2
print result
产生

True
线路

result1 = 200 in a
查找值为
200
的列表元素。 但是列表元素是字典。所以你的期望是不可能实现的

因此,假设您的目标是检查list
a
的任何元素(即字典)中是否包含特定值,您应该编写

result1 = any(200 in el.values() for el in a)
result2 = any('php' in el.values() for el in a)

result = result1 and result2
print result
产生

True

使用iteritems通过字典迭代获取其键和值

a = [
        {'amount':200, 'currency':'php'},
        {'amount':100, 'currency':'usd'}
        ]

for lst in a:
    for k,v in lst.iteritems():
        if 200 == v:
            res1 = 'True'
        if 'php' == v:
            res2 = 'True'
print res1 and res

使用iteritems通过字典迭代获取其键和值

a = [
        {'amount':200, 'currency':'php'},
        {'amount':100, 'currency':'usd'}
        ]

for lst in a:
    for k,v in lst.iteritems():
        if 200 == v:
            res1 = 'True'
        if 'php' == v:
            res2 = 'True'
print res1 and res
可能的重复可能的重复