[Baypiggies] manipulating lists question
Michael Moore
michaelglennmoore at gmail.com
Thu Dec 5 10:07:35 CET 2013
There are many ways. Here is ONE of them
>>> def squish(a_list, b_list):
res = []
matching = False
for idx, i in enumerate(a_list):
if i == b_list[idx]:
matching = True
res.append(i)
continue
if matching: # note this does not happen unless dissimilar
res.append(i)
res.append(b_list[idx])
else:
res.append(i)
return res, matching
>>> while len(x):
res, matching = squish(x[0], x[1])
if matching:
y.append(res)
del x[0]
del x[1]
else:
y.append(x[0])
del x[0]
if len(x) == 1:
y.append(x[0])
x = []
>>> y
[['cat', 'NM123', 'NM234', 12], ['cat', 'NM234', 12]]
The problem here is ... Definition is fuzzy. Should that happen for every
match even if lists with matching multiples even if they are not adjacent
(leaving that as an exercise since the problem was not well-defined).
Michael Moore
On Wed, Dec 4, 2013 at 11:06 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
>
> _______________________________________________
> Baypiggies mailing list
> Baypiggies at python.org
> To change your subscription options or unsubscribe:
> https://mail.python.org/mailman/listinfo/baypiggies
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/baypiggies/attachments/20131205/cb9228f0/attachment.html>
More information about the Baypiggies
mailing list