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

猜你喜欢

转载自blog.csdn.net/m0_37443131/article/details/81217545