list和str比较

相同点

都属于序列类型的数据

所谓序列类型的数据,就是说它的每一个元素都可以通过指定一个编号,行话叫做“偏移量”的方式得到,而要想一次得到多个元素,可以使用切片。偏移量从0开始,总元素数减1结束。

对于此类数据,下面一些操作是类似的:

>>> first = "hello,world"
>>> welcome_str
'Welcome you'
>>> first+","+welcome_str   #用+号连接str
'hello,world,Welcome you'
>>> welcome_str             #原来的str没有受到影响,即上面的+号连接后从新生成了一个字符串
'Welcome you'
>>> first
'hello,world'

>>> language = ['python']
>>> git_list
['qiwsir', 'github', 'io']
>>> language + git_list     #用+号连接list,得到一个新的list
['python', 'qiwsir', 'github', 'io']
>>> git_list
['qiwsir', 'github', 'io']
>>> language
['python']

>>> len(welcome_str)    #得到字符数
11
>>> len(git_list)       #得到元素数
3

区别

list和str的最大区别是:list是可以改变的,str不可变。这个怎么理解呢?

首先看对list的这些操作,其特点是在原处将list进行了修改:

>>> git_list
['qiwsir', 'github', 'io']

>>> git_list.append("python")
>>> git_list
['qiwsir', 'github', 'io', 'python']

>>> git_list[1]
'github'
>>> git_list[1] = 'github.com'
>>> git_list
['qiwsir', 'github.com', 'io', 'python']

>>> git_list.insert(1,"algorithm")
>>> git_list
['qiwsir', 'algorithm', 'github.com', 'io', 'python']

>>> git_list.pop()
'python'

>>> del git_list[1]
>>> git_list
['qiwsir', 'github.com', 'io']

以上这些操作,如果用在str上,都会报错

list和str转化

str.split()

这个内置函数实现的是将str转化为list。其中str=""是分隔符。

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

例:

>>> line = "Hello.I am qiwsir.Welcome you."

>>> line.split(".")     #以英文的句点为分隔符,得到list
['Hello', 'I am qiwsir', 'Welcome you', '']

>>> line.split(".",1)   #这个1,就是表达了上文中的:If maxsplit is given, at most maxsplit splits are done.
['Hello', 'I am qiwsir.Welcome you.']

>>> name = "Albert Ainstain"    #也有可能用空格来做为分隔符
>>> name.split(" ")
['Albert', 'Ainstain']
例:

>>> s = "I am, writing\npython\tbook on line"   #这个字符串中有空格,逗号,换行\n,tab缩进\t 符号
>>> print s         #输出之后的样式
I am, writing
python  book on line
>>> s.split()       #用split(),但是括号中不输入任何参数
['I', 'am,', 'writing', 'python', 'book', 'on', 'line']

如果split()不输入任何参数,显示就是见到任何分割符号,就用其分割了。

"[sep]".join(list)

join可以说是split的逆运算,举例:

>>> name
['Albert', 'Ainstain']
>>> "".join(name)       #将list中的元素连接起来,但是没有连接符,表示一个一个紧邻着
'AlbertAinstain'
>>> ".".join(name)      #以英文的句点做为连接分隔符
'Albert.Ainstain'
>>> " ".join(name)      #以空格做为连接的分隔符
'Albert Ainstain'

回到上面那个神奇的例子中,可以这么使用join.

>>> s = "I am, writing\npython\tbook on line"
>>> print s
I am, writing
python  book on line
>>> s.split()
['I', 'am,', 'writing', 'python', 'book', 'on', 'line']
>>> " ".join(s.split())         #重新连接,不过有一点遗憾,am后面逗号还是有的。怎么去掉?
'I am, writing python book on line'

转载于:https://my.oschina.net/yangy8/blog/670269

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值