Python中append、extend和join的区别

转自:https://blog.csdn.net/soaringlee_fighting/article/details/78891033

 

函数用法说明:

append()

 

list.append(object) 向列表list中添加一个对象object
例子:

>>> list=[12,34,56,78,90]
>>> list
[12, 34, 56, 78, 90]
>>> list.append(45)
>>> list
[12, 34, 56, 78, 90, 45]

extend()

list.extend(sequence) 把一个序列sequence的内容添加到列表list中

 

例子:

>>> list
[12, 34, 56, 78, 90, 45]
>>> seq=[23,45,67,89]
>>> list.extend(seq)
>>> list
[12, 34, 56, 78, 90, 45, 23, 45, 67, 89]

join()

list.join:
函数:string.join()
Python中有join()和os.path.join()两个函数,具体作用如下:
    join():    连接字符串数组。将字符串、元组、列表中的元素以指定的字符(分隔符)连接生成一个新的字符串。
    os.path.join():  将多个路径组合后返回
一、函数说明
1、join()函数
语法:  'sep'.join(seq)
参数说明
sep:分隔符。可以为空
seq:要连接的元素序列、字符串、元组、字典
上面的语法即:以sep作为分隔符,将seq所有的元素合并成一个新的字符串。
返回值:返回一个以分隔符sep连接各个元素后生成的字符串 
例子:

>>> list
[12, 34, 56, 78, 90, 45, 23, 45, 67, 89]
>>> map(str,list)
['12', '34', '56', '78', '90', '45', '23', '45', '67', '89']
>>> test=map(str,list)
>>> test
['12', '34', '56', '78', '90', '45', '23', '45', '67', '89']
>>> '#'.join(test)
'12#34#56#78#90#45#23#45#67#89'
2、os.path.join()函数
语法:  os.path.join(path1[,path2[,......]])
返回值:将多个路径组合后返回
例子:
>>> os.path.join('/usr/bin/','python27')
'/usr/bin/python27'
>>> str1='/usr/bin/'
>>> str2='python27'
>>> os.path.join(str1,str2)
'/usr/bin/python27'

参考:

https://www.cnblogs.com/subic/p/6553187.html
https://www.cnblogs.com/jsplyy/p/5634640.html

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值