简体   繁体   中英

make tuple pairs from list's element Python

It is very simple problem but I can not think of how to do it from last night. Say I have a list like:

L = ['AAG', 'AGA', 'GAT', 'ATT', 'TTC', 'TCT', 'CTC', 'TCT', 'CTA', 
     'TAA', 'AAG', 'AGA']

I have 12 elements in this list so I can make element like first element with second element would be first element and second element eith third element would be the second pair,and this follows the rest elements. Here is how it looks after making pairs:

L = [('AAG', 'AGA'),('AGA', 'GAT'),('GAT', 'ATT'),('ATT', 
      'TTC'),('TTC', 'TCT'),('TCT', 'CTC'),('CTC', 'TCT'),
     ( 'TCT', 'CTA'),('CTA', 'TAA'),('TAA', 'AAG'),('AAG', 'AGA')]

Now I want to take first element from each pair and check if this element exists in other element as first element in pair; if it does then I will print like: AAG -> AGA,AGA. The 'AAG' appears in first pair and last pair as first element. SO the whole output will be like:

> AAG -> AGA,AGA

> AGA -> GAT

> ATT -> TTC

> CTA -> TAA

> CTC -> TCT

> GAT -> ATT

> TAA -> AAG

> TCT -> CTA,CTC

> TTC -> TCT

How do i it?

First part:

>>> L = ['AAG', 'AGA', 'GAT', 'ATT', 'TTC', 'TCT', 'CTC', 'TCT', 'CTA', 'TAA', 'AAG', 'AGA']
>>> zip(L,L[1:])
[('AAG', 'AGA'), ('AGA', 'GAT'), ('GAT', 'ATT'), ('ATT', 'TTC'), ('TTC', 'TCT'), ('TCT', 'CTC'), ('CTC', 'TCT'), ('TCT', 'CTA'), ('CTA', 'TAA'), ('TAA', 'AAG'), ('AAG', 'AGA')]

The second part is:

>>> from itertools import groupby
>>> LoT=zip(L, L[1:])
>>> for k, g in groupby(sorted(LoT), lambda t: t[0]):
...    print k, "->",",".join([e[1] for e in g])
... 
AAG -> AGA,AGA
AGA -> GAT
ATT -> TTC
CTA -> TAA
CTC -> TCT
GAT -> ATT
TAA -> AAG
TCT -> CTA,CTC
TTC -> TCT

It's a pretty simple defaultdict. Every three letter string except the last one can be thought of as the start of one or more pairs. For each index i, just append the string at i+1 to the list associated with with element i as the starting string.

from collections import defaultdict
L = ['AAG', 'AGA', 'GAT', 'ATT', 'TTC', 'TCT', 'CTC', 'TCT', 'CTA', 'TAA', 'AAG', 'AGA']

my_map = defaultdict(list)
for i in range(len(L)-1):
    my_map[L[i]] += [L[i+1]]

for start, end in my_map.iteritems():
    print start, end
>>> L = ['AAG', 'AGA', 'GAT', 'ATT', 'TTC', 'TCT', 'CTC', 'TCT', 'CTA', 'TAA', 'AAG', 'AGA']
>>> L2 = zip(L, L[1:])
>>> from collections import defaultdict
>>> D = defaultdict(list)
>>> for i, j in L2:
...     D[i].append(j)
... 
>>> for k in sorted(D):
...     print(k, "->", ",".join(D[k]))
... 
AAG -> AGA,AGA
AGA -> GAT
ATT -> TTC
CTA -> TAA
CTC -> TCT
GAT -> ATT
TAA -> AAG
TCT -> CTC,CTA
TTC -> TCT

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM