我有两个字典列表。
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。 - PM 2Ring