0
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)

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

4

1 回答 1

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

于 2012-10-17T14:10:18.550 回答