append() 方法向列表的尾部添加一个新的元素。只接受一个参数。
>>> num = [1,2]>>> num.append(3)
>>> num
[1, 2, 3]>>> num.append('a')
>>> num
[1, 2, 3, 'a']>>> num.append(6,7)
Traceback (most recent call last):
File "", line 1, in
num.append(6,7)
TypeError: append() takes exactly one argument (2 given)
>>> num.append([6])
>>> num
[1, 2, 3, 'a', [6]]>>> num.append({'a'})
>>> num
[1, 2, 3, 'a', [6], set(['a'])]
extend()方法只接受一个列表作为参数,并将该参数的每个元素都添加到原有的列表中。也是只接受一个参数。
>>> num=[1,2]>>> num.extend([5])
>>> num
[1, 2, 5]>>> num.extend(['b'])
>>> num
[1, 2, 5, 'b']>>> num.extend(6,7)
Traceback (most recent call last):
File "", line 1, in
num.extend(6,7)
TypeError: extend() takes exactly one argument (2 given)
总结
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对软件开发网的支持。如果你想了解更多相关内容请查看下面相关链接
您可能感兴趣的文章:python中append实例用法总结python列表插入append(), extend(), insert()用法详解基于pandas中expand的作用详解对python中的pop函数和append函数详解对numpy.append()里的axis的用法详解Python 创建空的list,以及append用法讲解Python.append()与Python.expand()用法详解