[Baypiggies] manipulating lists question
Monte Davidoff
davidoff56 at alluvialsw.com
Thu Dec 5 09:57:45 CET 2013
On 12/5/13 12:06 AM, 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]]
How about this:
from itertools import groupby
from operator import itemgetter
key = itemgetter(0, 2)
y = [[k[0], ', '.join(h[1] for h in g), k[1]]
for k, g in groupby(sorted(x, key=key), key)]
Monte
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/baypiggies/attachments/20131205/c87e3d4d/attachment.html>
More information about the Baypiggies
mailing list