我正在尝试获取匹配的ID并将数据存储到一个列表中。我有一个词典列表:
list = [
{'id':'123','name':'Jason','location': 'McHale'},
{'id':'432','name':'Tom','location': 'Sydney'},
{'id':'123','name':'Jason','location':'Tompson Hall'}
]
预期输出类似于
# {'id':'123','name':'Jason','location': ['McHale', 'Tompson Hall']},
# {'id':'432','name':'Tom','location': 'Sydney'},
如何根据dict ID值获取匹配数据?我试过了:
for item in mylist:
list2 = []
row = any(list['id'] == list.id for id in list)
list2.append(row)
这不起作用(抛出:TypeError: tuple indices must be integers or slices, not str
)。如何获得具有相同ID的所有项目并存储到一个词典中?
答案 0 :(得分:1)
鉴于此输入
mylist = [
{'id':'123','name':'Jason','location': 'McHale'},
{'id':'432','name':'Tom','location': 'Sydney'},
{'id':'123','name':'Jason','location':'Tompson Hall'}
]
你可以用理解来提取它
matched = [d for d in mylist if d['id'] == '123']
然后你想要合并这些位置。假设matched
不为空
final = matched[0]
final['location'] = [d['location'] for d in matched]
这是解释器
In [1]: mylist = [
...: {'id':'123','name':'Jason','location': 'McHale'},
...: {'id':'432','name':'Tom','location': 'Sydney'},
...: {'id':'123','name':'Jason','location':'Tompson Hall'}
...: ]
In [2]: matched = [d for d in mylist if d['id'] == '123']
In [3]: final=matched[0]
In [4]: final['location'] = [d['location'] for d in matched]
In [5]: final
Out[5]: {'id': '123', 'location': ['McHale', 'Tompson Hall'], 'name': 'Jason'}
显然,您希望将'123'
替换为包含所需id
值的变量。
将其全部包含在函数中:
def merge_all(df):
ids = {d['id'] for d in df}
result = []
for id in ids:
matches = [d for d in df if d['id'] == id]
combined = matches[0]
combined['location'] = [d['location'] for d in matches]
result.append(combined)
return result
另外,请不要将list
用作变量名。它会隐藏内置list
类。
答案 1 :(得分:1)
首先,您在for循环中遍历词典列表,但从不引用您在item
中存储的词典。我想当你写list[id]
时,你的意思是item[id]
。
其次,any()
返回一个布尔值(true或false),这不是你想要的。相反,也许尝试row = [dic for dic in list if dic['id'] == item['id']]
第三,如果你在for循环中定义了list2,它将在每次迭代时消失。在for循环之前移动list2 = []
。
这应该会给你一个良好的开端。请记住,row
只是具有相同ID的所有词典的列表。
答案 2 :(得分:1)
在将我期望成为列表的字典条目转换为列表之后,我会使用kdopen的方法和合并方法。当然,如果你想避免冗余,那就制作它们。
mylist = [
{'id':'123','name':['Jason'],'location': ['McHale']},
{'id':'432','name':['Tom'],'location': ['Sydney']},
{'id':'123','name':['Jason'],'location':['Tompson Hall']}
]
def merge(mylist,ID):
matches = [d for d in mylist if d['id']== ID]
shell = {'id':ID,'name':[],'location':[]}
for m in matches:
shell['name']+=m['name']
shell['location']+=m['location']
mylist.remove(m)
mylist.append(shell)
return mylist
updated_list = merge(mylist,'123')