2016-10-31 34 views
1

我需要阅读sp_list1,使得来自相应位置的每个列表中的三个元素都在列表中。接下来的三个(不重叠的)被放入一个单独的列表中,以便列出一个列表。如何使用Python中原始列表中的位置特定元素创建列表列表?

Input: seq_list1 = ['ATGCTATCATTA','ATGCTATCATTA','ATGCTATCATTT'] 

所需的输出

seq_list_list1 =[['ATG','ATG','ATG'],['CTA','CTA','CTA'],['TCA','TCA','TCA'],['TTA','TTA','TTT']] 

我有一种感觉,这应该使用像列表解析是可行的,但我不能算出它(特别是,我无法弄清楚如何访问项目的索引,以便在使用列表理解时选择三个不重叠的连续索引)。

+0

你一定已经意识到'append'需要一个参数。你为什么不提供一个? – TigerhawkT3

+0

这是我不确定的一部分。我只是把一个空的列表,就像我编辑过的那样? – Biotechgeek

+0

它看起来像实际上做你想做的事的代码与你所尝试的完全不同(因为上面给出的原因,它甚至不会没有错误地运行)。这当然是可行的,但SO不是一种编码服务。你将不得不学习更多,并再次尝试。 – TigerhawkT3

回答

0

你可以在这里使用这段代码,你可以根据你的愿望操纵它。我希望它能帮助:

seq_list1 = ['ATGCTATCATTA','ATGCTATCATTA','ATGCTATCATTT'] 
n=3 

seq_list1_empty=[] 
counter = 0 

for k in range(len(seq_list1)+1): 
    for j in seq_list1: 
     seq_list1_empty.append([j[i:i+n] for i in range(0, len(j), n)][counter])# this will reassemble the string as an index 
    counter+=1 

counter1=0 
counter2=3 
final_dic=[] 
for i in range(4): 
    final_dic.append(seq_list1_empty[counter1:counter2])#you access the first index and the third index here 
    counter1+=3 
    counter2+=3 
print final_dic 

输出是

[['ATG', 'ATG', 'ATG'], ['CTA', 'CTA', 'CTA'], ['TCA', 'TCA', 'TCA'], ['TTA', 'TTA', 'TTT']] 
+0

1)你正在为他们做某人的工作,而SO不是一个编码服务。 2)___输出甚至不正确.___ – TigerhawkT3

+0

我想在这里帮忙。我仍在检查输出PRO:D –

+0

编辑完成后,你仍然在为他们做零工解释(对于OP以及未来的访问者无用),这仍然是错误的。 – TigerhawkT3

0
seq_list1 = ['ATGCTATCATTA','ATGCTATCATTA','ATGCTATCATTT'] 


def new_string(string, cut): 
    string_list = list(string) # turn string into list 

    # create new list by appending characters from from index specified by 
    # cut variable 
    new_string_list = [string_list[i] for i in range(cut, len(string_list))] 

    # join list characters into a string again 
    new_string = "".join(new_string_list) 

    # return new string 
    return new_string 


new_sequence = [] # new main sequence 

# first for loop is for getting the 3 sets of numbers 
for i in range(4): 
    sub_seq = [] # contains sub sequence 

    # second for loop ensures all three sets have there sub_sets added to the 
    #sub sequence 
    for set in range(3): 
     new_set = seq_list1[set][0:3] #create new_set 
     sub_seq.append(new_set) # append new_set into sub_sequence 


    #checks if sub_seq has three sub_sets withing it, if so 
    if len(sub_seq) == 3: 
     #the first three sub_sets in seq_list1 sets are removed 
     for i in range(3): 
      # new_string function removes parts of strings and returns a new 
      # string look at function above 

      new_set = new_string(seq_list1[i], 3) # sub_set removed 
      seq_list1[i] = new_set # new set assigned to seq_list1 

    # new_sub sequence is added to new_sequence 
    new_sequence.append(sub_seq) 

    #sub_seq is errased for next sub_sequence 
    sub_seq = [] 


print(new_sequence) 

试试这个。如果难以理解,我很抱歉,文档不够精通。

相关问题