Basic usage under python list

One: Create a list

We know that in c language array can store the same data type collection of scores = [1,2,3], but in python, we can use the list,List can store a set of any data type

In [1]: name1 = 'tom'                                                   

In [2]: name2 = 'Tony'                                                  

In [3]: name3 = 'coco'                                                  

In [4]: name1                                                           
Out[4]: 'tom'

In [5]: name2                                                           
Out[5]: 'Tony'

In [6]: name3                                                           
Out[6]: 'coco'

In [7]: name = ['tom','Tony','coco']                                                                                               

In [8]: name                                                            
Out[8]: ['tom', 'Tony', 'coco']

In [9]: type(name)                                                      
Out[9]: list

Here Insert Picture Description

#列表里:可以存储不同的数据类型
li = [1,1.2,True,'hello']
print(li)
print(type(li))

#列表里也可以嵌套列表(列表:本身也是一种数据类型)
li1 = [1,1.2,True,'hello',[1,2,3,4,5]]
print(li1)
print(type(li1))

Here Insert Picture Description

Two: the list of features

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

#索引
print(service[0])
print(service[-1])

#切片
print(service[1:])
print(service[:-1])
print(service[::-1])

#重复
print(service * 3)

#连接
service1 = ['mysql','firewalld']
print(service + service1)

#成员操作符
print('firewalld' in service)
print('firewalld' in service1)

#for循环遍历
for se in service:
    print(se)

Here Insert Picture Description
Here Insert Picture Description
Here Insert Picture Description
Here Insert Picture Description

#列表里嵌套列表
service = ['http','ssh','ftp']
service1 = ['mysql','firewalld']
service1 = [['http',80],['ssh',22],['ftp',21]]
    
#索引
print(service2[1][1])
print(service2[-1][1])
    
#切片
print(service2[:][1])
print(service2[:-1][0])
print(service2[0][:-1])

Here Insert Picture Description

Three: the increase of the list

service = ['http','ssh','ftp']
#1.
print(service + ['firewalld'])

#2.append:追加一个元素到列表中
service.append('firewalld')
print(service)

#3.extend:拉伸 追加多个元素到列表中
service.extend(['mysql','firewalld'])
print(service)

#4.insert:在指定索引位置插入元素
service.insert(1,'samba')
print(service)

Here Insert Picture Description
Here Insert Picture Description
Here Insert Picture Description
Here Insert Picture Description

IV. Delete List

#pop默认弹出最后一个
In [12]: service = ['http','ssh','ftp']                                 

In [13]:                                                                

In [13]: service.pop()                                                  
Out[13]: 'ftp'

In [15]: service.pop()                                                  
Out[15]: 'ssh'

In [17]: service.pop()                                                  
Out[17]: 'http'

In [18]: service                                                        
Out[18]: [ ]

Here Insert Picture Description

# 2.remove: delete the specified element

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

Here Insert Picture Description

# 3.del keyword removed from memory

print(service)
del service
print(service)

Here Insert Picture Description

V. modify the list of

Index modifier individual
sections which can be modified

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

#通过索引,重新赋值
service[0] = 'mysql'
print(service)

#通过切片
print(service[:2])
service[:2] = ['samba','ldap']
print(service)

Here Insert Picture Description

VI. List View

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

#查看出现的次数
print(service.count('ftp'))

#查看指定元素的索引值(可以指定索引范围查看)
print(service.index('ssh'))
print(service.index('ftp',0,3))

Here Insert Picture Description

VII. Sorting the list

In [24]: names = ['alice','bob','harry','Borry']                        

In [25]: names.sort()                                                   

In [26]: names                                                          
Out[26]: ['Borry', 'alice', 'bob', 'harry']

In [27]: names.sort(key=str.lower)                                      

In [28]: names                                                          
Out[28]: ['alice', 'bob', 'Borry', 'harry']

In [29]: names.sort(key=str.upper)                                      

In [30]: names                                                          
Out[30]: ['alice', 'bob', 'Borry', 'harry']

Here Insert Picture Description

# Disrupt the original list order

import random
li = list(range(10))
print(li)
random.shuffle(li)
print(li)

Here Insert Picture Description

Guess you like

Origin blog.csdn.net/bmengmeng/article/details/94737939