1. 程式人生 > >python基礎學習--append、extend與insert

python基礎學習--append、extend與insert

append 和extend都只需要一個引數,並且自動新增到陣列末尾

如果需要新增多個,可用陣列巢狀,

但是  append是將巢狀後的陣列作為一個物件,

而       extend是將巢狀的陣列內容作為多個物件,新增到原陣列中

>>> member = ['1','23','2.1','hehe',[1,2,3]]
>>> 
>>> member
['1', '23', '2.1', 'hehe', [1, 2, 3]]
>>> member.append("hello")
>>> member
['1', '23', '2.1', 'hehe', [1, 2, 3], 'hello']
>>> member.extend(['wuha','world'])
>>> member
['1', '23', '2.1', 'hehe', [1, 2, 3], 'hello', 'wuha', 'world']
>>> member.append(['nihao','wohao'])
>>> member
['tiankong', '1', '23', '2.1', 'hehe', [1, 2, 3], 'hello', 'wuha', 'world', ['nihao', 'wohao']]
>>> 
insert 兩個引數,指定插入的位置
>>> member.insert(0,'tiankong')
>>> member
['tiankong', '1', '23', '2.1', 'hehe', [1, 2, 3], 'hello', 'wuha', 'world']