[英]Match values from two separate datasets into a single dataset
I have a two files. 我有两个文件。
One is a json document and the other is a list of tuples. 一个是json文档,另一个是元组列表。
The list of tuples contains ID numbers for all of the pairs. 元组列表包含所有对的ID号。
In the json document, all of those ID numbers appear as the value inside of each dictionary row. 在json文档中,所有这些ID号都作为每个字典行内的值出现。 So these can be gotten at:
因此可以在以下位置获得这些:
id_list = []
for line in list_of_dicts:
user = line.get('user')
id = user.get('id')
id_list.append(id)
Also in the json document is a name that goes with each ID number. JSON文档中还包含每个ID号的名称。 These can be gotten at:
这些可以在以下位置获得:
name_list = []
for line in list_of_dicts:
user = line.get('user')
name = user.get('name')
name_list.append(name)
How can I put the names in these lists and match them as dictionaries with the ID numbers in the list of tuples? 如何将名称放在这些列表中,并将其与字典和ID编号匹配在元组列表中?
If both of them are equal sized list then use dict with zip ie 如果两个列表大小相等,则将dict与zip一起使用,即
dict(zip(id_list,name_list))
Example 例
x = [1,2,3]
y= [4,5,6]
dict(zip(x,y))
{1: 4, 2: 5, 3: 6}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.