這篇文章主要介紹了python中split()方法怎么用,具有一定借鑒價(jià)值,需要的朋友可以參考下。希望大家閱讀完這篇文章后大有收獲。下面讓小編帶著大家一起了解一下。
泰山ssl適用于網(wǎng)站、小程序/APP、API接口等需要進(jìn)行數(shù)據(jù)傳輸應(yīng)用場(chǎng)景,ssl證書未來(lái)市場(chǎng)廣闊!成為成都創(chuàng)新互聯(lián)的ssl證書銷售渠道,可以享受市場(chǎng)價(jià)格4-6折優(yōu)惠!如果有意向歡迎電話聯(lián)系或者加微信:028-86922220(備注:SSL證書合作)期待與您的合作!
split()方法的作用
是按指定的分隔符,把一個(gè)字符串分隔成指定數(shù)目的子字符串,然后把它們放入一個(gè)列表中,然后應(yīng)用。
實(shí)際應(yīng)用
比如有一篇文章,我們可以把它當(dāng)做一個(gè)字符串,然后用split()分隔成一個(gè)一個(gè)的單詞組成的列表,這樣我們把這個(gè)列表導(dǎo)出到Excel中,或者其他的工具中,然后就可以非常容易的統(tǒng)計(jì)出這篇文章有多少個(gè)單詞,某個(gè)單詞出現(xiàn)的次數(shù),從而可以統(tǒng)計(jì)出哪些單詞是高頻詞,然后我們可以用這些數(shù)據(jù)干很多事情。
實(shí)例展示
long_string="My name is Monkey D Lufy. The man who is going to be the king in the sea." str_list1=long_string.split() str_list2=long_string.split(" ",1) str_list3=long_string.split(" ",16) str_list4=long_string.split(" ",17) str_list5=long_string.split(" ",20) print(str_list1) print(str_list2) print(str_list3) print(str_list4) print(str_list5)
運(yùn)行結(jié)果
["My","name","is","Monkey","D","Lufy.","The","man","who","is","going","to","be","the","king","in","the","sea."] ["My","name is Monkey D Lufy. The man who is going to be the king in the sea."] ["My","name","is","Monkey","D","Lufy.","The","man","who","is","going","to","be","the","king","in","the sea."] ["My","name","is","Monkey","D","Lufy.","The","man","who","is","going","to","be","the","king","in","the","sea."] ["My","name","is","Monkey","D","Lufy.","The","man","who","is","going","to","be","the","king","in","the","sea."]
關(guān)于split()方法總結(jié)
1)split()有兩個(gè)參數(shù),第一個(gè)參數(shù)是分隔符,如果不指定,則默認(rèn)以空格、換行、制表符為分隔符,第二個(gè)參數(shù)為分隔次數(shù),如果不指定,則跟據(jù)字符串中有多少個(gè)分隔符,就分隔多少次。
2)str_list2指定空格為分隔符,只分隔1次,所以在My和name之間分隔一次,后面的都不分隔。
3)long_string這個(gè)字符串中總共有17個(gè)空格,如果指定分隔次數(shù)大于17,則Python也不會(huì)報(bào)錯(cuò),還是以分隔符的總數(shù)進(jìn)行分隔。
感謝你能夠認(rèn)真閱讀完這篇文章,希望小編分享python中split()方法怎么用內(nèi)容對(duì)大家有幫助,同時(shí)也希望大家多多支持創(chuàng)新互聯(lián),關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,遇到問題就找創(chuàng)新互聯(lián),詳細(xì)的解決方法等著你來(lái)學(xué)習(xí)!