您的位置:首页 > 编程语言 > Python开发

【Python繁星点点】【KPoint007】Python中append和extend的区别

2018-05-27 17:42 453 查看
list.append(object) 向列表中添加一个对象object

list.extend(sequence) 把一个序列seq的内容添加到列表中

>>> test1_list = ["a", "b", "c"]
>>> test2_list = ["d", "e", "f"]
>>> test1_list
['a', 'b', 'c']
>>> test1_list.append(test2_list)
>>> test1_list
['a', 'b', 'c', ['d', 'e', 'f']]

【解释1】

test1_list.append(test2_list):test2_list指向的对象被当做一个元素,加入到test1_list列表中。

>>> test1_list.extend(test2_list)
>>> test1_list
['a', 'b', 'c', ['d', 'e', 'f'], 'd', 'e', 'f']
>>>

【解释2】

test1_list.extend(test2_list):test2_list指向的对象是一个列表,被当做一个列表,扩充到test1_list中。
阅读更多
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: