【python】split 和 join函数
一、关于split 和 join 方法
1只针对字符串进行处理。split:拆分字符串、join连接字符串
2.string.join(sep): 以string作为分割符,将sep中所有的元素(字符串表示)合并成一个新的字符串
3.string.split(str=' ',num=string.count(str)): 以str为分隔,符切片string,如果num有指定值,则仅分隔num个子字符串。
二、split()方法
help后的信息如下:

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.

中文翻译:
split(…) S.split([sep [,maxsplit]]) -> 由字符串分割成的列表 返回一组使用分隔符(sep)分割字符串形成的列表。如果指定最大分割数,则在最大分割时结束。如果分隔符未指定或者为none,则分隔符默认为空格。
实例:

1 s='a b c' 2 print s.split(' ') 3 st='hello world' 4 print st.split('o') 5 print st.split('o',1) 6 7 --------output--------- 8 ['a', 'b', 'c'] 9 ['hell', ' w', 'rld'] 10 ['hell', ' world']

注意:分隔符不能为空,否则会出错如下:
Traceback (most recent call last): File "<pyshell#12>", line 1, in <module> s.split('') ValueError: empty separator
#但是可以有不含其中的分隔符 >>> s.split('x') ['a b c'] >>> s.split('xsdfadsf') ['a b c']
二、join()
1 a='abcd' 2 print '.'.join(a) 3 print '|'.join(['a','b','c']) #可以把['a','b','c']看做是 a='abcd';下面同理 4 print '.'.join({'a':1,'b':2,'c':3,'d':4})
注意:'.'等做分隔符,将join里的所有元素(字符串)通过分隔符连接成一个新的字符串
可能有人像我一样咬文嚼字,针对string.join()的定义爱钻牛角尖,硬生生地将['a','b','c']先转换为字符串,然后在join
如:
1 b=str(['a','b','c']) 2 print '|'.join(b)
我以为这样是正解,但是不然。输出结果是:[|'|a|'|,| |'|b|'|,| |'|c|'|],而导致与上面不一致的原因就是画蛇添足了,把['a','b','c']转换成了字符串,在Python中,我们发现字符串、元祖、列 表它们是序列类型,有着相同的访问方式,可以以下标来访问其中的元素。
以上可以再敲一遍试试。
输入:
1 输出: 2 a.b.c.d 3 a|b|c 4 a.c.b.d
【python】split 和 join函数的更多相关文章
- Python中的join()函数split()函数
函数:string.join() Python中有join()和os.path.join()两个函数,具体作用如下: join(): 连接字符串数组.将字符串.元组.列表中的元素以指定的 ...
- Python中的join()函数的用法
函数:string.join() Python中有join()和os.path.join()两个函数,具体作用如下: join(): 连接字符串数组.将字符串.元组.列表中的元素以指定的字 ...
- 说明split()与join()函数的区别?
前者是切割成数组的形式,后者是将数组转换成字符串join函数获取一批字符串,然后用分隔符字符串将它们连接起来,从而返回一个字符串.Split函数获取一个字符串,然后再分隔符处将其断开,从而返回一批字符 ...
- Python中的join()函数的用法及列表推导式
[红色为转载后新增部分] 函数:string.join() Python中有join()和os.path.join()两个函数,具体作用如下: join():连接字符串数组.将字符串.元组.列表中的元 ...
- 详解Python中的join()函数的用法
函数:string.join() Python中有join()和os.path.join()两个函数,具体作用如下: join(): 连接字符串数组.将字符串.元组.列表中的元素以指定的 ...
- Python中关于join函数的陷阱?
目录 说明 数据说明 正确示例 错误示例 解决办法 说明 最近在用Python的join函数连接多个列表时,出现了如下两个错误,即合并类型不一致.折腾了很久才找到原因,真是基础不牢,地动山摇. Typ ...
- python里的Join函数
用法是将数组里的每个元素用字符连接起来 import string string.join(["aaaa", "bbb"]) 或者: from string i ...
- split和join函数的比较
关于split和join方法 处理对象字符串.split拆分字符串,join连接字符串 string.join(sep): 以string作为分隔符,将seq中的所有元素(字符串表示)合并成一个新的字 ...
- python-CSV格式清洗与转换、CSV格式列变换、CSV格式数据清洗【数据读入的三种方法】【strip、replace、split、join函数的使用】
1)CSV格式清洗与转换 描述 附件是一个CSV格式文件,提取数据进行如下格式转换: (1)按行进行倒序排列: (2)每行数据倒序排 ...
随机推荐
- [LeetCode] 35. Search Insert Position ☆(丢失的数字)
转载:https://leetcode.windliang.cc/leetCode-35-Search-Insert-Position.html 思路 Given a sorted array ...
- python 利用quick sort思路实现median函数
# import numpy as np def median(arr): #return np.median(arr) arr.sort() return arr[len(arr)>>1 ...
- 利用padding-top/padding-bottom百分比,进行占位和高度自适应
在css里面,padding-top,padding-bottom,margin-top,margin-bottom取值为百分比的时候,参照的是父元素的宽度. 比如:父元素宽度是100px, 子元素p ...
- James Whittaker:经营成功的测试职业生涯
转注:这篇文章出自 James A. Whittaker,Google的工程总监,负责Google部分产品的测试,包括Chrome.地图.GoogleWebApp.在加盟Google之前,James在 ...
- 关于这个 SDK.InvalidRegionId : Can not find endpoint to access
String product = "Dysmsapi";//短信API产品名称(短信产品名固定,无需修改) TM 调试了半天,一直报错.原来是因为我改了上面的 Dysm ...
- EhLib TitleButton SVisibleColumnsEh = '错误的列';
unit EhLibConsts; interface resourcestring SClearSelectedCellsEh = '清除选择的单元?'; SInvalidTextFormatEh ...
- Spring MVC和Spring Data JPA之获取数据表数据放在List集合,显示在JSP页面
涉及到很多xml配置没写:只写具体实现的所有类 1.实体类 对应数据表SYS_SBGL, 主键是SBBM,主键是自动生成的uuid 数据表内容如下(有图有真相): package com.jinhet ...
- 关于命名空间的using声明
1.std::cin.std::cout 意思就是编译器通过作用域运算符左边的作用域,去找右边的名字.有点繁琐. 2.可以使用using声明:using namespace::name; 例如,usi ...
- AngularJS----ngModelController
文档地址:https://docs.angularjs.org/api/ng/type/ngModel.NgModelController 首先声明:DOM value 与view value是完全不 ...
- HDU 1548 A strange lift(BFS)
Problem Description There is a strange lift.The lift can stop can at every floor as you want, and th ...