2016-11-12 4 views
0

Ich bin neu in der Programmierung und stecken auf meinem aktuellen Programm. Ich muss eine Geschichte aus einer Datei einlesen, die Wörter sortieren und die Anzahl der Vorkommen pro Wort zählen. Es wird die Wörter zählen, aber es wird die Wörter nicht sortieren, die Interpunktion entfernen oder Wörter duplizieren. Ich bin verloren, warum es nicht funktioniert. Jeder Rat wäre hilfreich.Sortieren und Zählen von Wörtern aus einer Textdatei

ifile = open("Story.txt",'r') 
fileout = open("WordsKAI.txt",'w') 
lines = ifile.readlines() 

wordlist = [] 
countlist = [] 

for line in lines: 
    wordlist.append(line) 
    line = line.split() 
    # line.lower() 

    for word in line: 
     word = word.strip(". , ! ? : ") 
     # word = list(word) 
     wordlist.sort() 
     sorted(wordlist) 
     countlist.append(word) 

     print(word,countlist.count(word)) 
+0

Wie möchten Sie die Wörter sortieren? alphabetisch oder nach Anzahl? – inspectorG4dget

+0

Mögliches Duplikat von [Python - Zählen von Wörtern in einer Textdatei] (http://stackoverflow.com/questions/25778341/python-counting-words-in-a-text-file) – TessellatingHeckler

Antwort

0

Sie müssen den Sortiermethoden eine Schlüsselfunktion zuweisen. Versuchen Sie, diese r = sorted(wordlist, key=str.lower)

+0

Sie müssen keine angeben Schlüssel. Es hängt davon ab, was genau du willst. –

0
punctuation = ".,!?: " 
counts = {} 
with open("Story.txt",'r') as infile: 
    for line in infile: 
     for word in line.split(): 
      for p in punctuation: 
       word = word.strip(p) 
      if word not in counts: 
       counts[word] = 0 
      counts[word] += 1 

with open("WordsKAI.txt",'w') as outfile: 
    for word in sorted(counts): # if you want to sort by counts instead, use sorted(counts, key=counts.get) 
     outfile.write("{}: {}\n".format(word, counts[word])) 
1

Es Hauptproblem in Ihrem Code auf der Linie (Linie 9):

wordlist.append(line) 

Du die ganze Zeile in die wordlist anhängt, bezweifle ich, dass das, was man ist wollen. Wenn Sie dies tun, wird das hinzugefügte Wort nicht .strip() ed, bevor es wordlist hinzugefügt wird.

Was Sie tun müssen, ist das Wort hinzuzufügen erst, nachdem Sie haben strip() es ed und stellen Sie sicher, dass Sie nur tun, nachdem Sie überprüft, dass es keine anderen gleichen Worte (keine Duplikate):

ifile = open("Story.txt",'r') 
lines = ifile.readlines() 

wordlist = [] 
countlist = [] 

for line in lines: 
    # Get all the words in the current line 
    words = line.split() 
    for word in words: 
     # Perform whatever manipulation to the word here 
     # Remove any punctuation from the word 
     word = word.strip(".,!?:;'\"") 
     # Make the word lowercase 
     word = word.lower() 

     # Add the word into wordlist only if it is not in wordlist 
     if word not in wordlist: 
      wordlist.append(word) 

     # Add the word to countlist so that it can be counted later 
     countlist.append(word) 

# Sort the wordlist 
wordlist.sort() 

# Print the wordlist 
for word in wordlist: 
    print(word, countlist.count(word)) 

Eine andere Möglichkeit, dies zu tun, ist die Verwendung eines Wörterbuchs, Speichern des Wortes als Schlüssel und die Anzahl der Vorkommen als Wert:

ifile = open("Story.txt", "r") 
lines = ifile.readlines() 

word_dict = {} 

for line in lines: 
    # Get all the words in the current line 
    words = line.split() 
    for word in words: 
     # Perform whatever manipulation to the word here 
     # Remove any punctuation from the word 
     word = word.strip(".,!?:;'\"") 
     # Make the word lowercase 
     word = word.lower() 

     # Add the word to word_dict 
     word_dict[word] = word_dict.get(word, 0) + 1 

# Create a wordlist to display the words sorted 
word_list = list(word_dict.keys()) 
word_list.sort() 

for word in word_list: 
    print(word, word_dict[word]) 
+0

Vielen Dank. Eine letzte Frage wann würde ich die Wörter in Kleinbuchstaben umwandeln? –

+0

@KennyI. Sie müssen nur irgendeine Manipulation an dem Wort vornehmen, bevor Sie '.append()' es auf die 'Wortliste' setzen. Siehe die letzte Änderung. –

Verwandte Themen