2017-04-03 3 views
3

Ich habe einige Probleme mit einem Code. Ich versuche, wiederholte Wörter in einer Datei zu finden, wie zum Beispiel "the the", und drucke dann die Zeile, auf der es passiert. Bis jetzt funktioniert mein Code für die Zeilenzählung, aber gibt mir alle Wörter, die in der ganzen Datei wiederholt werden und nicht nur die direkt nach dem anderen. Was muss ich ändern, damit es nur die doppelten Wörter zählt?So finden Sie doppelte Wörter in Datei

my_file = input("Enter file name: ") 
lst = [] 
count = 1 
with open(my_file, "r") as dup: 
for line in dup: 
    linedata = line.split() 
    for word in linedata: 
     if word not in lst: 
      lst.append(word) 
     else: 
      print("Found word: {""} on line {}".format(word, count)) 
      count = count + 1 
dup.close() 
+0

nur die 'lst zurückgesetzt = []' bei jeder Zeile Iteration. –

+0

@ Jean-FrançoisFabre, die doppelte Wörter in der Zeile erkennen würde, nicht nur benachbarte. – Maciek

Antwort

0

hier nur die reine Antwort auf die Frage gestellt:

„Was muss ich also nur die verdoppelten Wörter zählt es ändern?“

Sie sind hier:

my_file = input("Enter file name: ") 
count = 0 
with open(my_file, "r") as dup: 
for line in dup: 
    count = count + 1 
    linedata = line.split() 
    lastWord = '' 
    for word in linedata: 
     if word == lastWord: 
      print("Found word: {""} on line {}".format(word, count)) 
     lastWord = word 
dup.close() 
1
my_file = input("Enter file name: ") 
with open(my_file, "r") as dup: 
    for line_num, line in enumerate(dup): 
     words_in_line = line.split() 
     duplicates = [word for i, word in enumerate(words_in_line[1:]) if words_in_line[i] == word] 
     # now you have a list of duplicated words in line in duplicates 
     # do whatever you want with it 
+1

Es sollte 'words_in_line [i]' da enumerate bereits bei 0 beginnt;) – swenzel

+0

@swenzel du hast recht, danke! Es wurde jetzt behoben. – Maciek

0

Setzen Sie den Code unten in einer Datei mit dem Namen THISfile.py und führen Sie es aus, um zu sehen, was funktioniert:

# myFile = input("Enter file name: ") 
# line No 2: line with with double 'with' 
# line No 3: double (word , word) is not a double word 
myFile="THISfile.py" 
lstUniqueWords = [] 
noOfFoundWordDoubles = 0 
totalNoOfWords  = 0 
lineNo    = 0 
lstLineNumbersWithWordDoubles = [] 
with open(myFile, "r") as myFile: 
    for line in myFile: 
     lineNo+=1 # memorize current line number 
     lineWords = line.split() 
     if len(lineWords) > 0: # scan line only if it contains words 
      currWord = lineWords[0] # remember already 'visited' word 
      totalNoOfWords += 1 
      if currWord not in lstUniqueWords: 
       lstUniqueWords.append(currWord) 
       # put 'visited' word word into lstAllWordsINmyFile (if it is not already there) 
      lastWord = currWord # we are done with current, so current becomes last one 
      if len(lineWords) > 1 : # proceed only if line has two or more words 
       for word in lineWords[1:] : # loop over all other words 
        totalNoOfWords += 1 
        currWord = word 
        if currWord not in lstUniqueWords: 
         lstUniqueWords.append(currWord) 
         # put 'visited' word into lstAllWordsINmyFile (if it is not already there) 
        if(currWord == lastWord): # duplicate word found: 
         noOfFoundWordDoubles += 1 
         print("Found double word: ['{""}'] in line {}".format(currWord, lineNo)) 
         lstLineNumbersWithWordDoubles.append(lineNo) 
        lastWord = currWord 
        #  ^--- now after all all work is done, the currWord is considered lastWord 
print(
    "noOfDoubles", noOfFoundWordDoubles, "\n", 
    "totalNoOfWords", totalNoOfWords, "uniqueWords", len(lstUniqueWords), "\n", 
    "linesWithDoubles", lstLineNumbersWithWordDoubles 
) 

Die Ausgabe sollte:

Found double word: ['with'] in line 2 
Found double word: ['word'] in line 19 
Found double word: ['all'] in line 33 
noOfDoubles 3 
totalNoOfWords 221 uniqueWords 111 
linesWithDoubles [2, 19, 33] 

Jetzt Sie können die Kommentare im Code lesen, um besser zu verstehen, wie es funktioniert. Viel Spaß Codierung :)

Verwandte Themen