[英]a list of several tuples, how to extract the same of the first two elements in the small tuple in the large tuple
[英]How to merge two tuples in a list if the first tuple elements match?
我有两个以下形式的元组列表:
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”中元组的第一个元素匹配,则将匹配的元组合并在一起以产生一个新列表元组? 形式:
merged_data = [(ansonca01,4,1871,1,RC1, Anson,Cap,05/06/1871),(...),(...), etc.]
新的元组列表将使ID号与正确播放器的名字和姓氏匹配。
背景信息:我正在尝试合并两个棒球统计数据的CSV文件,但是其中一个包含所有相关统计数据的文件不包含球员姓名,仅包含参考编号(例如“ ansoc101”),而第二个文档中包含参考编号一个列,另一列中相应玩家的名字和姓氏。
CSV的大小太大,无法手动执行(大约20,000个播放器),因此我正在尝试使该过程自动化。
使用列表理解来遍历您的列表:
[x + y[1:] for x in list1 for y in list2 if x[0] == y[0]]
我在列表上尝试过:
list1 = [("this", 1, 2, 3), ("that", 1, 2, 3), ("other", 1, 2, 3)]
list2 = [("this", 5, 6, 7), ("that", 10, 11, 12), ("notother", 1, 2, 3)]
并得到:
[('this', 1, 2, 3, 5, 6, 7), ('that', 1, 2, 3, 10, 11, 12)]
那是你想要的吗?
您可以首先创建一个字典来启用快速ID号码查找,然后通过列表理解非常有效地将两个列表中的数据合并在一起:
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')]
字典
playerinfo
列表并创建字典,其中key是元组中的第一项,value是所有项的列表。 idmatch
列表,并检查结果字典idmatch
组的第一项与否。 如果存在,则通过列表扩展方法用新值扩展键的值。 演示:
import pprint
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")]
result = {}
for i in playerinfo:
result[i[0]] = list(i[:])
print "Debug Rsult1:"
pprint.pprint(result)
for i in idmatch:
if i[0] in result:
result[i[0]].extend(list(i[1:]))
print "\nDebug Rsult2:"
pprint.pprint(result)
final_rs = []
for i,j in result.items():
final_rs.append(tuple(j))
print "\nFinal result:"
pprint.pprint(final_rs)
输出:
infogrid@infogrid-vivek:~/workspace/vtestproject$ python task4.py
Debug Rsult1:
{'ansonca01': ['ansonca01', 4, 1871, 1, 'RC1'],
'forceda01': ['forceda01', 44, 1871, 1, 'WS3'],
'mathebo01': ['mathebo01', 68, 1871, 1, 'FW1']}
Debug Rsult2:
{'ansonca01': ['ansonca01', 4, 1871, 1, 'RC1', 'Anson', 'Cap', '05/06/1871'],
'forceda01': ['forceda01', 44, 1871, 1, 'WS3'],
'mathebo01': ['mathebo01', 68, 1871, 1, 'FW1']}
Final result:
[('ansonca01', 4, 1871, 1, 'RC1', 'Anson', 'Cap', '05/06/1871'),
('forceda01', 44, 1871, 1, 'WS3'),
('mathebo01', 68, 1871, 1, 'FW1')]
infogrid@infogrid-vivek:~/workspace/vtestproject$
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.