Python中join和split用法实例

yipeiwu_com6年前Python基础

join用来连接字符串,split恰好相反,拆分字符串的。
不用多解释,看完代码,其意自现了。

复制代码 代码如下:

>>>li = ['my','name','is','bob']
>>>' '.join(li)
'my name is bob'
>>>s = '_'.join(li)
>>>s
'my_name_is_bob'
>>>s.split('_')
['my', 'name', 'is', 'bob']

其join和split的英文版解释如下:

join(...)
S.join(sequence) -> string

Return a string which is the concatenation of the strings in the
sequence.  The separator between elements is S.

split(...)
S.split([sep [,maxsplit]]) -> list of strings

Return a list of the words in the string S, using sep as the
delimiter string.  If maxsplit is given, at most maxsplit
splits are done. If sep is not specified or is None, any
whitespace string is a separator and empty strings are removed
from the result.

相关文章

Python实现读取并保存文件的类

本文实例讲述了Python实现读取并保存文件的类。分享给大家供大家参考,具体如下: 这个类写在一个叫class_format.py 的文件里, 放在D盘 >>> i...

pytorch中如何使用DataLoader对数据集进行批处理的方法

pytorch中如何使用DataLoader对数据集进行批处理的方法

最近搞了搞minist手写数据集的神经网络搭建,一个数据集里面很多个数据,不能一次喂入,所以需要分成一小块一小块喂入搭建好的网络。 pytorch中有很方便的dataloader函数来方...

Python中让MySQL查询结果返回字典类型的方法

Python的MySQLdb模块是Python连接MySQL的一个模块,默认查询结果返回是tuple类型,只能通过0,1..等索引下标访问数据 默认连接数据库: 复制代码 代码如下: M...

Python之父谈Python的未来形式

6月初,Python之父Guido van Rossum在今天的PyCon US大会上作了名为“Python Language”的演讲。近日,他又接受了IT媒体Infoworld的采访,...

python skimage 连通性区域检测方法

涉及到的函数为 import matplotlib.pyplot as plt from skimage import measure, color labels = measure...