Python: 索引列表中字符串的字母

Python: indexing letters of string in a list

我想问一下有没有办法获取存储在列表中的某个字符串的确切字母?我正在处理 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. 

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

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

如果你想存储"strings in a list"你可以这样做:

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

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