在zip中添加额外信息

时间:2012-10-17 13:53:39

标签: python

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)

我尝试了所有更改/添加密钥等但它似乎不起作用。 我知道我的英语很模糊,所以如果我需要澄清一些话或任何事情就这么说。 已经提前了。

1 个答案:

答案 0 :(得分:1)

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

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中的密钥以与新数据布局保持一致并打印结果。