一、添加的数据类型不同
list.append(object)
list.extend(sequence)
append() 可以添加一切数据类型,Python万物皆对象
extend() 只能添加可迭代对象
a = [1, 2, 3]
b = 4
c = [5, 6, 7]
d = "s"
e = "svip"
f = ["hehe", "haha"]
g = ("hua", "guo")
h = {"name": "xiaoming"}
a.append(b)
a.append(c)
a.append(d)
a.append(e)
a.append(f)
a.append(g)
a.append(h)
print a
a.extend(b)
Traceback (most recent call last):
File "/Users/gaoyueyun/Documents/code/machine_test/practice_test.py", line 97, in <module>
a.extend(b)
TypeError: 'int' object is not iterable
二、添加后结果不同
list.append(object)
list.extend(sequence)
a = [1, 2, 3]
c = [5, 6, 7]
d = "s"
g = ("hua", "guo")
h = {"name": "xiaoming"}
1、添加列表
a.append(c)
print a
a.extend(c)
print a
2、添加字符串
a.append(d)
print a
a.extend(d)
print a
a.append(e)
print a
a.extend(e)
print a
3、添加元组
a.append(g)
print a
a.extend(g)
print a
4、添加字典
a.append(h)
print a
a.extend(h)
print a
extend()添加字典时只能添加到字典的“key”
三、都是对原有列表进行扩容,没有返回值,无法赋值给变量
a = [1, 2, 3]
b = 4
c = [5, 6, 7]
d = "s"
e = "svip"
f = ["hehe", "haha"]
g = ("hua", "guo")
h = {"name": "xiaoming"}
print id(a)
a.append(h)
print id(a)
a.extend(h)
print id(a)
print a.append(c)
print a.extend(c)
t = a.append(c)
print t
s = a.extend(c)
print s
四、参考文档
1、https://www.cnblogs.com/subic/p/6553187.html
2、https://www.cnblogs.com/wushuaishuai/p/7729011.html
3、https://www.cnblogs.com/xuchunlin/p/5479119.html
|