[BangPypers] list question

Dhananjay Nene dhananjay.nene at gmail.com
Thu Dec 5 09:51:15 CET 2013


from itertools import groupby
x = [['cat','NM123',12],['cat','NM234',12], ['dog', 'NM56',65]]
y = [['cat','NM123, NM234', 12], ['dog', 'NM56', 65]]

def grouper(t) : return (t[0],t[2])
assert y ==  list(list((keys[0],", ".join(val[1] for val in vals),keys[1]))
                  for keys, vals in  groupby(sorted(x,key=grouper),grouper))


----------------------------------------------------------------------------------------------------------------------------------------
http://blog.dhananjaynene.com twitter: @dnene google+:
https://google.com/+DhananjayNene


On Thu, Dec 5, 2013 at 2:16 PM, L Radhakrishna Rao
<satishsagar83 at gmail.com>wrote:

> One thing is here:
>
> The x[0] and x[1] are in union with each other.
>
> this needs to be solved by set operations on list of lists.
>
>
>
>
> On Thu, Dec 5, 2013 at 1:55 PM, Vikram K <vikthirtyfive at gmail.com> 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]]
> > >>> x
> > [['cat', 'NM123', 12], ['cat', 'NM234', 12], ['dog', 'NM56', 65]]
> > >>> y = []
> > >>> y = [['cat','NM123, NM234', 12], ['dog', 'NM56', 65]]
> > >>>
> >
> > Regards
> > Vik
> > _______________________________________________
> > BangPypers mailing list
> > BangPypers at python.org
> > https://mail.python.org/mailman/listinfo/bangpypers
> >
> _______________________________________________
> BangPypers mailing list
> BangPypers at python.org
> https://mail.python.org/mailman/listinfo/bangpypers
>


More information about the BangPypers mailing list