[Baypiggies] manipulating lists question

Michael Moore michaelglennmoore at gmail.com
Thu Dec 5 10:29:01 CET 2013


Actually there is a hidden flaw in that code...You need to start with the
proper x, not with the one processed by an earlier failed attempt and the
remove/del haas side effects, better to use slices.

>>> x = [['cat','NM123',12],['cat','NM234',12], ['dog', 'NM56',65]]
>>> y = []
>>> while len(x):
if len(x) == 1:
y.append(x[0])
x=[]
continue
res, matching = squish(x[0], x[1])
if matching:
y.append(res)
x = x[2:]
continue
else:
y.append(x[0])
x = x[1:]
continue

>>> y
[['cat', 'NM123', 'NM234', 12], ['dog', 'NM56', 65]]
>>>




On Thu, Dec 5, 2013 at 12:07 AM, Michael Moore
<michaelglennmoore at gmail.com>wrote:

> 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/f25f409e/attachment.html>


More information about the Baypiggies mailing list