如何使用字母数字字符从单个列表中创建元组? [英] How to create tuples from a single list with alpha-numeric chacters?

查看:147
本文介绍了如何使用字母数字字符从单个列表中创建元组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下2个元素的列表:

  ['AGCTT 6 6 35 25 10','AGGGT 7 7 28 29 2'] 

我需要制作一个列表或zip文件,使每个字母对应于它的在列表中进一步的数字。例如列表[0]列表/ zip应该读取

  {A:6,G 6,C:35,T:25,T:10} 

我可以列出这样的列表/拉链,它们存储列表[0],列表[1],...列表[n]的相应值吗?



注意:字母只能是A,G,C或T,数字可以取任何值



编辑1:以前,我以为我可以使用字典。但有几位成员指出,这是不能做到的。所以我只想做一个列表或邮政或其他任何建议把字母表元素与其相应的数字配对。

解决方案

使用元组拆分一次即可获取对,然后分割每对的第二个元素, >一起:

  l = ['AGCTT 6 6 35 25 10','AGGGT 7 7 28 29 2'] 

pairs =(a,b.split())for a,b in(sub.split(None,1)for sub in l]

哪个会给你:

  [[ A','6'),('G','6'),('C','35'),('T','25'),('T',' ('A','7'),('G','7'),('G','28'),('G','29'),('T' ] 

使用带有 list.append的for循环:

  l = ['AGCTT 6 6 35 25 10'''AGGGT 7 7 28 29 2'] 
out = []
for a,b in(sub.split(None,1)for sub in l):
out.append(zip(a,b))

如果您要将任何字母转换为 Z ,其中数字为< 10,您只需要另一个循环,我们检查每个配对中的数字:

  pairs = [[(Z,i)if int (i) 10其他(c,i)for c,i in zip(a,b.split())] 
for a,b in(sub.split(None,1)for sub in l)]
打印(对)

哪个会给你:



'pre> [[('Z','6'),('Z','6'),('C','35'),('T' ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ','29'),('Z','2')]]

成为一个常规循环:

  pairs = [] 
for a,b in(sub.split(None,1 )对于在l)中的子项:
pairs.append([(Z,i)if int(i)< 10 else(c,i)for c,i in zip(a,b.split ))))
print(pairs)

[( Z,i)如果int(i) 10其他(c,i)for c,i in zip(a,b.split())] 将字母设置为 Z 如果相应的数字 i 10 或者我们只是离开这封信。



如果你想要回到原来的对,只需要转置 zip

 在[13]中:l = ['AGCTT 6 6 35 25 10','AGGGT 7 7 28 29 2'] 

在[14] :pairs = [[(Z,i)if int(i)<对于a中的a,b的
....中的(),(sub,1,sub),sub(l,1)),其他(c,i)对于c,i在zip(a,b.split())] )]

在[15]中:pair
Out [15]:
[[('Z','6'),('Z','6' ,('C','35'),('T','25'),('T','10')],
[('Z','7'),(' ','7'),('G','28'),('G','29'),('Z','2')]对于a中的a(zip(* tup),对于tup),unzipped = [[.join(a),.join(b)]]

在[17] :unzipped
Out [17]:[['ZZCTT','6 6 35 25 10'],['ZZGGZ','7 7 28 29 2']]

zip(* ...)将给你原始元素回到元组他们自己,我们然后只需要将字符串加入到一起。如果您想恢复原始状态,您可以再次加入:

 在[18] [。 (对于tup的(zip(* tup))中的a,b([.join(a),.join(b)])] 
Out [19]:['ZZCTT 6 6 35 25 10','ZZGGZ 7 7 28 29 2']


I have the following list with 2 elements:

['AGCTT 6 6 35 25 10', 'AGGGT 7 7 28 29 2']

I need to make a list or zip file such that each alphabet corresponds to its number further in the list. For example in list[0] the list/zip should read

{"A":"6", "G":"6", "C":"35","T":"25","T":"10"}

Can I make a list of such lists/zips that stores the corresponding vales for list[0], list[1],...list[n]?

Note: The alphabets can only be A,G,C or T, and the numbers can take anyvalue

Edit 1: Previously, I thought I could use a dictionary. But several members pointed out that this cannot be done. So I just want to make a list or zip or anything else recommended to pair the Alphabet element to its corresponding number.

解决方案

Use tuples splitting once to get the pairs, then split the second element of each pair, zip together:

l  =['AGCTT 6 6 35 25 10', 'AGGGT 7 7 28 29 2']

pairs =  [zip(a,b.split()) for a,b in (sub.split(None,1) for sub in l]

Which would give you:

[[('A', '6'), ('G', '6'), ('C', '35'), ('T', '25'), ('T', '10')], [('A', '7'), ('G', '7'), ('G', '28'), ('G', '29'), ('T', '2')]]

Of using a for loop with list.append:

l  = ['AGCTT 6 6 35 25 10', 'AGGGT 7 7 28 29 2']
out = []
for a,b in (sub.split(None,1) for sub in l ):
    out.append(zip(a,b))

If you want to convert any letter to Z where the digit is < 10, you just need another loop where we check the digit in each pairing:

pairs = [[("Z", i ) if int(i) < 10 else (c, i) for c,i in zip(a, b.split())] 
         for a,b in (sub.split(None, 1) for sub in l)]
print(pairs)

Which would give you:

[[('Z', '6'), ('Z', '6'), ('C', '35'), ('T', '25'), ('T', '10')], [('Z', '7'), ('Z', '7'), ('G', '28'), ('G', '29'), ('Z', '2')]]

To break it into a regular loop:

pairs = []
for a, b in (sub.split(None, 1) for sub in l):
    pairs.append([("Z", i) if int(i) < 10 else (c, i) for c, i in zip(a, b.split())])
print(pairs)

[("Z", i) if int(i) < 10 else (c, i) for c, i in zip(a, b.split())] sets the letter to Z if the corresponding digit i is < 10 or else we just leave the letter as is.

if you want to get back to the original pairs after you just need to transpose with zip:

In [13]: l = ['AGCTT 6 6 35 25 10', 'AGGGT 7 7 28 29 2']

In [14]: pairs = [[("Z", i) if int(i) < 10 else (c, i) for c, i in zip(a, b.split())] for a, b in
   ....:          (sub.split(None, 1) for sub in l)]

In [15]: pairs
Out[15]: 
[[('Z', '6'), ('Z', '6'), ('C', '35'), ('T', '25'), ('T', '10')],
 [('Z', '7'), ('Z', '7'), ('G', '28'), ('G', '29'), ('Z', '2')]]

In [16]: unzipped = [["".join(a), " ".join(b)] for a, b in (zip(*tup) for tup in pairs)]

In [17]: unzipped
Out[17]: [['ZZCTT', '6 6 35 25 10'], ['ZZGGZ', '7 7 28 29 2']]

zip(*...) will give you the original elements back into a tuple of their own, we then just need to join the strings back together. If you wanted to get back to the total original state you could just join again:

In[18][ " ".join(["".join(a), " ".join(b)]) for a, b in (zip(*tup) for tup in pairs) ]
Out[19]: ['ZZCTT 6 6 35 25 10', 'ZZGGZ 7 7 28 29 2']

这篇关于如何使用字母数字字符从单个列表中创建元组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆