【发布时间】: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