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

相關文章
相關標籤/搜索