Python中join和split用法实例

yipeiwu_com5年前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 二叉树的层序建立与三种遍历实现详解

前言 二叉树(Binary Tree)时数据结构中一个非常重要的结构,其具有。。。。(此处省略好多字)。。。。等的优良特点。 之前在刷LeetCode的时候把有关树的题目全部跳过了,(O...

Python编写带选项的命令行程序方法

运行python程序时,有时需要在命令行传入一些参数。常见的方式是在执行时,在脚本名后直接追加空格分隔的参数列表(例如 python test.py arg0 arg1 arg2),然后...

浅谈对pytroch中torch.autograd.backward的思考

反向传递法则是深度学习中最为重要的一部分,torch中的backward可以对计算图中的梯度进行计算和累积 这里通过一段程序来演示基本的backward操作以及需要注意的地方 >...

python中类变量与成员变量的使用注意点总结

前言 最近在用python写一个项目,发现一个很恶心的bug,就是同由一个类生成的两个实例之间的数据竟然会相互影响,这让我非常不解。后来联想到java的类有类变量也有实例变量,因此翻阅了...

Python绘制3d螺旋曲线图实例代码

Python绘制3d螺旋曲线图实例代码

Line plots Axes3D.plot(xs, ys, *args, **kwargs) 绘制2D或3D数据 参数 描述...