[Baypiggies] manipulating lists question
Filip Machi
fil at macfellow.com
Thu Dec 5 10:12:53 CET 2013
[I'm not sure I can post to BayPiggies using this email address.]
This is a start, y1 and y2 the two forms noted by Michael:
#!/usr/bin/env python2.7
from collections import OrderedDict
x = [['cat','NM123',12],['cat','NM234',12], ['dog', 'NM56',65]]
z = OrderedDict([(e[0], (set(), set())) for e in x])
for e in x:
z[e[0]][0].update([e[1]])
z[e[0]][1].update([e[2]])
y1 = [[e] + list(z[e][0]) + list(z[e][1]) for e in z]
print y1
y2 = [[e] + [', '.join(list(z[e][0]))] + list(z[e][1]) for e in z]
print y2
Output:
[['cat', 'NM234', 'NM123', 12], ['dog', 'NM56', 65]]
[['cat', 'NM234, NM123', 12], ['dog', 'NM56', 65]]
-Fil
On Dec 5, 2013, at 00:53 , Michiel Overtoom wrote:
>
> On Dec 5, 2013, at 09:06, Vikram K wrote:
>
>> Any suggestions on what i have to do to go from x to y?
>>
>>>>> x = [['cat','NM123',12],['cat','NM234',12], ['dog', 'NM56',65]]
>>>>> y = [['cat','NM123, NM234', 12], ['dog', 'NM56', 65]]
>
> Are you sure you mean
>
> [['cat','NM123, NM234', 12], ['dog', 'NM56', 65]]
>
> and not
>
> [['cat','NM123', 'NM234', 12], ['dog', 'NM56', 65]]
>
> ?
>
>
>>>>>
>>
>> Regards
>> Vik
>> _______________________________________________
>> Baypiggies mailing list
>> Baypiggies at python.org
>> To change your subscription options or unsubscribe:
>> https://mail.python.org/mailman/listinfo/baypiggies
>
>
> --
> Veel spullen zijn tegenwoordig waardeloos, maar toch hecht men er
> veel waarde aan. Mensen hebben vaak teveel spullen. Hoe komt dat en
> wat doe je ertegen? - een essay van Paul Graham, op http://www.michielovertoom.com/articles/paul-graham-spullen
>
> "Een prima telefoon met draaischijf weggooien? Die zou ik nog wel
> eens nodig kunnen hebben!"
>
>
> _______________________________________________
> Baypiggies mailing list
> Baypiggies at python.org
> To change your subscription options or unsubscribe:
> https://mail.python.org/mailman/listinfo/baypiggies
More information about the Baypiggies
mailing list