金狮镖局 Design By www.egabc.com
在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 ret if __name__ == "__main__": print(my_split("abcded", "cd", -1)) print(my_split('Hello World! Nice to meet you', ' ', 3))
标签:
Python,split()
金狮镖局 Design By www.egabc.com
金狮镖局
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件!
如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
金狮镖局 Design By www.egabc.com
暂无Python-split()函数实例用法讲解的评论...
更新日志
2024年09月23日
2024年09月23日
- 华少翌.2005-华少翌【中唱艺能】【WAV+CUE】
- 群星《十二女声Ⅱ》[WAV/CUE/分轨][527MB]
- 大自然音乐系列《漫步在山裡》[WAV/CUE/分轨][502.8MB]
- 大自然音乐系列《星光夜语》[WAV/CUE/分轨][453.3MB]
- 史依弘《史依弘交响乐京剧演唱专辑HQ》头版限量编号[WAV+CUE]
- 曼丽2020《女人三十风继续吹》HQCD[WAV+CUE]
- 曼丽2021《女人三十2雾之恋》HQCD[WAV+CUE]
- 隔壁老樊.2019-我曾【好听音乐】【FLAC分轨】
- 高林生.1993-牵挂你的人是我【白天鹅】【WAV+CUE】
- 洪卓立.2012-找个懂我的女孩(国专)【英皇娱乐】【WAV+CUE】
- 曼丽2022《女人三十3晚秋》HQCD[WAV+CUE]
- 曼丽2018《女人三十4冬天》HQCD[WAV+CUE]
- 柏菲-《好歌30年壹》限量开盘母带ORMCD[低速原抓WAV+CUE]
- 大自然音乐系列《水舞》[WAV/CUE/分轨][424.3MB]
- 大自然音乐系列《星光夜语》[WAV/CUE/分轨][453.3MB]