多次循环列表

时间:2018-02-13 00:00:28

标签: python iteration genome

是否可以多次遍历列表?基本上,我有一个字符串列表,我正在寻找最长的超弦。列表中的每个字符串都有一些重叠,至少有一半的长度,它们都是相同的大小。我想查看我添加的超字符串是否以列表中的每个序列开头或结尾,当我找到一个匹配我想将该元素添加到我的超级字符串,从列表中删除该元素,然后一次又一次地循环它直到我的列表为空。

sequences=['ATTAGACCTG','CCTGCCGGAA','AGACCTGCCG',''GCCGGAATAC']
halfway= len(sequences[0])/2
genome=sequences[0]     # this is the string that will be added onto throughout the loop
sequences.remove(sequences[0]) 


for j in range(len(sequences)):
    for sequence in sequences:
        front=[]
        back=[]
        for i in range(halfway,len(sequence)):

            if genome.endswith(sequence[:i]):
                genome=genome+sequence[i:] 
                sequences.remove(sequence)

            elif genome.startswith(sequence[-i:]):
                genome=sequence[:i]+genome  
                sequences.remove(sequence)
'''
            elif not genome.startswith(sequence[-i:]) or not genome.endswith(sequence[:i]):

                sequences.remove(sequence)      # this doesnt seem to work want to get rid of 
                                                #sequences that are in the middle of the string and 
                                                 #already accounted for 
'''

当我不使用最终的elif语句并给出正确答案ATTAGACCTGCCGGAATAC时,这是有效的。但是,当我使用更大的字符串列表执行此操作时,我仍然在列表中留下我希望为空的字符串。如果我只是寻找要添加到超弦的前面和后面的字符串(我的代码中的基因组),也是最后一个循环甚至是必要的。

2 个答案:

答案 0 :(得分:0)

试试这个:

sequences=['ATTAGACCTG','CCTGCCGGAA','AGACCTGCCG','GCCGGAATAC']
sequences.reverse()
genome = sequences.pop(-1)     # this is the string that will be added onto throughout the loop

unrelated = []

while(sequences):
    sequence = sequences.pop(-1)
    if sequence in genome: continue
    found=False
    for i in range(3,len(sequence)):
        if genome.endswith(sequence[:i]):
            genome=genome+sequence[i:]
            found = True
            break
        elif genome.startswith(sequence[-i:]):
            genome=sequence[:i]+genome  
            found = True
            break
    if not found:
        unrelated.append(sequence)

print(genome)
#ATTAGACCTGCCGGAATAC
print(sequences)
#[]
print(unrelated)
#[]

我不知道你是否保证在同一批次中没有多个不相关的序列,所以我允许不相关的。如果没有必要,请随意删除。

Python从list前面删除的处理效率非常低,所以我颠倒了列表并从后面拉了下来。根据完整数据(可能是您的示例数据),可能不需要进行反转。

我从sequences list弹出,同时有可用的序列,以避免在迭代它时从list中删除元素。然后我检查它是否已经在最终的基因组中。如果不是,那么我将检查endswith / beginswith检查。如果找到匹配,将其切成基因组;设置发现标志;突破for循环

如果序列尚未包含且未找到部分匹配,则会将其放入unrelated

答案 1 :(得分:0)

这就是我最终解决它的方法,我意识到你需要做的就是找出哪个字符串是超弦的起点,因为我们知道序列的重叠是1/2或者更多我找到了一半不包含在任何序列中。从这里开始,我在列表中循环等于列表长度的次数,并查找基因组末端与适当序列的开头匹配的序列。当我发现这个时,我将序列添加到基因组(超弦)上,然后删除了这个序列并继续迭代列表。使用长度为1000的50个序列列表时,此代码需要大约.806441才能运行

<table class="table1" style="width:82%">
  <col style="width:18%">
  <col style="width:20%">
  <col style="width:20%">
  <col style="width:40%">
<tr>
    <th class="title88" colspan=1>From</th>
    <th class="title88" colspan=1>Received</th>
    <th class="title88" colspan=1>Type</th>
    <th class="title88" colspan=1>Name</th>
</tr>
<tbody>
{% for email in email_list %}
<tr>
<td colspan=1><a href="{{ email.get_absolute_url }}"><h2 class="title4">{{email.sender}}</h2></td></a>
<td colspan=1><a href="{{ email.get_absolute_url }}"><h2 class="title4">{{ email.timestamp }}</h2></td></a>
<td colspan=1><a href="{{ email.get_absolute_url }}"><h2 class="title4">{{ email.subject }}</h2></td></a>
<td colspan=1><a href="{{ email.get_absolute_url }}"><h2 class="title4">{{ email.procedure_name }}</h2></td></a>
</tr>
{% endfor %}
</tbody>
相关问题