python-列表list
1.列表的建立
2.列表的特性
3.列表元素的增加、刪除、修改、檢視
1.列表的建立
#陣列:儲存同一種資料型別的集和 scores=[1,2,33,44]
#列表(打了激素的陣列):可以儲存任意資料型別的集和
li = [1,2.2,True,'hello']
print(li,type(li))
[1, 2.2, True, 'hello'] <class 'list'>
##列表裡面是可以巢狀列表的
li = [1,2,3,False,'python',[1,2,3,4,5]] print(li,type(li)) [1, 2, 3, False, 'python', [1, 2, 3, 4, 5]] <class 'list'>
練習
隨機輸出一個亂序的列表:
import random
li = list(range(10))
# 快速生成列表
random.shuffle(li)
print(li)
2.列表的特性
索引、切片、重複、連線、成員操作符、巢狀列表、巢狀列表的索引
service = ['http','ssh','ftp']
#索引
#正向索引
print(service[0]) #輸出第一個索引值
http
#反向索引
print(service[-1]) #輸出最後一個索引值
ftp
#切片
print(service[::-1]) # 列表的反轉,倒序輸出 ['ftp', 'ssh', 'http'] print(service[1::]) # 除了第一個之外的其他元素 ['ssh', 'ftp'] print(service[:-1]) # 除了最後一個之外的其他元素 ['http', 'ssh']
#重複
print(service * 3)
['http', 'ssh', 'ftp', 'http', 'ssh', 'ftp', 'http', 'ssh', 'ftp']
#連線
service1 = ['mysql','firewalld']
print(service + service1)
['http', 'ssh', 'ftp', 'mysql', 'firewalld']
#成員操作符
print('firewalld' in service) False print('ftp' in service) True print('firewalld' not in service) True print('ftp' not in service) False
#列表裡面巢狀列表
service2 = [['http',80],['ssh',22],['ftp',21]]
#索引
print(service2[0][0]) #第一個列表中的第一個元素
http
print(service2[-1][1]) #最後一個列表的第二個元素
21
練習1:
根據用於指定月份,列印該月份所屬的季節。
提示: 3,4,5 春季 6,7,8 夏季 9,10,11 秋季 12, 1, 2 冬季
month = int(input('Month:'))
if month in [3,4,5]:
print('春季')
elif month in [6,7,8]:
print('夏季')
elif month in [9,10,11]:
print('秋季')
else:
print('冬季')
練習2:
假設有下面這樣的列表:
names = [‘fentiao’,‘fendai’,‘fensi’,‘fish’]
輸出的結果為:‘I have fentiao,fendai,fensi and fish’
names = ['fentiao', 'fendai', 'fensi', 'fish']
print('I have ' + ','.join(names[:3]) + ' and ' + names[-1])
3.列表元素的增加
-1用+[’’]的方式
-2append(‘元素’)追加一個元素
-3extend([‘元素1’,'元素2 '])追加多個元素
-4insert(索引值,‘元素’)
-1
service = ['http', 'ssh', 'ftp']
print(service + ['firewalld'])
['http', 'ssh', 'ftp', 'firewalld']
-2
#append:追加,追加一個元素到列表中
service.append('firewalld')
print(service)
['http', 'ssh', 'ftp', 'firewalld']
-3
#extend:拉伸,追加多個元素到列表中
service.extend(['hello','python'])
print(service)
['http', 'ssh', 'ftp', 'hello', 'python']
-4
#insert:在索引位置插入元素
service.insert(0,'firewalld') #在索引值為0的位置插入
print(service)
['firewalld', 'http', 'ssh', 'ftp']
4.列表的刪除
-1 pop() 彈出最後一個元素
pop(1) 彈出索引值為1的元素
-2 remove(‘元素’) 移除元素
-3 clear() 清空列表裡所有元素
-4 del(python關鍵字) 從記憶體中刪除列表
-1.pop() 彈出索引元素
In [1]: li = [1,2,3,4]
In [2]: li.pop()#彈出最後一個元素
Out[2]: 4
In [3]: li.pop(0)#彈出索引為0的元素
Out[3]: 1
In [4]:li.pop(3) #彈出索引為3的元素 ,這裡已經彈出了所以沒有索引為3的元素
---------------------------------------------------------------------------
IndexError Traceback (most recent call last)
<ipython-input-4-e9f9f299015e> in <module>()
----> 1 li.pop(3)
-2 remove()移除某元素
service = ['http', 'ssh', 'ftp']
service.remove('ftp') #移除列表中ftp
print(service)
['http', 'ssh']
service.remove('https') #列表中沒有https,輸出會報錯
print(service)
Traceback (most recent call last):
File "/home/kiosk/Documents/python/python1124/day03/09_列表元素的刪除.py", line 25, in <module>
service.remove('https')
ValueError: list.remove(x): x not in list
-3 clear()清空列表裡面的所有元素
service = ['http', 'ssh', 'ftp']
service.clear() #清空列表裡面的所有元素
print(service)
[]
-4 del(python關鍵字) 從記憶體中刪除列表
service = ['http', 'ssh', 'ftp']
del service #從記憶體中移除service,之後再列印報錯service不存在
print(service)
Traceback (most recent call last):
File "/home/kiosk/Documents/python/python1124/day03/09_列表元素的刪除.py", line 34, in <module>
print(service)
NameError: name 'service' is not defined
print('刪除列表第一個索引對應的值:',end='')
del service[1] #刪除列表第一個索引對應的值
print(service)
刪除列表第一個索引對應的值:['http', 'ftp']
print('刪除前兩個元素之外的其他元素:',end='')
del service[2:] #刪除前兩個元素之外的其他元素
print(service)
刪除前兩個元素之外的其他元素:['http', 'ssh']
5.列表元素的修改
-1索引
-2 slice切片
service = ['ftp','http', 'ssh', 'ftp']
#1通過索引,重新賦值
service[0] = 'mysql'
print(service)
['mysql', 'http', 'ssh', 'ftp']
#2通過slice(切片)
service[:2] = ['mysql','firewalld']
print(service)
['mysql', 'firewalld', 'ssh', 'ftp']
6.列表元素的檢視
-1檢視元素出現的次數
-2檢視制定元素的索引值(也可以指定範圍)
-3 排序檢視(按照ascii碼進行排序)
-4 對字串排序不區分大小寫
1檢視元素出現的次數
service = ['ftp','http','ssh','ftp']
#檢視元素出現的次數
print(service.count('ftp'))
2檢視制定元素的索引值(也可以指定範圍)
#檢視指定元素索引值 ,也可以指定[1:4]範圍
print(service.index('ftp'))
print(service.index('ftp',1,4))
3 排序檢視(按照ascii碼進行排序)
#排序檢視 按照ASCII順序
service.sort()
print(service)
#逆序排序
service.sort(reverse=True)
print(service)
4 對字串排序不區分大小寫
phones = ['alice','bob','harry','Borry']
phones.sort(key=str.lower) #輸出結果['alice', 'bob', 'Borry', 'harry']
phones.sort(key=str.upper) #輸出結果['alice', 'bob', 'Borry', 'harry']
print(phones)