python中的列表

一.列表的定義mysql

1.定義一個列表sql

li = [1,1.2,True,'hello'] print li print type(li)

2.定義一個空列表app

li=[] print li print type(li)

 

二.列表的特性ssh

1.索引spa

service = ['http', 'ssh', 'ftp'] print service[0] print service[-1]

 

 

2.切片code

 
 
service = ['http', 'ssh', 'ftp']
print
service[::-1] # 列表的翻轉 print service[1:] # 除了第一個元素以外的其餘元素 print service[:-1] # 除了最後一個元素以外的其餘元素

 

 

 

3.重複和鏈接blog

service = ['http', 'ssh', 'ftp'] print service * 3 service1 = ['mysql','firewalld'] print service + service1

 

4.成員操做符排序

service = ['http', 'ssh', 'ftp'] print 'firewalld' in service print 'firewalld' in service1 print 'firewalld' not in service

 

5.for循環遍歷列表索引

service = ['http', 'ssh', 'ftp'] print '顯示服務'.center(50,'*') for se in service: print se

 

6.列表裏能夠嵌套列表內存

service2 = [['http',80],['ssh',22],['ftp',21]] print service2,type(service2)

 

7.嵌套的索引

print service2[0][1] print service2[-1][1]

 

8.嵌套的切片

print service2[:][1] print service2[:-1][0] print service2[0][:-1]

 

三.列表的操做

1.增長

(1).輸出追加

service = ['http', 'ssh', 'ftp'] print service + ['firewalld']

 

(2).append()——適用於單個元素的添加

service = ['http', 'ssh', 'ftp'] service.append('firewalld') print service

 

(3).extend()——適用於多個元素的添加

service = ['http', 'ssh', 'ftp'] service.extend(['mysql', 'firewalld']) print service

 

(4).insert()——在指定索引位置後插入元素

service = ['http', 'ssh', 'ftp'] service.insert(1,'samab') print service

 

2.刪除

(1).pop()

service = ['http', 'ssh', 'ftp'] print service.pop() ## 若是pop()不傳遞值的時候,默認彈出最後一個元素

 

 

service = ['http', 'ssh', 'ftp'] print service.pop(1)  ## 若是給pop()傳遞值,彈出相關索引值的元素

 

(2).remove()——刪除指定元素

service = ['http', 'ssh', 'ftp'] service.remove('ssh') print service

 

(3).關鍵字del——從內存中刪除列表

service = ['http', 'ssh', 'ftp'] print service del service print service

 

3.修改

(1).經過索引從新賦值

service = ['http', 'ssh', 'ftp'] service[0] = 'mysql'
print service

 

(2).經過切片,從新賦值

service = ['http', 'ssh', 'ftp'] print service[:2] service[:2] = ['samba','ladp'] print service

 

4.查看

(1).查看元素在列表中出現的次數

service = ['http', 'ssh', 'ftp','ftp'] print service.count('ssh')

 

(2).查看指定索引值的元素

service = ['http', 'ssh', 'ftp','ftp'] print service.index('ssh')

 

5.排序

service = ['http', 'ssh', 'ftp','ftp'] service.sort() print service # 按照Ascii碼進行排序
相關文章
相關標籤/搜索