Python 列表方法append 和extend 有什么区别?

2023-12-20

列表方法有什么区别append() and extend()?


.append() https://docs.python.org/3/library/stdtypes.html#mutable-sequence-types将指定的对象追加到列表末尾:

>>> x = [1, 2, 3]
>>> x.append([4, 5])
>>> print(x)
[1, 2, 3, [4, 5]]

.extend() https://docs.python.org/3/library/stdtypes.html#mutable-sequence-types通过附加指定迭代中的元素来扩展列表:

>>> x = [1, 2, 3]
>>> x.extend([4, 5])
>>> print(x)
[1, 2, 3, 4, 5]
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Python 列表方法append 和extend 有什么区别? 的相关文章

随机推荐