日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問(wèn)題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營(yíng)銷(xiāo)解決方案
創(chuàng)新互聯(lián)Python教程:Python-split()函數(shù)用法及簡(jiǎn)單實(shí)現(xiàn)

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

split函數(shù)用法

split(sep=None, maxsplit=-1)

參數(shù)

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

maxsplit – 分割次數(shù)。默認(rèn)為 -1, 即分隔所有。

實(shí)例:

// 例子

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) 
// 也可以將字符串分割后返回給對(duì)應(yīng)的n個(gè)目標(biāo),但是要注意字符串開(kāi)頭是否存在分隔符,若存在會(huì)分割出一個(gè)空字符串
String1 = 'Hello'
String2 = 'world! Nice to meet you'

String.split('!')
// 選擇其他分隔符
['Hello world', ' Nice to meet you']

split函數(shù)實(shí)現(xiàn)

    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()函數(shù)用法及簡(jiǎn)單實(shí)現(xiàn),希望能幫助到你哦~

(推薦操作系統(tǒng):windows7系統(tǒng)、Python 3.9.1,DELL G3電腦。)


網(wǎng)頁(yè)名稱(chēng):創(chuàng)新互聯(lián)Python教程:Python-split()函數(shù)用法及簡(jiǎn)單實(shí)現(xiàn)
鏈接地址:http://www.5511xx.com/article/cojpcgp.html