简体   繁体   English

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

[英]Python: indexing letters of string in a list

I would like to ask if there is a way how to get exact letters of some string stored in a list? 我想问一下是否有一种方法可以获取列表中存储的某些字符串的确切字母? I'm working with DNA strings, get them from FASTA file using BioPython SeqIO and store them as strings in a list. 我正在使用DNA字符串,使用BioPython SeqIO从FASTA文件中获取它们,并将其作为字符串存储在列表中。 In next step I will convert it to numerical sequence (called genomic signals). 在下一步中,我将其转换为数字序列(称为基因组信号)。 But as novice in Python I don't know how to obtain it from the list correctly. 但是作为Python的新手,我不知道如何从列表中正确获取它。 Should I use different data type? 我应该使用其他数据类型吗?

In Maltab I used: 在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

what creates a vector and replace zeros with according values. 什么会创建向量,并用相应的值替换零。 I wasn't able to find a similar question. 我找不到类似的问题。 Thanks for replies. 感谢您的答复。

My python code: 我的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. 

I don't know how MATLAB does it. 我不知道MATLAB是如何做到的。 In Python you can access any position in a string without converting to a list: 在Python中,您可以访问字符串中的任何位置而无需转换为列表:

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

If you want to store "strings in a list" you can do this: 如果要将“字符串存储在列表中”,可以执行以下操作:

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM