【问题标题】:Convert Output String into List - Python将输出字符串转换为列表 - Python
【发布时间】:2015-06-05 21:47:30
【问题描述】:

我正在将人类 ORF 密码子代码文件转换为两个列表,一个是 ORF_ID 片段,另一个是实际序列数据。

for i in fasta_file:
    ID_list = []
    sequences = []
    if '>' in i:
        ORF_start = i.find('>')
        ORF_end = i.find('PERFECT_MATCH')
        ORF_ID = i[ORF_start:ORF_end+13]
        ID_list.append(ORF_ID)
        print type(ID_list)
    else:
        sequences.append(i)

当我打印列表时,它们都不是字符串列表,而是许多行的小列表。

例子:

What I want = separate list of ORF_ID and another data or ['a','b','c']
What I get (represented through variables) = 
['a']
[]
['b']
[]
['c']

这在 python 上总是发生在我身上。我希望得到一些帮助。

【问题讨论】:

  • 您不应该在循环之外创建序列和 ID_list 吗?
  • biopython 可能已经做了你想做的事情......

标签: python list append output text-files


【解决方案1】:

您正在重置 for 循环内的列表。

看看这是否能满足你的需求:

ID_list = []
sequences = []
for i in fasta_file:
    ORF_start = i.find('>')
    if ORF_start >= 0:
        ORF_end = i.find('PERFECT_MATCH', ORF_start)
        ORF_ID = i[ORF_start:ORF_end+13]
        ID_list.append(ORF_ID)
    else:
        sequences.append(i)
print ID_list
print sequences

如果fasta_file

['gattaca >cippalippaPERFECT_MATCHxyz', 'sgnoppa >cippalippaPERFECT_MATCHxyz', 'wslewa >cippalippaPERFECT_MATCHxyz', 'whatever', 'anotherone']

它产生这个输出:

['>cippalippaPERFECT_MATCH', '>cippalippaPERFECT_MATCH', '>cippalippaPERFECT_MATCH']
['whatever', 'anotherone']

【讨论】:

    猜你喜欢
    • 2015-04-14
    • 2016-11-09
    • 1970-01-01
    • 2023-03-16
    • 2017-11-15
    • 1970-01-01
    • 2014-08-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多