무한 루프가 있다고 생각합니까? 검색 용어가 키가되는 사전과 my_string에서 해당 키가있는 인덱스를 만들었습니다. 나는 각 검색어에 대한 색인으로 나열된 my_string에있는 모든 일치 목록을 가진 search_dict를 만들고 싶습니다.문자열에있는 모든 일치 항목의 색인을 찾아 사전에 검색어와 색인을 추가합니다.
수백만 개의 항목이있는 항목을 제외하고 내 search_dict는 채워지지 않습니다.
my_string='Shall I compare thee to a summer\'s day?'
#string_dict has only a single index as a value where its key was found in my_string
string_dict={'a': 36, ' ': 34, 'e': 30, '': 39, 'h': 17, 'm': 29, 'l': 4, 'o': 22, 'e ': 19, 's': 33, 'r': 31, 't': 21, ' t': 20, 'e t': 19}
#I'd like search_dict to have all indices for key matches in my_string
search_dict=dict()
for key in string_dict:
search_dict[key]=list()
for item in search_dict:
start=0
end=len(my_string)
found=my_string.find(item,start,end)
while start<end:
if found>=0:
search_dict[key].append(found)
start=found+len(item)
found=my_string.find(item,start,end)
else:
break
print search_dict
나는 또한 아래 변경을 시도했다. 아직도 my_string.find가 -1 (발견되지 않음)이되면 루프가 다음 검색 키 반복을 위반하지 않는 이유는 확실하지 않습니다.
else:
break
#with
if found<0:
break
코드 검토를 찾고 있습니까? –
나는 이렇게함으로써 리뷰를하는 것이 도움이 될 것입니다. 나는 왜 그것이 영원히 반복하는지 이해하지 못한다. – 12345678910111213
네, 무한 루프가 있습니다. 두 번째 while 문에서 조건을 확인하십시오. – senderle