【问题标题】:filter keys out from list of dicts从字典列表中过滤键
【发布时间】:2022-12-02 17:17:25
【问题描述】:

Say I have a list of dict:

ld = [{\'a\':1,\'b\':2,\'c\':9},{\'a\':1,\'b\':2,\'c\':10}]

And a list to filter the keys out:

l = [\'a\',\'c\']

Want to remove key a and c from ld:

Try:

result = [d for d in ld for k in d if k in l]

Desired Result:

[{\'b\':2},{\'b\':2}]
  • why the downvote ? the question is clear.

标签: python


【解决方案1】:
【解决方案2】:
【解决方案3】:
猜你喜欢
  • 2020-04-08
  • 1970-01-01
  • 1970-01-01
  • 2018-05-01
  • 1970-01-01
  • 1970-01-01
  • 2015-05-17
  • 2020-09-11
  • 2020-02-06
相关资源
最近更新 更多