2017-02-27 62 views
0

我不太清楚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一个添加的项目?也不太理解在最后一行打印的列表。 你能告诉我这些错误的原因吗? 非常感谢!

+4

您在两个示例中都使用完全相同的代码 - 是否意味着有所不同? –

+0

你的第二个例子是用缺省的分割字符(全部为空格)分割字符串文字'list',''。您也可以像调用任何其他对象方法一样拆分字符串。在这种情况下,你的第一个电话是'ten_things.split('')',你的第二个电话是''list''“.split()' –

+0

其实,我是新来的。非常感谢提醒我! –

回答

0

当你有这样的代码:

ten_things = "Apples Oranges Crows Telephone Light Sugar" 
stuff = str.split(ten_things, ' ') 

然后stuff['Apples', 'Oranges', 'Crows', 'Telephone', 'Light', 'Sugar', 'Boy', 'Girl', 'Banana', 'Corn']就像你期望的那样。

但是当你运行:

ten_things = "Apples Oranges Crows Telephone Light Sugar" 
stuff = str.split("ten_things, ' '") 

然后你不使用ten_things可言,你还不如只写第二行!

您只是将字符串"ten_things, ' '"用空白分隔,并且最终将以stuff['ten_things,', "'", "'"]

+0

明白了!它只是以'[ten_things,','',''“]结尾,这就是为什么Python会从第4个开始添加的原因!万分感谢! –

0

Python的split()默认功能分割如下:

如果未指定SEP是或为无,一个不同的分割算法被应用于:连续的空白的运行被视为单一的分离器,并且结果将如果字符串具有前导或尾随空白,则在开始或结束时不包含空字符串。

因此,如果您不提供参数,则会调用默认值。

在您的代码中,您调用str.split(),这是str类的方法 - 要求先使用字符串(str对象的实例)才能工作。这就是你传递2个参数的原因。

所以回答,不同的是这个:

l = "sfrsfs" 
str.split(l, '') # l is the string instance you'd like to split, '' the separator 

但是,如果你把这个:

str.split("list, ''") 

调用split()方法上串"list, ''",并split()默认为其默认分隔器。

此外,实现str.split(my_string, '')相同my_string.split('')

+0

明白了!万分感谢! –

相关问题