python中split()方法怎么用
这篇文章主要介绍了python中split()方法怎么用,具有一定借鉴价值,需要的朋友可以参考下。希望大家阅读完这篇文章后大有收获。下面让小编带着大家一起了解一下。
泰山ssl适用于网站、小程序/APP、API接口等需要进行数据传输应用场景,ssl证书未来市场广阔!成为成都创新互联的ssl证书销售渠道,可以享受市场价格4-6折优惠!如果有意向欢迎电话联系或者加微信:028-86922220(备注:SSL证书合作)期待与您的合作!
split()方法的作用
是按指定的分隔符,把一个字符串分隔成指定数目的子字符串,然后把它们放入一个列表中,然后应用。
实际应用
比如有一篇文章,我们可以把它当做一个字符串,然后用split()分隔成一个一个的单词组成的列表,这样我们把这个列表导出到Excel中,或者其他的工具中,然后就可以非常容易的统计出这篇文章有多少个单词,某个单词出现的次数,从而可以统计出哪些单词是高频词,然后我们可以用这些数据干很多事情。
实例展示
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)
运行结果
["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."]
关于split()方法总结
1)split()有两个参数,第一个参数是分隔符,如果不指定,则默认以空格、换行、制表符为分隔符,第二个参数为分隔次数,如果不指定,则跟据字符串中有多少个分隔符,就分隔多少次。
2)str_list2指定空格为分隔符,只分隔1次,所以在My和name之间分隔一次,后面的都不分隔。
3)long_string这个字符串中总共有17个空格,如果指定分隔次数大于17,则Python也不会报错,还是以分隔符的总数进行分隔。
感谢你能够认真阅读完这篇文章,希望小编分享python中split()方法怎么用内容对大家有帮助,同时也希望大家多多支持创新互联,关注创新互联行业资讯频道,遇到问题就找创新互联,详细的解决方法等着你来学习!
网站题目:python中split()方法怎么用
文章URL:http://scjbc.cn/article/jhopsg.html