Python里的append和extend

append()用法示例:    
>>> mylist = [1,2,0,'abc']    
>>> mylist    
[1, 2, 0, 'abc']    
>>> mylist.append(4)    
>>> mylist    
[1, 2, 0, 'abc', 4]    
>>> mylist.append('haha')    
>>> mylist    
[1, 2, 0, 'abc', 4, 'haha']
extend()用法示例:    
>>> mylist    
[1, 2, 0, 'abc', 4, 'haha']    
>>> mylist.extend(['lulu'])    
>>> mylist    
[1, 2, 0, 'abc', 4, 'haha', 'lulu']    
>>> mylist.extend([aaa,'lalalala'])    
Traceback (most recent call last):    
  File "<stdin>", line 1, in <module>    
NameError: name 'aaa' is not defined    
>>> mylist.extend(['123123','lalalala'])    
>>> mylist    
[1, 2, 0, 'abc', 4, 'haha', 'lulu', '123123', 'lalalala']    
>>> mylist.extend([111111,222])    
>>> mylist    
[1, 2, 0, 'abc', 4, 'haha', 'lulu', '123123', 'lalalala', 111111, 222]    



看到了吗?append()只能在list中添加一个元素;extend()只能添加另外一个list。

你可能感兴趣的:(Python里的append和extend)