阅读量:0
草稿一、用单一方法遍历文本
9.1代码
genome_text = 'TTATGTTTTAAGGATGGGGCGTTAGTT' def div_word(word_to_judge): len_num = len(word_to_judge) save_word = "" if len_num % 3 == 0: print("This word is valid") if save_word.find("ATG") == "ATG": for i in range(1,len_num): save_word = word_to_judge[0 + i:i + 3] print(save_word) else: print("This word is invalid") div_word(genome_text)
代码结果
我没用find这个命令.我想用for循环不断循环一个个遍历这些字符.
有的时候有结果,有的时候只能沉淀
草稿二、尝试分割文本
.9.2
今天研究了一个对文本进行分割的函数
def div_text(start_num, end_num, text_word): text_i_save = "" for i_t in range(start_num, end_num): text_i_save += text_word[i_t] print(text_i_save) div_text(0, 3, genome_text) div_text(4, 7, genome_text) div_text(8,11, genome_text)
一、利用分割文本得到的结果这里写目录标题
# 这个序列不对.应该遵循的是另一个模式 div_text(0, 3, genome_text) div_text(4, 7, genome_text) div_text(8,11, genome_text)
二、遍历了整个字符串的序列
div_text(0, 3, genome_text) div_text(2, 5, genome_text) div_text(4, 7, genome_text) div_text(6, 9, genome_text) div_text(8, 11, genome_text) div_text(10, 13, genome_text)
起始到结束间隔3, 两行之前的起始和截止都间隔2.这样的就可以遍历整个字符串
三、奇怪的循环遍历了整个字符串的序列
大家看当起始从-2+i的时候结果
for i in range(0, len_num, 2): div_text(-2 + i, 1 + i, genome_text)
多出了TTT
那我就删除呗,好见证奇怪的时候来了
for i in range(0, len_num, 2): div_text(0 + i, 1 + i, genome_text)
我们来进行打印分析
for i in range(0, len_num, 2): div_text(0 + i, 1 + i, genome_text) print("0+i", 0 + i, "1+i", 1 + i)
又换成了-2
我发现我画蛇添足了.起始位置,直接用i就完事了,成功了.
但是新的问题开始
四、新的问题string index out of range
在函数div_text里
text_i_save += text_word[i_t]
它没事呀.怎么一放到循环里变成了这个怪样子.???
五、字符串索引超出范围这里写目录标题
for i in range(0, len_num, 2): if i+2 < len_num: div_text(i, i + 3, genome_text)
五、区分开头ATG的函数里写目录标题
def cut_word_start(judge_word,text_word): len_num = len(text_word) for i in range(0, len_num, 2): if i + 2 < len_num: if div_text(i, i + 3, text_word) == judge_word: print(text_word[i + 3:])
六、9.2 17点前写的代码段,可以截取ATG开头这里写目录标题
genome_text = 'TTATGTTTTAAGGATGGGGCGTTAGTT' len_num_out = len(genome_text) def div_text(start_num, end_num, text_word): text_i_save = "" for i_t in range(start_num, end_num): text_i_save += text_word[i_t] return text_i_save def cut_word_start(judge_word, text_word): len_num = len(text_word) for i in range(0, len_num, 2): if i + 2 < len_num: if div_text(i, i + 3, text_word) == judge_word: return text_word[i + 3:] def cut_word_end(judge_word, text_word): len_num = len(text_word) for i in range(0, len_num, 2): if i + 2 < len_num: if div_text(i, i + 3, text_word) == judge_word: print(text_word[:i+3]) # print(i) else: print('a',i) def main(): a = cut_word_start("ATG", genome_text) # print(a) cut_word_end("TAA", a) main()