美文网首页每天一个芝士点
extend 和 append区别联系

extend 和 append区别联系

作者: 抄书侠 | 来源:发表于2020-05-31 10:18 被阅读0次

在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扩充方法了

相关文章

网友评论

    本文标题:extend 和 append区别联系

    本文链接:https://www.haomeiwen.com/subject/vgdlzhtx.html