python基礎學習--append、extend與insert
阿新 • • 發佈:2019-02-02
append 和extend都只需要一個引數,並且自動新增到陣列末尾
如果需要新增多個,可用陣列巢狀,
但是 append是將巢狀後的陣列作為一個物件,
而 extend是將巢狀的陣列內容作為多個物件,新增到原陣列中
insert 兩個引數,指定插入的位置>>> 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']] >>>
>>> member.insert(0,'tiankong')
>>> member
['tiankong', '1', '23', '2.1', 'hehe', [1, 2, 3], 'hello', 'wuha', 'world']