繁体   English   中英

在zip中添加其他信息

[英]Add extra information to the zip

from itertools import groupby
#input
l = [['Cautus  B.V.', 'plein 92', '1129008', '10', 'AVB', 'Geachte mevrouw Daa', 'Mevrouw C.P. Daa'] ,
['Cautus  B.V.', 'Wei 9-11', '1019123', '10', 'AVB', 'Geachte mevrouw Daa', 'Mevrouw C.P. Daa'] ,
['Cautus  B.V.', 'plein 92', '1129008', '10', 'BEDR', 'Geachte mevrouw Daa', 'Mevrouw C.P. Daa'] ,
['Cautus  B.V.', 'Wei 9-11', '1019123', '10', 'BEDR', 'Geachte mevrouw Daa', 'Mevrouw C.P. Daa'] ,
['De company', 'tiellaan 42', 'KD0022232', '13', 'AVB', 'Geachte heer Tigch', 'De heer I. Tigch'] ,
['De company', 'tiellaan 42', 'KD0022232', '13', 'DAS', 'Geachte heer Tigch', 'De heer I. Tigch'] ,
['Slever ', 'klopt 42', 'KD2220115', '17', 'AVB', 'Geachte heer Slever', 'De heer T. Slever']]
#script
l_clean = sorted(zip(zip(*l)[1], zip(*l)[4],))

l_final = [(k, zip(*v)[1]) for k,v in groupby(l_clean, key = lambda x:x[0])]

for k,v in l_final:

     print k,list(v)

#My output is:

Wei 9-11 ['AVB', 'BEDR']

klopt 42 ['AVB']

plein 92 ['AVB', 'BEDR']

tiellaan 42 ['AVB', 'DAS']

问题

我的问题是我似乎无法将其他数据添加到输出中,我也想在输出中包含的数据是:

['Cautus  B.V.','1019123', '10', 'Geachte mevrouw Daa', 'Mevrouw C.P. Daa'](not only for this entry but for all entry's)

我尝试了一切更改/添加密钥等操作,但似乎不起作用。 我知道我的英语很模糊,所以如果我需要澄清一些单词或其他内容,只需说一下。 已经提前。

我很难理解您要做什么,但这也许会有所帮助:

from itertools import groupby
import operator
#input
l = [['Cautus  B.V.', 'plein 92', '1129008', '10', 'AVB', 'Geachte mevrouw Daa', 'Mevrouw C.P. Daa'] ,
['Cautus  B.V.', 'Wei 9-11', '1019123', '10', 'AVB', 'Geachte mevrouw Daa', 'Mevrouw C.P. Daa'] ,
['Cautus  B.V.', 'plein 92', '1129008', '10', 'BEDR', 'Geachte mevrouw Daa', 'Mevrouw C.P. Daa'] ,
['Cautus  B.V.', 'Wei 9-11', '1019123', '10', 'BEDR', 'Geachte mevrouw Daa', 'Mevrouw C.P. Daa'] ,
['De company', 'tiellaan 42', 'KD0022232', '13', 'AVB', 'Geachte heer Tigch', 'De heer I. Tigch'] ,
['De company', 'tiellaan 42', 'KD0022232', '13', 'DAS', 'Geachte heer Tigch', 'De heer I. Tigch'] ,
['Slever ', 'klopt 42', 'KD2220115', '17', 'AVB', 'Geachte heer Slever', 'De heer T. Slever']]
#script
sortkey = operator.itemgetter(1,4)
l_clean = sorted(l,key=sortkey)

l_final = [(k, list(v)) for k,v in groupby(l_clean, key = operator.itemgetter(1))]

for k,v in l_final:
   info_rest = v[0][:4]+v[0][5:]
   info_combine = map(operator.itemgetter(4),v) 
   print k,info_combine,info_rest

基本上,我是根据特定键对所有数据进行排序的。 这样,您就不会在排序阶段丢失任何数据。 然后,我将groupby的键更改为与新的数据布局一致,并打印了结果。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM