假设我有两个字典列表:
Dict1=[
{"name": "James","age": "21"},
{"name": "Evelyn","age": "28"},
{"name": "William","age": "31"}
]
Dict2=[
{"name": "James","age": "21","city":"NYC","Gender":"M"},
{"name": "William","age": "25","city":"NYC","Gender":"M"},
{"name": "Ella","age": "17","city":"NYC","Gender":"F"}
]
我如何才能仅根据名称关键字对它们进行比较,并从dict2中获得整个条目,在这种情况下,结果必须是:
new_dict=[
{"name": "James","age": "21","city":"NYC","Gender":"M"},
{"name": "William","age": "25","city":"NYC","Gender":"F"},
]
我正在寻找一种基于一个或多个选定的键进行比较的方法,而不使用for循环。
如果不循环,您想做的事情是不可能的。您最终可以通过使用库或复杂的方式隐藏循环,但最简单的方法是循环。
现在有坏的循环方式和好的循环方式。坏的方法是针对Dict2的每个元素检查Dict1的每个元素(O(n*m(复杂性(。
一个好的方法是构造Dict1中名称的set
索引,并使用它们来匹配Dict2:的名称
names = set(d['name'] for d in Dict1)
new_dict = [d for d in Dict2 if d['name'] in names]
输出:
>>> new_dict
[{'name': 'James', 'age': '21', 'city': 'NYC', 'Gender': 'M'},
{'name': 'William', 'age': '25', 'city': 'NYC', 'Gender': 'M'}]
- 为了清楚起见,您将列表命名为"Dicts";。这在未来可能会变得令人困惑
- 假设名称是唯一的,您可以使用这样的词典:
d1 = {person['name']: person for person in Dict1}
d2 = {person['name']: person for person in Dict2}
result = [person for name, person in d2.items() if name in d1]
# result is:
[
{'name': 'James', 'age': '21', 'city': 'NYC', 'Gender': 'M'},
{'name': 'William', 'age': '25', 'city': 'NYC', 'Gender': 'M'}
]
还有其他方法可以实现这一点,有些甚至更高效,但如果您需要可读性,这可能会有所帮助。
Edit:这显然使用了循环,但如果不迭代集合,就无法完成您的请求。所以我假设您只是想避免for
块。
有点复杂,但您可以使用panda来避免使用循环:
import pandas as pd
Dict1=[
{"name": "James","age": "21"},
{"name": "Evelyn","age": "28"},
{"name": "William","age": "31"}
]
Dict2=[
{"name": "James","age": "21","city":"NYC","Gender":"M"},
{"name": "William","age": "25","city":"NYC","Gender":"M"},
{"name": "Ella","age": "17","city":"NYC","Gender":"F"}
]
df1 = pd.DataFrame(Dict1)
df2 = pd.DataFrame(Dict2)
df = df2[df2['name'].isin(df1['name'])]
print(df.to_dict(orient="records"))
输出:
[{'name': 'James', 'age': '21', 'city': 'NYC', 'Gender': 'M'}, {'name': 'William', 'age': '25', 'city': 'NYC', 'Gender': 'M'}]