Python list comprehension for dictionaries in dictionaries?

后端 未结 5 2132
借酒劲吻你
借酒劲吻你 2021-01-03 19:46

I just learned about list comprehension, which is a great fast way to get data in a single line of code. But something\'s bugging me.

In my test I have this kind of

5条回答
  •  北荒
    北荒 (楼主)
    2021-01-03 20:38

    You can do this:

    s = dict([ (k,r) for k,r in mydict.iteritems() if r['x'] > 92 and r['x'] < 95 and r['y'] > 70 and r['y'] < 75 ])
    

    This takes a dict as you specified and returns a 'filtered' dict.

提交回复
热议问题