[BangPypers] list question

Nitin Kumar nitin.nitp at gmail.com
Thu Dec 5 10:30:44 CET 2013


long but this too works :)

>>> p=[]
>>> x=[['cat','NM123',12],['cat','NM234',12], ['dog', 'NM56',65]]
>>> import copy
>>> def fn(a,b):
if len(a)==3 and a[0]==b[0] and a[-1]==b[-1] and a[1]!=b[1]:
a.insert(-1,b[1])
p.append(a)
else:
p.append(b)
return p

>>> reduce(fn, copy.deepcopy(x))
[['cat', 'NM123', 'NM234', 12], ['dog', 'NM56', 65]]
>>> p
[['cat', 'NM123', 'NM234', 12], ['dog', 'NM56', 65]]
>>>
>>>
>>> x
[['cat', 'NM123', 12], ['cat', 'NM234', 12], ['dog', 'NM56', 65]]
>>> 'p is your y'

Nitin K


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
>


More information about the BangPypers mailing list