python string.split()

时间:2020-02-23 14:43:21  来源:igfitidea点击:

在此Python教程中,我们将学习Python split()字符串函数。
len()不同,某些函数特定于字符串。
要使用字符串函数,请键入字符串名称,点,函数名称以及该函数所需的任何参数:string.function(arguments)。
我们可以使用内置的字符串split()函数,根据某些分隔符将字符串分成较小的字符串列表。

Python string.split()语法

符合docs.python.org使用string的语法。

split():

string.split([separator[, maxsplit]])

其中

  • 分隔符是分隔符字符串

  • 如果给定了" maxsplit",则最多完成" maxsplit"个分割(因此,列表中最多具有maxsplit + 1个元素)

  • 如果未指定maxsplit或者-1,则分割数量没有限制(进行所有可能的分割)。

  • 如果给定了'separator',则连续的定界符不会组合在一起并且被视为定界空字符串(例如''1,,2'.split(',')返回['1','',' 2']`)

  • 如果未指定'separator'或者为'None',则连续运行的'whitespace'将被视为单个分隔符,并且如果字符串的开头或者结尾处有空格,则结果在开头或者结尾将不包含空字符串。
    例如,'1 2 3'.split()返回['1','2','3']

示例1:使用空格分割字符串

在此示例脚本中,我们将使用空格作为分隔符将包含字符串的句子拆分为多个子字符串。
如果没有定义分隔符,则只需提供split()即可,默认情况下会将分隔符视为None

#!/usr/bin/env python3
mystring = "This is Python Tutorial"
print(type(mystring)) ## This will return type as string
newstring = mystring.split() ## split the string and store into newstring var
print(newstring)  ## print the content of newstring
print(type(newstring))  ## the new type would be list after splitting

该脚本的输出:

string.split()将在传递的参数上对字符串进行拆分和分割,并返回列表中的所有部分。
列表"将不包含"分割符。

示例2:使用逗号作为分隔符

在此示例中,我们将分隔符定义为逗号(),并将字符串拆分为列表

#!/usr/bin/env python3
mystring = "abc,def,ghi"
print(type(mystring)) ## This will return type as string
newstring = mystring.split(',') ## split the string using ',' and store into newstring var
print(newstring)  ## print the content of newstring
print(type(newstring))  ## the new type would be list after splitting

所以这次使用逗号分隔输出,因为我们使用了string.split(,)。
同样,我们可以使用任何其他字符来分割字符串。

示例3:定义最大拆分限制

默认情况下,如果我们未指定分割限制,则所有可能的值将从提供的字符串中切出。
在此示例中,我们将maxlimit定义为1,因此在第一次拆分后,python将忽略其余的分隔符。

#!/usr/bin/env python3
mystring = "abc,def,ghi,tre,deb"
print(type(mystring)) ## This will return type as string
## split the string using sep=',' with maxlimit=1 and store into newstring var
newstring = mystring.split(',',1)
print(newstring)  ## print the content of newstring
print(type(newstring))  ## the new type would be list after splitting

从输出中可以看到,我们的字符串被分为两部分,在第一个分隔符匹配之后,所有其他逗号都将被忽略。

示例4:计算文件中单词的出现次数

split()方法在找到空格的任何地方将字符串分成几部分,并将字符串的所有部分存储在列表中。
结果是字符串中的单词列表,尽管某些单词可能还会出现标点符号。

我们将使用split()来计算/usr/share/doc/grep/README文件中的单词数。
如果我们还不熟悉try andexcept block,则可以忽略它,而可以专注于执行实际任务的else块:

该脚本的输出:

~]# python3 count-words.py
The file /usr/share/doc/grep/README has about 372 words.

让我们用wc验证输出:

~]# wc -w /usr/share/doc/grep/README
372 /usr/share/doc/grep/README

因此,脚本和wc的输出是相同的,这意味着split()已成功分隔了单词。

示例5:使用一个带for循环的衬线拆分字符串

在此示例中,我们将使用一个线性代码来分割字符串,并打印包含4个以上字符的单词。

#!/usr/bin/env python3
mystring = 'This is a dummy text we are testing python split string'
## One-Liner
w = [[x for x in line.split() if len(x)>4] for line in mystring.split('\n')]
## Result
print(w)

其中

  • 内部列表理解表达式[x for x in line.split() if len(x)>4]使用字符串split()函数将给定行划分为单词序列。
    我们对所有单词" x"进行迭代,如果它们的字符数超过三个,则将它们添加到列表中。

  • 外部列表推导表达式创建上一条语句中使用的字符串行。
    再次,它使用split()函数在换行符'\ n'上分割mystring。

该脚本的输出:

~]# python3 one-liner-split.py
[['dummy', 'testing', 'python', 'split', 'string']]