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']
Problem
My problem is that i can’t seem to add the other data to the output the data i also want to have in the output is:
['Cautus B.V.','1019123', '10', 'Geachte mevrouw Daa', 'Mevrouw C.P. Daa'](not only for this entry but for all entry's)
I tryed everything changing/adding the key etc but it doesn’t seem to work.
I know my english is vague so if i need to clarify some words or anything just say so.
Already thx in advance.
I’m having a difficult time understanding what you’re trying to do, but perhaps this will help:
Basically, I sorted all of the data based on a specific key. This way you don’t lose any data during the sort stage. I then changed the key in
groupbyto be consistent with the new data layout and printed the results.