PythonTip >> 博文 >> python

Python中列表append()、extend()、"+"、"+="的区别

zihua 2014-01-16 18:01:57 点击: 722 | 收藏


第一:添加一个元素到列表中

# 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与"+="的效果是一样的,会将列表中的元素添加到原列表,"+"也会将两个列表中的元素复制到一个新创建的列表中,所不同的会创建一个新的列表

原文链接:http://my.oschina.net/u/1258442/blog/180704

作者:zihua | 分类: python | 标签: python | 阅读: 722 | 发布于: 2014-01-16 18时 |