简体   繁体   English

将来自两个单独数据集的值匹配到单个数据集中

[英]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.

相关问题 匹配两个独立数据集之间的某些值并使用 python 创建新列 - Match certain values between two separate datasets and create new columns using python 根据特定列将原始数据集分成两个数据集 - Separate original dataset into two datasets based on specific columns 匹配 python 中两列上的两个数据集(日期是这些值之一) - Match two datasets on two columns in python (date being one of these values) 绘制来自两个数据集的值以进行比较 - Plotting values from two datasets for comparison 从两个数据集中比较和提取值 - Compare and extract values from two datasets 如何在 Python 中使用两个单独的尺度将两个数据集聚类为一个热图? - How to cluster two datasets into a single heatmap using two separate scales in Python? 如果我匹配一个单独的键,我迭代这个数据集以从另一个键值对返回所有匹配值的最佳方法是什么? - What is the best way for me to iterate over this dataset to return all matching values from another key value pair if I match a separate key? 比较来自两个单独文件的正则表达式匹配项,并替换其中一个的值 - Compare a regex match from two separate files and replace with values from one of them 从与关键字匹配的 JSON 数据集中提取值 - Pulling Values from a JSON Dataset that Match a Keyword Pandas:根据两个数据集中的匹配列,用另一个数据集中的数据填充数据集中的列 - Pandas: Filling column in dataset with data from another dataset based on matching columns in the two datasets
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM