python 基礎篇(三)列表常用操作方法
阿新 • • 發佈:2019-01-01
list = ['張三','李四','王五',['hehe','haha'],'趙6'] ; # append() 新增元素到末尾 # list.append('小7') # clear() 擦除所有元素 #list.clear(); # insert(i,value) 指定索引插入 #list.insert(1,'test') # remove(value) 移除指定字串 #list.remove('張三') # pop() 預設移除最後一個元素,並且返回被刪除的元素 # pop(index) 移除指定索引的元素,並且返回被刪除的元素 # del(list[index]) 刪除列表的指定元素 # del(list) 刪除整個列表 # in 判斷表示式,列表中是否存在指定元素,存在返回true # not in ,是否不存在,是返回true # print('張三' in list) # print('張三2' not in list) # len(list) 列表長度 # print(len(list)) # max(list) 獲取最大的元素 # list = [1,8,3]; # list1 extend(list2) 列表繼承後擁有全部元素 # list2 = ['AAA'] # list2.extend(list) # list1.reverse() 反向排序 # list.sort() 按大小排序 預設升序,從小到大 print(list)