[转载]Python中列表append()、extend()、"+"、"+="的区别
程序员文章站
2024-01-06 13:45:54
...
原文地址:Python中列表append()、extend()、"+"、"+="的区别
第一:添加一个元素到列表中
# encoding=utf-8
a = ["a", "b", "c"]
print "append|添加前id:%s" % id(a),
a.append("d")
print "添加后id:%s" % id(a), a
print "-"*62
a = ["a", "b", "c"]
print "extend|添加前id:%s" % id(a),
a.extend("d")
print "添加后id:%s" % id(a), a
print "-"*62
a = ["a", "b", "c"]
print " +|添加前id:%s" % id(a),
a = a + ["d"]
print "添加后id:%s" % id(a), a
print "-"*62
a = ["a", "b", "c"]
print " +=|添加前id:%s" % id(a),
a += "d"
print "添加后id:%s" % id(a), a
输出结果:
append|添加前id:40672072 添加后id:40672072 ['a', 'b', 'c', 'd']
--------------------------------------------------------------
extend|添加前id:40683080 添加后id:40683080 ['a', 'b', 'c', 'd']
--------------------------------------------------------------
+|添加前id:40672072 添加后id:40683208 ['a', 'b', 'c', 'd']
--------------------------------------------------------------
+=|添加前id:40672072 添加后id:40672072 ['a', 'b', 'c', 'd']
可以看出这四种方式都可以向列表中添加一个新元素,除了"+"之外,其他三种方式都是在原列表上添加数据,"+"则会创建一个新的列表,并且"+"只能连接两个列表,如果连接一个元素跟一个列表会报错
第二:添加一个列表到列表中
# encoding=utf-8
a = ["a", "b", "c"]
print "append|添加前id:%s" % id(a),
a.append(["d","e","f"])
print "添加后id:%s" % id(a), a
print "-"*62
a = ["a", "b", "c"]
print "extend|添加前id:%s" % id(a),
a.extend(["d","e","f"])
print "添加后id:%s" % id(a), a
print "-"*62
a = ["a", "b", "c"]
print " +|添加前id:%s" % id(a),
a = a + ["d","e","f"]
print "添加后id:%s" % id(a), a
print "-"*62
a = ["a", "b", "c"]
print " +=|添加前id:%s" % id(a),
a += ["d","e","f"]
print "添加后id:%s" % id(a), a
输出结果:
append|添加前id:40082248 添加后id:40082248 ['a', 'b', 'c', ['d', 'e', 'f']]
--------------------------------------------------------------
extend|添加前id:40093384 添加后id:40093384 ['a', 'b', 'c', 'd', 'e', 'f']
--------------------------------------------------------------
+|添加前id:40082248 添加后id:40082376 ['a', 'b', 'c', 'd', 'e', 'f']
--------------------------------------------------------------
+=|添加前id:40082248 添加后id:40082248 ['a', 'b', 'c', 'd', 'e', 'f']
append方法当添加的元素是列表时会回将整个列表添加进原列表,extend与"+="的效果是一样的,会将列表中的元素添加到原列表,"+"也会将两个列表中的元素复制到一个新创建的列表中,所不同的会创建一个新的列表
转载于:https://www.jianshu.com/p/73e7a35326a7
推荐阅读