在python中,向列表中添加新元素有extend和append这两种方法,但是这两种方法究竟有什么区别和联系呢?
首先看extend的作用:
The extend() method adds all the elements of an iterable (list, tuple, string etc.) to the end of the list.
举个例子:
list1.extend(iterable)
是把iterable中所有的的元素都加到了list1的末尾
而append:
The append() method adds an item to the end of the list.
list.append(item)
会将item这一个元素添加到list中
此外
a=[1,2]
b=[3,4]
c=a+b
#>>>c=[1,2,3,4]
这就是python中特殊的list扩充方法了
网友评论