這篇文章主要介紹了python中split()方法怎么用,具有一定借鑒價(jià)值,需要的朋友可以參考下。希望大家閱讀完這篇文章后大有收獲。下面讓小編帶著大家一起了解一下。
創(chuàng)新互聯(lián)建站2013年開創(chuàng)至今,是專業(yè)互聯(lián)網(wǎng)技術(shù)服務(wù)公司,擁有項(xiàng)目網(wǎng)站建設(shè)、網(wǎng)站制作網(wǎng)站策劃,項(xiàng)目實(shí)施與項(xiàng)目整合能力。我們以讓每一個(gè)夢(mèng)想脫穎而出為使命,1280元寶興做網(wǎng)站,已為上家服務(wù),為寶興各地企業(yè)和個(gè)人服務(wù),聯(lián)系電話:028-86922220split()方法的作用
是按指定的分隔符,把一個(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)-成都網(wǎng)站建設(shè)公司行業(yè)資訊頻道,遇到問題就找創(chuàng)新互聯(lián),詳細(xì)的解決方法等著你來學(xué)習(xí)!