【发布时间】:2017-07-10 13:21:04
【问题描述】:
我有 2 个字典列表。
list1 = [{'user_id':23, 'user_name':'John', 'age':30},
{'user_id':24, 'user_name':'Shaun', 'age':31},
{'user_id':25, 'user_name':'Johny', 'age':32}]
list2 =[{'user_id':23},
{'user_id':25}]
现在我想要输出
list3 = [{'user_id':23, 'user_name':'John', 'age':30},
{'user_id':25, 'user_name':'Johny','age':32}]
我想要最有效的方法,因为我的list1 可能包含数百万行。
【问题讨论】:
-
您是否尝试了一些不够快的方法?
-
如果你只需要对
list1进行一次扫描,那么你应该使用Jean-François Fabre的策略。但是,如果您需要多次搜索,那么您应该认真考虑将列表转换为字典,按照 omri_saadon 的回答。如果您使用元组或命名元组,而不是为这个新字典的内部项目使用字典,它会节省 RAM。
标签: python