【问题标题】:Python: indexing letters of string in a listPython:在列表中索引字符串的字母
【发布时间】:2016-09-02 21:23:00
【问题描述】:

我想问一下是否有一种方法可以获取存储在列表中的某些字符串的确切字母?我正在处理 DNA 字符串,使用 BioPython SeqIO 从 FASTA 文件中获取它们并将它们作为字符串存储在列表中。在下一步中,我会将其转换为数字序列(称为基因组信号)。但是作为 Python 的新手,我不知道如何正确地从列表中获取它。我应该使用不同的数据类型吗?

我在 Maltab 中使用过:

a=1+1i;c=-1-1i;g=-1+1i;t=1-1i; %base values definition
for i=1:number of sequences
    length_of_sequence(i)=length(sequence{1,i});
    temp=zeros(1,length_of_sequence(i),'double');
    temp(sequence{i}=='A')=angle(a);
    temp(sequence{i}=='C')=angle(c);
    temp(sequence{i}=='G')=angle(g);
    temp(sequence{i}=='T')=angle(t);    
    KontigNumS{i,1}=cumsum(temp); %cumulated phase of whole vector
end

什么创建一个向量并用相应的值替换零。 我找不到类似的问题。感谢您的回复。

我的python代码:

#Dependencies
from Bio import SeqIO #fasta loading
import cmath #complex numbers
import numpy as np 

#Open FASTA file new variable
lengths=list()
sequences=list()
handle=open("F:\GC_Assembler_Python\xx.fasta","r")
for record in SeqIO.parse(handle, "fasta"):
    print(record.id)
    print(len(record.seq))
    lengths.append(len(record.seq))
    sequences.append(str(record.seq))

#Convert to genomic signals
a=complex(1,1)
c=complex(-1,-1)
g=complex(-1,1)
t=complex(1,-1)
I stopped here. 

【问题讨论】:

  • 如果您可以发布您正在讨论的列表示例以及所需的输出,那将有很大帮助
  • 如果你在谈论一个实际的字符串,比如“这是一个字符串”,请使用variable = list("Your string goes here")
  • 那个代码不是 biopython,是吗?使用 biopython,您可以获得序列的迭代器,您可以随意转换和使用。我觉得很难理解你的问题,因为 SeqIO 返回一个序列类而不是一个字符串(尽管大多数方法都是相同的)
  • 是的,这是 Matlab。我的python代码其实是(我加起来的)

标签: python biopython


【解决方案1】:

我不知道 MATLAB 是如何做到的。在 Python 中,您可以访问字符串中的任何位置,而无需转换为列表:

DNA = "ACGTACGTACGT"
print(DNA[2])
# outputs "G", the third base

如果您想存储“列表中的字符串”,您可以这样做:

DNA_list = ["AAAAAA", "CCCCC", "GGGGG", "TTTTT"]
print(DNA_list[0][0])
# outputs "A", the first "A" of the first sequence
print(DNA_list[1][0])
# outputs "C", the first "C" of the second sequence

【讨论】:

  • 非常感谢!我想这就是我一直在寻找的。是的,我将序列字符串存储在一个列表中。
【解决方案2】:

如果您使用以下内容,您可以将任何字符串转换为列表 list(The string)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-04-09
    • 2020-03-12
    • 2016-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-21
    相关资源
    最近更新 更多