2017-10-13 1 views
-1

이 코드는 간단한 목록에서 완벽하게 실행되었지만 데이터베이스에서 실행하려고했기 때문에 double for 루프에 StopIteration 오류가 발생합니다. EDIT : 이제 문제는 단지 어떤 유형의 오류도 발생하지 않는다는 것입니다. 명령 행은 실행할 때 비어 있습니다.간단한 목록 대신 데이터베이스에서 Visual Studio를 실행할 때 코드가 실행되지 않는 이유는 무엇입니까?

['flore', 'fleur', 'fleuriste'], ['remaniement', 'remanier', 'manier', 'maniable'], ['désaimer', 'aimer', 'aimant'], ['mêler', 'emmêler', 'désemmêler'] 

별도로 코드의 각 부분을 시도하는 데, 모든 작품 :

# -*- coding: utf-8 -*- 
import pyodbc 
import re 
from difflib import SequenceMatcher 

[x for x in pyodbc.drivers() if x.startswith('Microsoft Access Driver')] 

# Connection to accdb 

conn_str = (
    r'DRIVER={Microsoft Access Driver (*.mdb, *.accdb)};' 
    r'DBQ=C:\\Users\\alice\\Desktop\\lexique3.accdb;' 
    ) 
cnxn = pyodbc.connect(conn_str) 
crsr = cnxn.cursor() 

# Put all words into a list 

crsr.execute("select unites_lexicales from Mot where cat_grammat='Verbe' or 'Nom' or 'Adjectif' or 'Adverbe'") 
resultat = crsr.fetchall() 
words = [row[0] for row in resultat] 
#print(words) 

# Remove unwanted characters and put radicals into a list 

radicals = [] 
motifp = "^(an|re|dé|dés)" 
motifs = "(ist|man|er|an|able|asyon)$" 

for word in words : 
    word = re.sub(motifp, '', word) 
    word = re.sub(motifs, '', word) 
    radicals.append(word) 
#print(radicals) 

# Match radicals and put words into new lists 

ratio = 0.6 
n = len(radicals) 
result = [] 
used_js = [] 

for i in range(n) : 
    if i in used_js: 
     continue 
    matches = [words[i]] 
    js = (x for x in range(n) if x != i and x not in used_js) 
    for j in js : # Previous error : StopIteration 
     if SequenceMatcher(None, radicals[i], radicals[j]).ratio() >= ratio : 
      matches.append(words[j]) 
      used_js.append(j) 
    result.append(matches) 
print(result) 

가 예상되는 결과는 (내가 이전에 간단한 목록 있던) 다음과 같다 : 여기

내 코드입니다 double for 루프 외에도 괜찮습니다. 왜 내가 그것을 실행할 수 없는지에 대한 단서와 내가 이것을 고칠 수있는 방법이 정말로 도움이 될 것이다. 감사합니다.

(x for x in range(n) if x != i and x not in used_js) 

이 아이템을 얻을 수 없습니다 반복자입니다 :

+0

당신이 스택 추적을 게시 할 수 있습니까? –

+0

@LaurentLAPORTE print_stack()을 사용하여 작동하는 경우, 실행할 때 아무 것도 나타나지 않습니다. –

+0

@LaurentLAPORTE Visual Studio에서는 오류가 전혀 발생하지 않지만 코드는 실행되지 않는다고 알려줍니다. –

답변

0

당신은 StopIteration 때문에이있다. 즉, 목록 [x for x in range(n) if x != i and x not in used_js]은 비어 있습니다.

당신은 고전 for 루프를 사용한다고 : 또한 목록으로 발전기 식을 대체 할 수

for j in range(n): 
    if j != i and j not in used_js: 
     if SequenceMatcher(None, radicals[i], radicals[j]).ratio() >= ratio : 
      matches.append(words[j]) 
      used_js.append(j) 

,이 같은 :

js = [x for x in range(n) if x != i and x not in used_js] 
for j in js: 
    if SequenceMatcher(None, radicals[i], radicals[j]).ratio() >= ratio : 
     matches.append(words[j]) 
     used_js.append(j) 
+2

항목을 생성하지 않는 반복자를 반복하면 0 반복 만 수행됩니다. for 루프는 StopIteration을 처리해야합니다. – user2357112

+1

피곤한 반복자에 대해 for 루프를 사용하면 StopIteration이 발생하지 않습니다. –

+0

맞습니다. 더 깊은 조사가 필요합니다. –

관련 문제