Python检查值是否在dicts列表中

时间:2022-02-18 18:05:42

I have a list of dicts e.g.

我有一个dicts列表,例如

[{'name':'Bernard','age':7},{'name':'George','age':4},{'name':'Reginald','age':6}]

I'd like to check to see if a string value is the same as the 'name' value in any of the dicts in the list. For example 'Harold' would be False, but 'George' would be True.

我想检查一下字符串值是否与列表中任何一个dicts中的'name'值相同。例如,'哈罗德'将是假的,但'乔治'将是真的。

I realise I could do this by looping through each item in the list, but I was wondering if there was a more efficient way?

我意识到我可以通过遍历列表中的每个项目来做到这一点,但我想知道是否有更有效的方法?

5 个解决方案

#1


14  

No, there cannot be a more efficient way if you have just this list of dicts.

不,如果您只有这个词典列表,就不会有更有效的方法。

However, if you want to check frequently, you can extract a dictionary with name:age items:

但是,如果要经常检查,可以提取名称为:age items的字典:

l = [{'name':'Bernard','age':7},{'name':'George','age':4},{'name':'Reginald','age':6}]
d = dict((i['name'], i['age']) for i in l)

now you have d:

现在你有了d:

{'Bernard': 7, 'George': 4, 'Reginald': 6}

and now you can check:

现在你可以检查:

'Harold' in d   -> False
'George' in d   -> True

It will be much faster than iterating over the original list.

它比迭代原始列表要快得多。

#2


7  

The Proper Solution

There is a much more efficient way to do this than with looping. If you use operators.itemgetter you can do a simple if x in y check

与循环相比,有一种更有效的方法。如果你使用operators.itemgetter你可以做一个简单的if x in y check

#to simply check if the list of dicts contains the key=>value pair
'George' in map(itemgetter('name'), list_of_dicts)

#if you want to get the index 
index = map(itemgetter('name'), list_of_dicts).index("George") if 'George' in map(itemgetter('name'), list_of_dicts) else None

#3


0  

l = [{'name':'Bernard','age':7},{'name':'George','age':4},{'name':'Reginald','age':6}]
search_for = 'George'
print True in map(lambda person: True if person['name'].lower() == search_for.lower() else False, l )

#4


0  

smf = [{'name':'Bernard','age':7},{'name':'George','age':4},{'name':'Reginald','age':6}]
def names(d):
    for i in d:
        for key, value in i.iteritems():
             if key == 'name':
                 yield value


In [5]: 'Bernard' in names(smf)
Out[5]: True


In [6]: 'Bernardadf' in names(smf)
Out[6]: False

#5


0  

I think a list comprehension would do the trick here too.

我认为列表理解也可以解决这个问题。

names = [i['name'] for i in l]

Then use it like so:

然后像这样使用它:

'Bernard' in names (True)
'Farkle' in names (False)

Or a one liner (If it's only one check)

或一个班轮(如果它只是一个检查)

'Bernard' in [i['name'] for i in l] (True)

#1


14  

No, there cannot be a more efficient way if you have just this list of dicts.

不,如果您只有这个词典列表,就不会有更有效的方法。

However, if you want to check frequently, you can extract a dictionary with name:age items:

但是,如果要经常检查,可以提取名称为:age items的字典:

l = [{'name':'Bernard','age':7},{'name':'George','age':4},{'name':'Reginald','age':6}]
d = dict((i['name'], i['age']) for i in l)

now you have d:

现在你有了d:

{'Bernard': 7, 'George': 4, 'Reginald': 6}

and now you can check:

现在你可以检查:

'Harold' in d   -> False
'George' in d   -> True

It will be much faster than iterating over the original list.

它比迭代原始列表要快得多。

#2


7  

The Proper Solution

There is a much more efficient way to do this than with looping. If you use operators.itemgetter you can do a simple if x in y check

与循环相比,有一种更有效的方法。如果你使用operators.itemgetter你可以做一个简单的if x in y check

#to simply check if the list of dicts contains the key=>value pair
'George' in map(itemgetter('name'), list_of_dicts)

#if you want to get the index 
index = map(itemgetter('name'), list_of_dicts).index("George") if 'George' in map(itemgetter('name'), list_of_dicts) else None

#3


0  

l = [{'name':'Bernard','age':7},{'name':'George','age':4},{'name':'Reginald','age':6}]
search_for = 'George'
print True in map(lambda person: True if person['name'].lower() == search_for.lower() else False, l )

#4


0  

smf = [{'name':'Bernard','age':7},{'name':'George','age':4},{'name':'Reginald','age':6}]
def names(d):
    for i in d:
        for key, value in i.iteritems():
             if key == 'name':
                 yield value


In [5]: 'Bernard' in names(smf)
Out[5]: True


In [6]: 'Bernardadf' in names(smf)
Out[6]: False

#5


0  

I think a list comprehension would do the trick here too.

我认为列表理解也可以解决这个问题。

names = [i['name'] for i in l]

Then use it like so:

然后像这样使用它:

'Bernard' in names (True)
'Farkle' in names (False)

Or a one liner (If it's only one check)

或一个班轮(如果它只是一个检查)

'Bernard' in [i['name'] for i in l] (True)