Python的 - 主场迎战追加延长 [英] Python - append vs. extend

查看:165
本文介绍了Python的 - 主场迎战追加延长的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有什么清单方法之间的差异追加()延长()

What's the difference between the list methods append() and extend()?

推荐答案

追加 :年底对象追加

append: Appends object at end.

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

为您提供: [1,2,3,[4,5]]

延长 :从迭代追加元素扩展列表

extend: Extends list by appending elements from the iterable.

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

为您提供: [1,2,3,4,5]

这篇关于Python的 - 主场迎战追加延长的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆