[Biopython] not technically a biopython question

John Ladasky blind.watchmaker at yahoo.com
Mon Oct 8 19:59:04 UTC 2012


Date: Mon, 8 Oct 2012 11:29:37 -0400 From: George Devaniranjan 
<devaniranjan at gmail.com> Subject: [Biopython] not technically a 
biopython question To: Biopython Mailing List 
<biopython at lists.open-bio.org> Message-ID: 
<CAFU65PcN8zvGEECm=zF2OHDRJ2fY3Xvq8NRoANHWHnfm5fzv3w at mail.gmail.com>

> I get the following as the answer for one nCatSeq,
> '4241': ((('VUVV', 'DDRV'), 'DDVG'), 'VUVV')
>
>
> what I want however is :
>
> '4241': ('VUVV', 'DDRV', 'DDVG', 'VUVV')
>

What you want to do is to "flatten" a nested sequence object.  There are 
many Python recipes to do this.  Most of the solutions involve recursive 
function calls.  Here's a page that discusses several ways to get it done:

http://stackoverflow.com/questions/2158395/flatten-an-irregular-list-of-lists-in-python

If your list or tuple is VERY deeply nested (say, 256 parentheses), you 
may hit Python's recursion limit.  There are solutions on that page 
which don't require recursion, but they are frequently more difficult to 
understand.  I tend to prefer code that I can read at a glance, myself.

I don't know why flattening lists and tuples isn't a standard library 
function in Python yet, it seems like everyone needs to do this at some 
time or another.




More information about the Biopython mailing list