【问题标题】:How to modify .split function to to apply to different alpha numeric inputs?如何修改 .split 函数以应用于不同的字母数字输入?
【发布时间】:2017-01-26 15:29:47
【问题描述】:

我有以下两种输入:

## Case 1 (a)
# Input #1(a)
>qrst
ABC 10 9 7
>qqqq
ACC 2 5 3

# Case 1 (b) --> Simplified form of Case 1(a)
# Input #1()
>qrst
A 10
>qqqq
A 2

# After reading in the file and making it a list I store the above in l
l = ['ABC 10 9 7', 'ACC 2 5 3'] # Case 1(a)
l = ['A 10', 'A 2'] # Case 1(b)

 #In the following code I split the alpha-numeric elements above and
 #create separate lists where I store the alphabets alone (in list "sequences") and 
 #numeric alone (in list "qualities")

ll = len(l)
all_inputs = [] 
for i in range(0,ll):
    sq = l[i]
    sequence = sq.split(" ")[0] ## Stores only the alphabets
    qualities = sq.split(" ")[1:] ## Stores only the numeric
    qualities = filter(None, qualities)
    for sub in sequence:
        if sub == "-":
            idx = list(sequence).index(sub)
            qualities.insert(idx,"0")  
    all_inputs.append((sequence, qualities))
    print 

   #Case1(a) Output reads currently reads as
   A   #print sequence
   ['2']  #print qualities

我遇到另一种类型的输入文件如下:

## Case 2
 #  Input #2
 >qrst
A    #No space after A

10
>qqqq
A    #No space after A

2

Here 
l = ['A10', 'A2']

I use the same code as above

#Case2 Output reads currently reads as
A2 #print sequences
[] #print qualities

我需要#Case 2 也有输出 #print 序列 ['2'] #打印质量

如何修改上面的代码,使其可以同时容纳 ['ABC 10 9 7', 'ACC 2 5 3'] 或 ['A 10','A 2'] 或 ['A10',' A2'] 类型的输入文件/'l'? 我需要案例 2 具有与案例 1(b) 相同的输出,以便稍后可以应用相同的代码行。但请记住,它必须是案例 1 和案例 2 的通用代码。

【问题讨论】:

  • 听起来你在寻求家庭作业帮助。这不是这个网站的目的。
  • 另外,你的问题不是很好。
  • 请修改您的问题,以便更清楚地了解您的每个样本输入应如何与准确的预期输出一起表现。请阅读如何整理minimal reproducible example 以帮助编辑您的问题,以便获得好评。
  • 我已经完成了编码的“硬”部分......它可以工作。我真的不明白如何编写更通用的代码,我认为这是一个合理的疑问。另外,我正在做一个研究项目,所以我需要确保所有类型的输入都是可读的。如果您可以让我知道问题的哪一部分不清楚,我可以相应地修改
  • @idjaw 我已编辑。希望现在清楚

标签: python split alphanumeric


【解决方案1】:

正则表达式将是一个很好的方法,您正在寻找一个字母([A-Z]),后跟一个可选空格?,后跟一个或多个数字(\d+)

>>> import re
>>> re.match('([A-Z]) ?(\d+)', 'A10').groups()
('A', '10')
>>> re.match('([A-Z]) ?(\d+)', 'A 10').groups()
('A', '10')

【讨论】:

  • 但是对于大序列我该如何做呢?我有什么 l = ['A10', 'A2','C8','D20']?我需要将它们全部拆分
  • 就像在您的代码中一样,使用循环并将其应用于每个元素
  • 我不熟悉 re.match。因此,如果我有 l = ['A10', 'A2','C8','D20'] 我的下一行是否为 for i in l: re.match('([A-Z]) ?(\d+)', 'i''i+1').groups() 。它接受什么类型的输入?
  • 我认为应该只是for i in l: re.match('([A-Z]) ?(\d+)', i).groups()
  • 不,我认为您混淆了我想要代码差异的地方 首先,您建议的代码打印[('A', '10'), ('A', '2')],我需要['A 10', 'A 2'] as l. OR Else I need the difference to be reflected in the output. At the end of the last two lines of code, I print sequences` 和qualities。我需要所有情况的输出都相同。即如果初始输入是 ['A 10', 'T 8'] 或 ['AGT 10 10 20', 'TAT 2 3 4'] 或者如案例 2 中所述(仅单个字母)。跨度>
【解决方案2】:

这行得通吗:

import re

input_seq = "A10 B   20 C30 D1 Z   67"
input_raw = re.split(r'([A-Z]) *(\d+)' , input_seq)
input_clean = [x for x in input_raw if x and not x.isspace()]
print zip(input_clean[::2], input_clean[1::2])

给予

[('A', '10'), ('B', '20'), ('C', '30'), ('D', '1'), ('Z', '67')]

如果你改变最后一个表达式:

print [ "{} {}".format(*x) for x in zip(input_clean[::2], input_clean[1::2]) ]

你得到:

['A 10', 'B 20', 'C 30', 'D 1', 'Z 67']

或者,您可以尝试规范化输入:

input_seq = "A10 B   20 C30 D1 Z   67"
print re.sub(r'([A-Z]) *(\d+)', r'\1 \2', input_seq)

这将打印:

A 10 B 20 C 30 D 1 Z 67

如果您只想修改输入列表:

l = ['A10', 'A2']
l = [ re.sub(r'([A-Z]) *(\d+)', r'\1 \2', x) for x in l]
print l

打印出来:

['A 10', 'A 2']

【讨论】:

  • 我已经修改了问题。查看案例 1(a) 和案例 1(b)。案例 1(b) 和 2,是案例 1 的“特殊情况”
  • 为案例2提供案例1(a)示例,是“ABC2 3 3”吗?
  • 好吧,只考虑3个案例。案例 A l = ['ABC- 10 9 7 2', 'ACC- 2 5 3 0'],案例 B l = ['A 10', 'A 3'],案例 C l =['A10', 'A3']。 “字母”可以包含“字母和字符,如“-”或“?”
  • 那我想答案应该已经对你有所帮助了,你卡在哪里了?
  • 我使用 l = ['ATGC 10 9 8 7 ', 'AGGG 2 7 8 6'] input_seq = l input_raw = re.split(r'([A-Z]) *(\d+)' , input_seq) input_clean = [x for x in input_raw if x and not x.isspace()] print [ "{} {}".format(*x) for x in zip(input_clean[::2], input_clean[1::2]) ] 我收到此错误消息:文件“C:\Users\....”第 27 行,在 input_raw = re.split(r'([AZ]) *(\d+)' , input_seq) 文件“C:\Python27\lib\re.py”,第 171 行,拆分返回 _compile(pattern, flags).split (string, maxsplit) TypeError: expected string or buffer
猜你喜欢
  • 2018-07-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-04-25
  • 1970-01-01
  • 1970-01-01
  • 2013-10-26
  • 2015-12-02
相关资源
最近更新 更多