Python: get a dict from a list based on something inside the dict

匿名 (未验证) 提交于 2019-12-03 08:50:26

问题:

I need to be able to find an item in a list (an item in this case being a dict) based on some value inside that dict. The structure of the list I need to process looks like this:

[     {         'title': 'some value',         'value': 123.4,         'id': 'an id'     },     {         'title': 'another title',         'value': 567.8,         'id': 'another id'     },     {         'title': 'last title',         'value': 901.2,         'id': 'yet another id'     } ] 

Caveats: title and value can be any value (and the same), id would be unique.

I need to be able to get a dict from this list based on a unique id. I know this can be done through the use of loops, but this seems cumbersome, and I have a feeling that there's an obvious method of doing this that I'm not seeing thanks to brain melt.

回答1:

my_item = next((item for item in my_list if item['id'] == my_unique_id), None) 

This iterates through the list until it finds the first item matching my_unique_id, then stops. It doesn't store any intermediate lists in memory (by using a generator expression) or require an explicit loop. It sets my_item to None of no object is found. It's approximately the same as

for item in my_list:     if item['id'] == my_unique_id:         my_item = item         break else:     my_item = None 

else clauses on for loops are used when the loop is not ended by a break statement.



回答2:

If you have to do this multiple times, you should recreate a dictionnary indexed by id with your list :

keys = [item['id'] for item in initial_list] new_dict = dict(zip(keys, initial_list))   >>>{     'yet another id': {'id': 'yet another id', 'value': 901.20000000000005, 'title': 'last title'},      'an id': {'id': 'an id', 'value': 123.40000000000001, 'title': 'some value'},      'another id': {'id': 'another id', 'value': 567.79999999999995, 'title': 'another title'} } 

or in a one-liner way as suggested by agf :

new_dict = dict((item['id'], item) for item in initial_list) 


回答3:

In [2]: test_list Out[2]:  [{'id': 'an id', 'title': 'some value', 'value': 123.40000000000001},  {'id': 'another id', 'title': 'another title', 'value': 567.79999999999995},  {'id': 'yet another id', 'title': 'last title', 'value': 901.20000000000005}]  In [3]: [d for d in test_list if d["id"] == "an id"] Out[3]: [{'id': 'an id', 'title': 'some value', 'value': 123.40000000000001}] 

Use list comprehension



回答4:

You can create a simple function for this purpose:

lVals = [{'title': 'some value', 'value': 123.4,'id': 'an id'},  {'title': 'another title', 'value': 567.8,'id': 'another id'},  {'title': 'last title', 'value': 901.2, 'id': 'yet another id'}]  def get_by_id(vals, expId): return next(x for x in vals if x['id'] == expId)  get_by_id(lVals, 'an id') >>> {'value': 123.4, 'title': 'some value', 'id': 'an id'} 


易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!