Ich bin mir nicht sicher, der Unterschied zwischen str.split ("list, ''") und str .split (Liste, '').Was ist der Unterschied zwischen str.split ("list, ''") und str.split (list, '')
Wenn ich versuche, die Codes von Learn Python The Hard Way gegeben zu bearbeiten ->
1) Blöcke mit str.split (Liste, ' ‚) #The richtige dachte ich.
ten_things = "Apples Oranges Crows Telephone Light Sugar"
stuff = str.split(ten_things, ' ')
more_stuff = ["Day", "Night", "Song", "Frisbee", "Corn", "Banana", "Girl", "Boy"]
print "Let's check our list now. \n%s" % ten_things
while len(stuff) != 10:
next_one = more_stuff.pop()
print "Adding: ", next_one
stuff.append(next_one)
print "So there's %d items now." % len(stuff)
print "There we go: ", stuff
und das Ergebnis --->
Wait there's not 10 things in that list, let's fix that.
Let's check our list now.
Apples Oranges Crows Telephone Light Sugar
Adding: Boy
So there's 7 items now.
Adding: Girl
So there's 8 items now.
Adding: Banana
So there's 9 items now.
Adding: Corn
So there's 10 items now.
There we go: ['Apples', 'Oranges', 'Crows', 'Telephone', 'Light', 'Sugar', 'Boy', 'Girl', 'Banana', 'Corn']
2) Blöcke mit str.split ("Liste ''"). # Es ist etwas falsch mit ihm sein kann.
ten_things = "Apples Oranges Crows Telephone Light Sugar"
stuff = str.split("ten_things, ' '")
more_stuff = ["Day", "Night", "Song", "Frisbee", "Corn", "Banana", "Girl", "Boy"]
print "Let's check our list now. \n%s" % ten_things
while len(stuff) != 10:
next_one = more_stuff.pop()
print "Adding: ", next_one
stuff.append(next_one)
print "So there's %d items now." % len(stuff)
und ich habe --->
Wait there's not 10 things in that list, let's fix that.
Let's check our list now.
Apples Oranges Crows Telephone Light Sugar
Adding: Boy
So there's 4 items now. #why add from 4th item?
Adding: Girl
So there's 5 items now.
Adding: Banana
So there's 6 items now.
Adding: Corn
So there's 7 items now.
Adding: Frisbee
So there's 8 items now.
Adding: Song
So there's 9 items now.
Adding: Night
So there's 10 items now.
There we go: ['ten_things,', "'", "'", 'Boy', 'Girl', 'Banana', 'Corn', 'Frisbee', 'Song', 'Night']
Es gibt 6 Produkte in ten_things aber in Zeile 5 von 2) _result, warum Python Artikel aus dem 4. eines hinzugefügt? Verstehe auch nicht die Liste in der letzten Zeile gedruckt. Könnten Sie mir bitte den Grund für diesen Fehler nennen? Vielen Dank!
Sie verwenden den exakt gleichen Code in Ihren beiden Beispielen - soll das ein Unterschied sein? –
Ihr zweites Beispiel teilt die String-Literal 'list ',' durch das Standard-Split-Zeichen, das alle Leerzeichen ist. Sie können Strings auch wie bei jeder anderen Objektmethode aufteilen. In diesem Fall ist Ihr erster Aufruf 'ten_things.split ('')' und Ihr zweiter ist '" list "" ".split()' –
Eigentlich bin ich neu hier. Vielen Dank für die Erinnerung! –