2016-06-20 33 views
-1

Ich möchte eine leere Liste in Python erstellen, damit ich später durch eine Funktion Elemente hinzufügen kann. Aber als ich versuchte, Elemente über die Funktion hinzuzufügen, zeigte es mir "TypeError: Kann Tuple-Objekt nicht implizit in str konvertieren". Warum bekomme ich das?Wie erstellt man eine leere Mutable-Liste in Python, damit das Listenelement später hinzugefügt werden kann?

page = "There are many variations of passages of Lorem Ipsum available, but the majority have suffered alteration in some form, by injected humour, " \ 
     "or randomised words which don't look even slightly believable. If you are going to use a passage of Lorem Ipsum, you need to be sure there isn't " \ 
     "anything embarrassing hidden in the middle of text. All the Lorem Ipsum generators on the Internet tend to repeat predefined chunks as necessary, " \ 
     "making this the first true generator on the Internet. It uses a dictionary of over 200 Latin words, combined with a handful of model sentence " \ 
     "structures, to generate Lorem Ipsum which looks reasonable. The generated Lorem Ipsum is therefore always free from repetition, injected humour, " \ 
     "or non-characteristic words etc." 

find_word = "the" 
word_positions = [] 
pos = 0 

while page.find(find_word) != -1: 
     word_positions.append(page.find((find_word, pos))) 
     pos = pos + len(find_word) 

print(word_positions) 
+0

'page.find ((find_word, pos))' Sie erhalten das, weil Sie ein Tupel an 'find' übergeben. – freakish

Antwort

1

Im Ausdruck word_positions.append(page.find((find_word, pos))), geht page.find((find_word, pos)) ein tuple zu page.find, aber page.find erwarten das erste Argument ein String (das Wort zu finden) zu sein. Sie wollen:

page.find(find_word, pos) 

(Beachten Sie, dass ich einen Satz von Klammern fallen gelassen) sowie


Es gibt einige andere logische Fehler in Ihrem Code. Erstens könnte Ihre Schleife für immer weitergehen, weil page.find(find_word) immer etwas finden wird, wenn es beim ersten Mal etwas gefunden hat. Ändern Sie ihn auf:

while page.find(find_word, pos) != -1: 

Zweitens, werden Sie mit Dubletten in der Liste am Ende aus:

pos = pos + len(find_word) 

Die Anzahl der gefundenen Wörter hat nichts damit zu tun, welche Position Sie erwarten, dass sie finden bei . Sie möchten wahrscheinlich:

pos = word_positions[-1] + 1 

da Sie direkt nach dem letzten gefundenen Element suchen möchten.


Schließlich kann diese Aufgabe auch fast trivialen re Verwendung erreicht werden. (Sie haben nicht einmal einen regulären Ausdruck zu schreiben, da sie eine wörtliche Wort gesuchte!):

import re 
word_positions = [] 
for match in re.finditer(find_word, page): 
    word_positions.append(match.start()) 

print(word_positions) 

Beachten Sie, dass dies auch in 1 Zeile als Liste Verständnis geschrieben werden kann:

word_positions = [m.start() for m in re.finditer(find_word, page)] 
+0

Ich habe einen Satz Klammern weggelassen. Es wird jedoch nicht die Position des Wortes zur Liste hinzugefügt (word_positions) –

0

Wie wäre:

import re 

page = "There are many variations of passages of Lorem Ipsum available, but the majority have suffered alteration in some form, by injected humour, " \ 
     "or randomised words which don't look even slightly believable. If you are going to use a passage of Lorem Ipsum, you need to be sure there isn't " \ 
     "anything embarrassing hidden in the middle of text. All the Lorem Ipsum generators on the Internet tend to repeat predefined chunks as necessary, " \ 
     "making this the first true generator on the Internet. It uses a dictionary of over 200 Latin words, combined with a handful of model sentence " \ 
     "structures, to generate Lorem Ipsum which looks reasonable. The generated Lorem Ipsum is therefore always free from repetition, injected humour, " \ 
     "or non-characteristic words etc." 

find_word = "the" 
word_positions = [] 
pos = 0 

for match in re.finditer(find_word, page): 
    word_positions.append((find_word, match.start())) 

print(word_positions) 

Es gibt:

[('the', 68), ('the', 273), ('the', 317), ('the', 341), ('the', 371), ('the', 443), ('the', 471), ('the', 662)]