Python新手:修复迭代中的无限循环,2list搜索,输出到字典

2024-09-30 10:34:59 发布

您现在位置:Python中文网/ 问答频道 /正文

我是Python新手。我在试着编一本序列比对词典。---在生物学中,序列比对意味着将一个序列滑过通常较大的序列。当两个序列对齐时,记录一个匹配。我想在list1和list2中找到所有可能的匹配项。在

生物学中用于序列比对的工具不能很好地处理非生物字符串,需要氨基酸或核苷酸的单字母代码。我想创建一个可以处理任何字符串的工具。在

我搜索两个列表:list1,list2,都只包含字符串。搜索字符串(sslice)仅来自列表1。搜索字符串(sslice)是通过迭代list1中每个项(item1)的切片生成的。搜索字符串(sslice)是在list1本身中搜索的,然后是list2。当找到一个搜索,计数器增加。字符串splice found(sslice)=键和找到的次数(count)=我字典中的值

目标:我之前已经创建了一个my\u字符串。从中列出了两个列表:列表1和列表2。每个listu字符串只出现一次。List2是多次出现的my_string的每个子字符串。下面的代码试图挖掘list1和list2的子字符串,这些子字符串相互组成。在

问题是:有没有无限循环?进程将永远运行而没有输出:(

#Sample list1, list2 and desired my_dict:

list1=['and', 'a', 'ant'] 
#Search slices come from list1: sslice=item1[start:end]
list2=['and', 'b', 'an']
my_dict={'a':  5, 'an': 4, 'and': 2,  'n': 4, 'nd': 2, 'd': 2, 'ant': 1, 'nt': 1, 't': 1}
#         s        e        e          s       e        s       se        s        s
#new start slice index marked "s", new end slice index marked "e", new start & end index "se"
#slices 'a' item1[0:1] 'an' item1[0:2] 'and' item1[0:3] 'n' item1[1:2] 'nd' item1 [1:3] 'd' item1[2:3] 'ant' item1[0:3] 'nt' item1[1:3] 't' item1[2:3]
#found 'a' 5 times, 'an' 4x, 'and' 2x, 'n' 4x, 'nd' 2x, 'd' 2x, 'ant' 1x, 'nt' 1x, 't' 1x

my_dict=dict()
for item1 in list1:
    for item2 in list1:
        start=0
        end=start+1
        count=0
        sslice=item1[start:end]
        while start<len(item1):
            if sslice in item2:
                count+=1
                my_dict.update({sslice:count})
                end+=1
                sslice=item1[start:end]
            if end>=len(item1):
                start+=1
                end=start+1
            if start>=len(item1):
                 break
    for item3 in list2:
        start=0
        end=start+1
        count=0
        sslice=item1[start:end]
        while start<len(item1):
            if sslice in item3:
                count+=1
                my_dict.update({sslice:count})
                end+=1
                sslice=item1[start:end]
            if end>=len(item1):
                start+=1
                end=start+1
            if start>=len(item1):
                break                          
print my_dict

Tags: and字符串列表lenifmycount序列
1条回答
网友
1楼 · 发布于 2024-09-30 10:34:59

我想你的意图是这样的,但我不知道这意味着什么:

from collections import defaultdict

list1 = [' c', ' a', ' d', 'ee', ' i', 'om', 'a ', 'al', ' s', 'ay', 'ar', 'me', 'ha', 'he', 'e to', 's ', 'l ', 'e th', 'll', "r'", 'su', 'o ', 're', 'sh', 'mp', 'er', 'mm', '?']

list2 = ['a', 'h', 'm', 'l', 'o', 's', 'r', 'e t']

my_dict = defaultdict(int)
for item1 in list1:
    for start in range(len(item1)):
        for end in range(start + 1, len(item1)):
            sslice = item1[start:end]
            my_dict[sslice] += sum(sslice in item2 for item2 in list1)
            my_dict[sslice] += sum(sslice in item3 for item3 in list2)

print my_dict

你也可能想要for end in range(start + 1, len(item1) + 1)。再说一遍,不知道你想做什么。在

相关问题 更多 >

    热门问题