简体   繁体   English

如何使用字母数字字符从单个列表创建元组?

[英]How to create tuples from a single list with alpha-numeric chacters?

I have the following list with 2 elements: 我有以下包含2个元素的列表:

['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. 我需要制作一个列表或zip文件,以使每个字母都对应于列表中更进一步的数字。 For example in list[0] the list/zip should read 例如,在list [0]中,列表/邮政编码应显示为

{"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]? 我可以列出包含list [0],list [1],... list [n]的相应价位的此类list / zip的清单吗?

Note: The alphabets can only be A,G,C or T, and the numbers can take anyvalue 注意:字母只能是A,G,C或T,数字可以是任何值

Edit 1: Previously, I thought I could use a dictionary. 编辑1:以前,我认为我可以使用字典。 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. 因此,我只想列出一个列表或一个zip或任何其他建议将Alphabet元素与其对应的数字配对。

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: 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))

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: 如果要将任何字母转换为数字<10的字母Z ,则只需要另一个循环即可检查每个配对中的数字:

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. [("Z", i) if int(i) < 10 else (c, i) for c, i in zip(a, b.split())]如果对应的数字i < 10 [("Z", i) if int(i) < 10 else (c, i) for c, i in zip(a, b.split())]字母设置为Z否则,我们就按原样保留这封信。

if you want to get back to the original pairs after you just need to transpose with zip : 如果您只需要用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. zip(*...)会将原始元素返回给它们自己的元组,然后我们只需要将字符串重新连接在一起即可。 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']

If you consider using tuples to pair the items, then this works: 如果您考虑使用元组对项进行配对,则可以这样做:

>>> from pprint import pprint
>>> lst = ['AGCTT 6 6 35 25 10', 'AGGGT 7 7 28 29 2']
>>> new_lst = [list(zip(sub[0], sub[1:])) for sub in [i.split() for i in lst]]
>>> pprint(new_lst)
[[('A', '6'), ('G', '6'), ('C', '35'), ('T', '25'), ('T', '10')],
 [('A', '7'), ('G', '7'), ('G', '28'), ('G', '29'), ('T', '2')]]
  1. [i.split() for i in lst] : An initial split on the string. [i.split() for i in lst] :字符串的初始拆分。

  2. zip(sub[0], sub[1:])) : Zip lists of alphabets and list of numbers zip(sub[0], sub[1:])) :邮政编码列表和数字列表

Iterate through list > iterate through items (alpha numeric) of the list and construct list of characters and numbers > and then construct list of tuple. 遍历列表>遍历列表的项(字母数字)并构造字符和数字列表>然后构造元组列表。

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

for s in alphanum:
   ints = []
   chars = []
   for i in s.split():
      if i.isdigit():
         ints.append(i)
      else:
         chars.append(i)

   new_tuple = []
   for (n, item) in enumerate(list(chars[0])):
       new_tuple.append((item, ints[n]))
   list_of_tuple.append(new_tuple)

print list_of_tuple

This code would work, assuming the elements in the list are correctly formed. 假设列表中的元素格式正确,此代码将起作用。 This means the number of letters and numbers must match! 这意味着字母和数字的数量必须匹配!

And it will overwrite the value if the key already exists. 如果密钥已经存在,它将覆盖该值。

list = ['AGCTT 6 6 35 25 10', 'AGGGT 7 7 28 29 2']
dictionary = {}

for line in list:
    split_line = line.split()
    letters = split_line[0]
    iterator = 1
    for letter in letters:
        dictionary[letter] = split_line[iterator]
        iterator += 1

print dictionary

This modified one will check if the key exists and add it to a list with that key: 修改后的代码将检查该密钥是否存在,并将其添加到具有该密钥的列表中:

list = ['AGCTT 6 6 35 25 10', 'AGGGT 7 7 28 29 2']
dictionary = {}

for line in list:
    split_line = line.split()
    letters = split_line[0]
    iterator = 1
    for letter in letters:
        if letter in dictionary.keys():
            dictionary[letter].append(split_line[iterator])
        else:
            dictionary[letter] = [split_line[iterator]]

        iterator += 1

print dictionary

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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