# -*- coding: utf-8 -*- #創建空的list列表 list1 = [] #創建一個元素 listOne = ['hello'] listOne2 = list(['hello']) print(listOne) #['hello'] print(listOne2) #['hello'] #列表的創建(列表中可以插入類型不同的元素:int,float,str,list,tuple,dict,bool) list2 = [1, {'pi': 3.14}, (3, 'b'), 'hello', 3.14, 3e3, True] print(id(list2)) #4299951064 print(list2) #[1, {'pi': 3.14}, (3, 'b'), 'hello', 3.14, 3000.0, True] list3 = list2 #相當於給list2貼了一個list3的標籤 print(id(list3)) #4299951064 和list2一樣 list4 = list2[:] #以切片的形式會創建一塊新的內存空間 print(id(list4)) #4300021912 和list2不一樣 strTemp = 'abcdefg' list9 = strTemp.split() print(list9) #['abcdefg'] list10 = list(strTemp) #list是工廠模式 print(list10) #['a', 'b', 'c', 'd', 'e', 'f', 'g'] #修改列表元素 list2[2] = 'a' print(list2) #[1, {'pi': 3.14}, 'a', 'hello', 3.14, 3000.0, True] print(list3) #[1, {'pi': 3.14}, 'a', 'hello', 3.14, 3000.0, True] 更改list2的值,list3的也改變 print(list4) #[1, {'pi': 3.14}, (3, 'b'), 'hello', 3.14, 3000.0, True] #列表增加數據方式1,每次只能添加1個元素 list1.append('world') list1.append(23l) list1.append(23) list1.append(3.15) list1.append(23) list1.append(['a', True]) print(list1) #['world', 23L, 23, 3.15, 23, ['a', True]] #方式2,一次可以加多個元素,但參數只有一個,所有參數可以爲元組,字典,列表 list1.extend(list2) list1.extend((2, 'a')) list1.extend({'c', 9}) print(list1) #['world', 23L, 23, 3.15, 23, ['a', True], 1, 2, 'a', 'hello', 3.14, 3000.0, 2, 'a', 9, 'c'] #方式3 list1.insert(1, 'new') #['world', 'new', 23L, 23, 3.15, 23, ['a', True], 1, {'pi': 3.14}, 'a', 'hello', 3.14, 3000.0, True, 2, 'a', 9, 'c'] print(list1) # #查找某一元素的數量 print(list1.count(23)) #3 #查找某元素的索引 print(list1.index('a')) #9 print(list1.index('a', 4)) #9 , 索引區間[4, 18).不包含18 print(list1.index('a', 4, 14)) #9, 索引區間[4, 14).不包含14 #print(list1.index('a', 1, 4)) #區間查找, 找不到報錯,ValueError: 'a' is not in list #刪除列表元素和清空 list5 = ['1', 'a', 3.5, 4, 5] list5.remove('a') print(list5) #['1', 3.5, 4, 5] temp = list5.pop(2) print(temp) #4 print(list5) #['1', 3.5, 5] del(list5[0]) print(list5) #[3.5, 5] #清空,不要用list5 = [],這是新創建,如果還有變量指向原來的,原來的那塊是還存在的 del(list5) #print(str(list5)) #報錯, NameError: name 'list5' is not defined #列表比較 print(list4 < list3) #False #判斷元素在不在列表中 list6 = [1, 'a', 3.13, ['hello', 2], 4e2] print('a' in list6) #True print('hello' in list6) #False print('hello' in list6[3]) #True print('hello' not in list6) #True print('hello' not in list6[3]) #False #列表元素反序 list7 = [1, 'a', 3, 4.5, 5] list7.reverse() print(list7) #[5, 4.5, 3, 'a', 1] #列表排序(從小到大排序, 包含特殊元素沒關係, 會拍在後面, 具體位置可能隨機的) list8 = [5, 4.5, ('a', 9), {'pi': 3.14}, 'a', 3e4] list8.sort() print(list8) #[4.5, 5, 30000.0, {'pi': 3.14}, 'a', ('a', 9)] #列表長度 print(len(list8)) #6 #乘號*作爲重複器 list11 = [1, 'a', 3.3] * 3 print(list11) #[1, 'a', 3.3, 1, 'a', 3.3, 1, 'a', 3.3] list12 = [1, 2, 3.4, 4, 5, 6] print(max(list12)) #6 print(min(list12)) #1 print(list12[2]) #3.4 print(list12[-2]) #5 加個負號就相當於從後往前查找 list12[-n] == list12[len(li) - n] print(list12[-0]) #1 相當於list12[0], #所有元素單獨做處理 print([i*2 for i in list12]) #[2, 4, 6.8, 8, 10, 12]
python 列表list
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.