国产成人精品亚洲777人妖,欧美日韩精品一区视频,最新亚洲国产,国产乱码精品一区二区亚洲

您的位置:首頁技術文章
文章詳情頁

Python-split()函數實例用法講解

瀏覽:32日期:2022-07-01 18:36:13

在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 retif __name__ == '__main__': print(my_split('abcded', 'cd', -1)) print(my_split(’Hello World! Nice to meet you’, ’ ’, 3))

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

標簽: Python 編程
相關文章:
主站蜘蛛池模板: 准格尔旗| 依兰县| 洛南县| 双峰县| 阜宁县| 蓬莱市| 政和县| 淮滨县| 保亭| 浦县| 岱山县| 多伦县| 南丹县| 蓬莱市| 曲麻莱县| 龙陵县| 定西市| 东乌| 沭阳县| 南开区| 团风县| 诸城市| 苏尼特左旗| 宜川县| 塘沽区| 静海县| 当雄县| 镇平县| 宣恩县| 禹州市| 收藏| 商丘市| 丹凤县| 南汇区| 楚雄市| 阿图什市| 乌鲁木齐市| 江西省| 余干县| 团风县| 丹巴县|