[Baypiggies] Fwd: nested list help

Robert Zuber rob at zuber.net
Tue Jul 27 18:50:22 CEST 2010


Glen,

I too find myself wishing that list methods returned a reference to the list so that I could chain those calls or use them in list comprehensions.  Look forward to anyone's input on why they don't.

As far as simplifying, I'm not sure what the difference in intermediary objects is, but here's where I started:

>>> from collections import defaultdict
>>> d = defaultdict(list)
>>> for key, values in ((i[0], i[1:]) for i in x):
...     d[key].extend(values)
... 
>>> z = [[key] + values for key, values in d.iteritems()]
>>> z
[['NM100', 3, 4, 5, 6, 7, 10, 11, 12, 13], ['NM200', 15, 16, 17]]


With a regular dict, you could use setdefault() so you don't have to append the key later:

>>> d = {}
>>> for key, values in ((i[0], i[1:]) for i in x):
...     d.setdefault(key, [key]).extend(values)
... 
>>> z = d.values()
>>> z
[['NM100', 3, 4, 5, 6, 7, 10, 11, 12, 13], ['NM200', 15, 16, 17]]


But then you get extraneous object creation.  So, my best guess to get the benefits of both:


>>> from collections import defaultdict
>>> x = [['NM100', 3, 4, 5, 6, 7], ['NM100', 10, 11, 12, 13], ['NM200', 15, 16, 17]]
>>> class KeyDefaultDict(defaultdict):
...     def __missing__(self, key):
...         self[key] = [key]
...         return self[key]
... 
>>> d = KeyDefaultDict()
>>> for key, values in ((i[0], i[1:]) for i in x):
...     d[key].extend(values)
... 
>>> z = d.values()
>>> z
[['NM100', 3, 4, 5, 6, 7, 10, 11, 12, 13], ['NM200', 15, 16, 17]]


Vikram -- I don't really understand your problem space, but given that you seem to be using a set of lists with keys as the first entries, I'm wondering if you would find your coding easier with dictionaries as the base.  You would also likely save a lot of intermediary object creation.


Cheers,
Rob.


On Jul 27, 2010, at 9:03 AM, Glen Jarvis wrote:

> Vikram,
>     From my experience with your previous posts, you're looking for an elegant and nice solution -- not just an "answer" (and I commend that!)  What I have below is an answer, not an elegant solution.
> 
> Everyone else,
>    As a learning experience, I went through Julian's Snitow's suggestion on default dicts. What I have works -- however, what is "ugly" to me is trying to [].insert without getting a None... I have an ugly secondary outer list comprehension to get around that. It feels too hacky to me.
> 
> I'd be very curious how someone fromt his group makes this better. I'll probably slap myself on the forehead when I see the better solution.
> 
> 
> from collections import defaultdict
> 
> x=[['NM100', 3, 4, 5, 6, 7],
>    ['NM100', 10, 11, 12, 13],
>    ['NM200', 15, 16, 17]]
> 
> d=defaultdict(list)
> for item in x:
>     d[item[0]].extend(item[1:])
> 
> z = [ a for a, b in
>         [(y, y.insert(0, x))
>             for x, y in d.items() ]]
> 
> for i in z:
>     print i
> 
> lappy_adaptor> python vikram.py 
> ['NM100', 3, 4, 5, 6, 7, 10, 11, 12, 13]
> ['NM200', 15, 16, 17]
> 
> 
> 
> On Tue, Jul 27, 2010 at 7:19 AM, Vikram K <kpguy1975 at gmail.com> wrote:
> 
> 
> ---------- Forwarded message ----------
> From: Vikram K <kpguy1975 at gmail.com>
> Date: Tue, Jul 27, 2010 at 7:48 PM
> Subject: Re: [Baypiggies] nested list help
> To: Glen Jarvis <glen at glenjarvis.com>
> 
> 
> Hi Glen,
>  the elements of the original list will never contain a duplicate element. The numbers other than the NMID are actually representing exon coordinates and the joining is being done to represent the fact that exons (or rather exon coordinates) corresponding to the same NMID are joined together to represent splicing during transcription from DNA to mRNA.
> 
> 
> On Tue, Jul 27, 2010 at 7:31 PM, Glen Jarvis <glen at glenjarvis.com> wrote:
> To help define the problem, what behavior do you expect if the second element of your original list also contains a duplicate element. In this example, what result are you looking for if your second element was ['NM100', 7, 11, 12, 13]
> 
> Glen
> 
> 
> 
> > Suppose i have this nested list:
> >
> > >>> x
> > [['NM100', 3, 4, 5, 6, 7], ['NM100', 10, 11, 12, 13], ['NM200', 15, 16, 17]]
> > >>> for i in x:
> > ...   print i
> > ...
> > ['NM100', 3, 4, 5, 6, 7]
> > ['NM100', 10, 11, 12, 13]
> > ['NM200', 15, 16, 17]
> > >>>
> >
> > how do i obtain from the above the following nested list:
> >
> > >>> z
> > [['NM100', 3, 4, 5, 6, 7, 10, 11, 12, 13], ['NM200', 15, 16, 17]]
> > >>> for i in z:
> > ...   print i
> > ...
> > ['NM100', 3, 4, 5, 6, 7, 10, 11, 12, 13]
> > ['NM200', 15, 16, 17]
> > >>>
> >
> > _______________________________________________
> > Baypiggies mailing list
> > Baypiggies at python.org
> > To change your subscription options or unsubscribe:
> > http://mail.python.org/mailman/listinfo/baypiggies
> 
> 
> 
> _______________________________________________
> Baypiggies mailing list
> Baypiggies at python.org
> To change your subscription options or unsubscribe:
> http://mail.python.org/mailman/listinfo/baypiggies
> 
> 
> 
> -- 
> Whatever you can do or imagine, begin it;
> boldness has beauty, magic, and power in it.
> 
> -- Goethe 
> _______________________________________________
> Baypiggies mailing list
> Baypiggies at python.org
> To change your subscription options or unsubscribe:
> http://mail.python.org/mailman/listinfo/baypiggies

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/baypiggies/attachments/20100727/4308dc05/attachment-0001.html>


More information about the Baypiggies mailing list