本文介绍了如果第一个元组元素匹配,如何合并列表中的两个元组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个元组列表,形式为:

I've got two lists of tuples of the form:

playerinfo = [(ansonca01,4,1871,1,RC1),(forceda01,44,1871,1,WS3),(mathebo01,68,1871,1,FW1)]

idmatch = [(ansonca01,Anson,Cap,05/06/1871),(aaroh101,Aaron,Hank,04/13/1954),(aarot101,Aaron,Tommie,04/10/1962)]

我想知道的是,我如何遍历两个列表,并且如果 playerinfo中元组的第一个元素与 idmatch中元组的第一个元素匹配,合并匹配的元组在一起以产生新的元组列表?格式为:

What I would like to know, is how could I iterate through both lists, and if the first element in a tuple from "playerinfo" matches the first element in a tuple from "idmatch", merge the matching tuples together to yield a new list of tuples? In the form:

merged_data = [(ansonca01,4,1871,1,RC1, Anson,Cap,05/06/1871),(...),(...), etc.]

新列表元组中的ID号必须与正确的运动员的名字和姓氏匹配。

The new list of tuples would have the ID number matched to the first and last names of the correct player.

背景信息:我正在尝试合并棒球统计数据的两个CSV文件,但是具有所有相关统计数据的人不包含球员姓名,而仅包含参考号码,例如 ansoc101,而第二个文档的一列中包含参考号,另一列中包含相应玩家的名字和姓氏。

Background info: I'm trying to merge two CSV documents of baseball statistics, but the one with all of the relevant stats doesn't contain player names, only a reference number e.g. 'ansoc101', while the second document contains the reference number in one column and the first and last names of the corresponding player in the other.

CSV的大小为太大而无法手动执行此操作(大约20,000个玩家),因此我正在尝试使该过程自动化。

The size of the CSV is too large to do this manually (about 20,000 players), so I'm trying to automate the process.

推荐答案

创建一个字典以启用快速ID号码查找,然后通过列表理解非常有效地将两个列表中的数据合并在一起:

You could first create a dictionary to enable fast ID number look-ups, and then merge the data from the two lists together very efficiently with a list comprehension:

import operator

playerinfo = [('ansonca01', 4, 1871, 1, 'RC1'),
              ('forceda01', 44, 1871, 1, 'WS3'),
              ('mathebo01', 68, 1871, 1, 'FW1')]

idmatch = [('ansonca01', 'Anson', 'Cap', '05/06/1871'),
           ('aaroh101', 'Aaron', 'Hank', '04/13/1954'),
           ('aarot101', 'Aaron', 'Tommie', '04/10/1962')]

id = operator.itemgetter(0)  # To get id field.

idinfo = {id(rec): rec[1:] for rec in idmatch}  # Dict for fast look-ups.

merged = [info + idinfo[id(info)] for info in playerinfo if id(info) in idinfo]

print(merged) # -> [('ansonca01', 4, 1871, 1, 'RC1', 'Anson', 'Cap', '05/06/1871')]

这篇关于如果第一个元组元素匹配,如何合并列表中的两个元组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-18 10:24
查看更多