我不太清楚str.split之间的差异(‘名单‘’’)和海峡.split(list,'')。什么str.split之间的差异(“名单‘’”)和str.split(列表,'“)
当我尝试编辑通过学习Python的硬地给出的代码 - >
1)块与str.split(列表,'“)#The合适的人我想。
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
和结果--->
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)str.split块( “名单 ''”)。#可能有什么不妥的地方。
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)
和我--->
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']
有在ten_things 6项,但在2)_result线5,为什么蟒蛇从4一个添加的项目?也不太理解在最后一行打印的列表。 你能告诉我这些错误的原因吗? 非常感谢!
您在两个示例中都使用完全相同的代码 - 是否意味着有所不同? –
你的第二个例子是用缺省的分割字符(全部为空格)分割字符串文字'list',''。您也可以像调用任何其他对象方法一样拆分字符串。在这种情况下,你的第一个电话是'ten_things.split('')',你的第二个电话是''list''“.split()' –
其实,我是新来的。非常感谢提醒我! –