Python--关于 join 和 split
.join()
join将 容器对象 拆分并以指定的字符将列表内的元素(element)连接起来,返回字符串(注:容器对象内的元素须为字符类型)
>>> a = ['no','pain','no','gain']
>>> '_ '.join(a)
'no_pain_no_gain'
>>>
注:容器对象内的元素须为字符类型
>>> b = ['I','am','no',]
>>> '_'.join(b)
Traceback (most recent call last):
File "<pyshell#32>", line 1, in <module>
'_'.join(b)
TypeError: sequence item 3: expected string, int found
>>>
dict是以Key值作连接
>>> L = {'p':'P','y':'Y','t':'T','h':'H','o':'O','n':'N'}
>>> '_'.join(L)
'h_o_n_p_t_y' #dict 的无序性,使元素随机连接。set 同理
>>>
.split()
与join相反,split以指定的字符将字符串分割为单个元素(字符类型)并加入list中,返回一个List
>>> a = 'no_pian_no_gain'
>>> a.split('_')
['no', 'pian', 'no', 'gain']
>>>
split是可以设定切割多少个字符的
>>> a = 'no_pian_no_gain'
>>> a.split('_',2)
['no', 'pian', 'no_gain']
>>> a.split('_',1)
['no', 'pian_no_gain']
>>> a.split('_',0)
['no_pian_no_gain']
>>> a.split('_',-1)
['no', 'pian', 'no', 'gain']
>>>
可见split('_')与split('_',-1)返回的结果是一致的
Python--关于 join 和 split的更多相关文章
- python中join和split函数
一个是分割,一个是连接. 惯例,先看内部帮助文档 Help on method_descriptor: join(...) S.join(iterable) -> string Return a ...
- Python中join 和 split详解(推荐)
http://www.jb51.net/article/87700.htm python join 和 split方法简单的说是:join用来连接字符串,split恰好相反,拆分字符串的. .join ...
- 解释python中join()和split()函数
join能让我们将指定字符添加至字符串中 a=') print(a) print(type(a)) #1,2,3,4,5,6 #<class 'str'> split()能让我们用指定字符 ...
- Python中的join()函数split()函数
函数:string.join() Python中有join()和os.path.join()两个函数,具体作用如下: join(): 连接字符串数组.将字符串.元组.列表中的元素以指定的 ...
- python join与split函数的用法举例
python join 和 split方法: join用来连接字符串,split恰好相反,拆分字符串的. 来看有关join.split方法的例子 1,join用法的例子 复制代码 代码示例: > ...
- python join和split和strip用法
python join 和 split方法的使用,join用来连接字符串,split恰好相反,拆分字符串的. strip()为去除开头结尾指定的字符,空着时是去除空白字符\t,\n,\r意思 1.jo ...
- python join 和 split的常用使用方法
函数:string.join()Python中有join()和os.path.join()两个函数,具体作用如下: join(): 连接字符串数组.将字符串.元组.列表中的元素以指定的字符 ...
- Python基本知识 os.path.join与split() 函数
Python中有join和os.path.join()两个函数,具体作用如下: join:连接字符串数组.将字符串.元组.列表中的元素以指定的字符(分隔符)连接生成一个新的字符串os.path.joi ...
- 【转】关于python中re模块split方法的使用
注:最近在研究文本处理,需要用到正则切割文本,所以收索到了这篇文章,很有用,谢谢原作者. 原址:http://blog.sciencenet.cn/blog-314114-775285.html 关于 ...
- python中join()函数的使用方法
函数:string.join() Python中有join()和os.path.join()两个函数,具体作用如下: join(): 连接字符串数组.将字符串.元组.列表中的元素以指定的字 ...
随机推荐
- Application 应用程序对象使用介绍
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="DemoApplicatio ...
- 如何解决Angular 2 的templateUrl和styleUrl的路径问题?
参考地址:https://github.com/kittencup/angular2-ama-cn/issues/18 前言: templateUrl表示的是组件在浏览器中运行时依赖的模板地址,所以在 ...
- CentOS最小化安装后,增加GNOME桌面
背景:下载CentOS 7的安装包后,在虚拟机上安装. 上来就遇到一个问题:提示需要开启intel vt-x. 这个进入BIOS,在CPU的设置中开启即可. 然后怀着兴奋的心情,开始各种下一步的安装, ...
- VPython—旋转坐标系
使用arrow( )创建三个坐标轴代表一个坐标系,其中X0-Y0-Z0为参考坐标系(固定不动),X-Y-Z为运动坐标系,这两个坐标系原点重合,运动坐标系可以绕参考坐标系或其自身旋转.在屏幕上输出一个转 ...
- deep-learning-frameworks
From: http://venturebeat.com/2015/11/14/deep-learning-frameworks/ Here’s a rundown of some other not ...
- js数组去重的三种常用方法总结
第一种是比较常规的方法 思路: 1.构建一个新的数组存放结果 2.for循环中每次从原数组中取出一个元素,用这个元素循环与结果数组对比 3.若结果数组中没有该元素,则存到结果数组中 Array.p ...
- [转自老马的文章]用MODI OCR 21种语言
作者:马健邮箱:stronghorse_mj@hotmail.com发布:2007.12.08更新:2012.07.09按照<MODI中的OCR模块>一文相关内容进行修订2012.07.0 ...
- poj 3348--Cows(凸包求面积)
链接:http://poj.org/problem?id=3348 Cows Time Limit: 2000MS Memory Limit: 65536K Total Submissions: ...
- POJ 1979 Red and Black (红与黑)
POJ 1979 Red and Black (红与黑) Time Limit: 1000MS Memory Limit: 30000K Description 题目描述 There is a ...
- hdu 3033 I love sneakers! 分组背包
I love sneakers! Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...