wordlist.txt, die eine Liste eindeutiger Wörter enthält:
Code: Select all
(word)
a
ad
and
at
Code: Select all
(word) (definition)
and congiunzione
at abbreviazione
at avverbio
Sobald ich sie gesammelt habe, unterbreche ich den Suchzyklus, da es sinnlos wäre, die dictionary.txt weiter zu durchsuchen.
Ich fahre mit der fort nächster Eintrag in wordlist.txt und so weiter.
Dies ist ein Auszug meines Codes:
Code: Select all
for wordtosearch in open("wordlist.txt", "r"):
found = 0
isfound = False
for dictionaryentry in open("dictionary.txt", "r"):
dictionaryelements = dictionaryentry.split("\t") #split the word and the definition
if wordtosearch == dictionaryelements[0]:
# ... here I gather the definition and I concatenate it to the previous one
found += 1 #at least 1 entry is found
isfound = True
else:
isfound = False
#if we don't have a match in the current cicle but we've had at least un match before, it means we can stop search further
if found > 0 and isfound == False:
break
Ich habe über das Speichern nachgedacht die Zeilennummer, in der das vorherige Wort übereinstimmte, sodass die Suche nach dem nächsten Wort in dieser Zeile von dictionary.txt beginnt und nicht am Anfang.
Wenn für das vorherige Wort keine Übereinstimmung gefunden wird, verwende ich die vorheriges dazu und so weiter.
Wäre das eine gute Lösung? Oder bietet Python etwas Besseres, das ich nicht kenne?
Ich bin übrigens nicht auf Python beschränkt, wenn Sie etwas Besseres wissen, sondern auf Windows.