日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python列表split_Python-split()函数实例用法讲解

發布時間:2023/12/10 python 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python列表split_Python-split()函数实例用法讲解 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

在Python中,split() 方法可以實現將一個字符串按照指定的分隔符切分成多個子串,這些子串會被保存到列表中(不包含分隔符),作為方法的返回值反饋回來。

split函數用法

split(sep=None, maxsplit=-1)

參數

sep – 分隔符,默認為所有的空字符,包括空格、換行(\n)、制表符(\t)等。

maxsplit – 分割次數。默認為 -1, 即分隔所有。

實例:

// 例子

String = 'Hello world! Nice to meet you'

String.split()

['Hello', 'world!', 'Nice', 'to', 'meet', 'you']

String.split(' ', 3)

['Hello', 'world!', 'Nice', 'to meet you']

String1, String2 = String.split(' ', 1)

// 也可以將字符串分割后返回給對應的n個目標,但是要注意字符串開頭是否存在分隔符,若存在會分割出一個空字符串

String1 = 'Hello'

String2 = 'world! Nice to meet you'

String.split('!')

// 選擇其他分隔符

['Hello world', ' Nice to meet you']

split函數實現

def split(self, *args, **kwargs): # real signature unknown

"""

Return a list of the words in the string, using sep as the delimiter string.

sep

The delimiter according which to split the string.

None (the default value) means split according to any whitespace,

and discard empty strings from the result.

maxsplit

Maximum number of splits to do.

-1 (the default value) means no limit.

"""

pass

上圖為Pycharm文檔

def my_split(string, sep, maxsplit):

ret = []

len_sep = len(sep)

if maxsplit == -1:

maxsplit = len(string) + 2

for _ in range(maxsplit):

index = string.find(sep)

if index == -1:

ret.append(string)

return ret

else:

ret.append(string[:index])

string = string[index + len_sep:]

ret.append(string)

return ret

if __name__ == "__main__":

print(my_split("abcded", "cd", -1))

print(my_split('Hello World! Nice to meet you', ' ', 3))

到此這篇關于Python-split()函數實例用法講解的文章就介紹到這了,更多相關Python-split()函數用法及簡單實現內容請搜索腳本之家以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持腳本之家!

總結

以上是生活随笔為你收集整理的python列表split_Python-split()函数实例用法讲解的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。