[python日誌]2019年1月10日 20:36:58
阿新 • • 發佈:2019-01-11
建立列表
>>> member = ['AAA','BB','CC','DDD'] >>> member ['AAA', 'BB', 'CC', 'DDD'] >>> number = [1,2,3,4] >>> number [1, 2, 3, 4] >>> mix = [1,'AAA',1.0011,[1,2,3]] >>> mix [1, 'AAA', 1.0011, [1, 2, 3]] >>> empty = [] >>> empty []
向列表新增元素
>>> member.append('ZZZZ') >>> member ['AAA', 'BB', 'CC', 'DDD', 'ZZZZ'] >>> len(member) 5 append同時只能新增一個 >>> member.append('11111','22222') Traceback (most recent call last): File "<pyshell#11>", line 1, in <module> member.append('11111','22222') TypeError: append() takes exactly one argument (2 given) extend的引數是列表 >>> member.extend('11111','22222') Traceback (most recent call last): File "<pyshell#12>", line 1, in <module> member.extend('11111','22222') TypeError: extend() takes exactly one argument (2 given) >>> member.extend(['11111','22222']) >>> member ['AAA', 'BB', 'CC', 'DDD', 'ZZZZ', '11111', '22222'] >>> len(member) 7 insert(位置,引數) >>> member.insert(1,'SSSSS') >>> member ['AAA', 'SSSSS', 'BB', 'CC', 'DDD', 'ZZZZ', '11111', '22222'] >>> len(member) 8 >>> member[0] 'AAA' >>> member[1] 'SSSSS' >>> temp = member[0] >>> member[0] = member[1] >>> member ['SSSSS', 'SSSSS', 'BB', 'CC', 'DDD', 'ZZZZ', '11111', '22222'] >>> member[1] = temp >>> member ['SSSSS', 'AAA', 'BB', 'CC', 'DDD', 'ZZZZ', '11111', '22222']
從列表刪除元素
remove(item)方法是直接對可變序中的元素進行檢索刪除,返回的是刪除後的列表,不返回刪除值(返回None) >>> member.remove('BB') >>> member ['SSSSS', 'AAA', 'CC', 'DDD', 'ZZZZ', '11111', '22222'] >>> member.remove('454') Traceback (most recent call last): File "<pyshell#29>", line 1, in <module> member.remove('454') ValueError: list.remove(x): x not in list >>> del(list[index])方法是對可變序列中元素下邊進行檢索刪除,不返回刪除值 >>> del member[1] >>> member ['SSSSS', 'CC', 'DDD', 'ZZZZ', '11111', '22222'] >>> mix [1, 'AAA', 1.0011, [1, 2, 3]] >>> del mix >>> mix Traceback (most recent call last): File "<pyshell#34>", line 1, in <module> mix NameError: name 'mix' is not defined >>> pop(index)方法是對可變序列中元素下標進行檢索刪除,返回刪除值 >>> member ['SSSSS', 'CC', 'DDD', 'ZZZZ', '11111', '22222'] >>> member.pop() '22222' >>> member ['SSSSS', 'CC', 'DDD', 'ZZZZ', '11111'] >>> member.pop(1) 'CC' >>> member ['SSSSS', 'DDD', 'ZZZZ', '11111'] >>>
列表分片(Slice)
下標包頭不包尾
>>> member[1:3]
['DDD', 'ZZZZ']
>>> member[:3]
['SSSSS', 'DDD', 'ZZZZ']
>>> member[0:]
['SSSSS', 'DDD', 'ZZZZ', '11111']
>>> member2 = member[:]
>>> member2
['SSSSS', 'DDD', 'ZZZZ', '11111']
>>> member
['SSSSS', 'DDD', 'ZZZZ', '11111']
>>>
列表的一些常用操作符
#比較操作符
#邏輯操作符
#連線操作符
#重複操作符
#成員關係操作符
>>> list1 = [123]
>>> list2 = [234]
>>> list1 > list2
False
>>> list1
[123]
>>> list1 = [123,456]
>>> list2 = [234,123]
>>> list1 > list2
False
>>> list3 = [123,456]
>>> (list1 < list2) and (list1 == list3)
True
#####################################################################
>>> list4 = list1 + list2
>>> list4
[123, 456, 234, 123]
#####################################################################
>>> list1 + "111"
Traceback (most recent call last):
File "<pyshell#11>", line 1, in <module>
list1 + "111"
TypeError: can only concatenate list (not "str") to list
>>> list3
[123, 456]
#####################################################################
>>> list3 * 3
[123, 456, 123, 456, 123, 456]
>>> list3
[123, 456]
>>> list3 *= 3
>>> list3
[123, 456, 123, 456, 123, 456]
#####################################################################
>>> 999 in list3
False
>>> 999 not in list3
True
>>> list5 = [123,['111','222'],456]
>>> '111' in list5
False
>>> '111' in list5[1]
True
>>> list5[1][1]
'222'
>>>
列表相關
>>> list3
[123, 456, 123, 456, 123, 456]
>>> list3.count(123)
3 #出現過三次
>>> list3.count(111)
0
>>>
>>> list3.index(456)
1
>>> list3.index(3)
Traceback (most recent call last):
File "<pyshell#30>", line 1, in <module>
list3.index(3)
ValueError: 3 is not in list
>>> list3.index(123,3,6) #查詢的物件,開始位置,結束位置
4
>>>
>>> list3.reverse()
>>> list3
[456, 123, 456, 123, 456, 123]
>>>
>>> list6 = [4,2,5,1,9,21,43,0]
>>> list6
[4, 2, 5, 1, 9, 21, 43, 0]
>>> list6.sort()
>>> list6
[0, 1, 2, 4, 5, 9, 21, 43]
>>> list6.sort(reverse=True)
>>> list6
[43, 21, 9, 5, 4, 2, 1, 0]
>>>
某大佬的sort方法詳解:https://blog.csdn.net/lianshaohua/article/details/80483357
(學習學習)
一個問題:
>>> list7 = list6
>>> list7
[43, 21, 9, 5, 4, 2, 1, 0]
>>> list8 = list6[:]
>>> list8
[43, 21, 9, 5, 4, 2, 1, 0]
>>> list6.sort()
>>> list6
[0, 1, 2, 4, 5, 9, 21, 43]
>>> list7
[0, 1, 2, 4, 5, 9, 21, 43]
>>> list8
[43, 21, 9, 5, 4, 2, 1, 0]
>>>