In [19]: service = ['http','ssh','ftp','dns']
In [20]: service.pop()
Out[20]: 'dns'
In [21]: service
Out[21]: ['http', 'ssh', 'ftp']
In [22]: a = service.pop()
In [23]: service
Out[23]: ['http', 'ssh']
In [24]: a
Out[24]: 'ftp'
In [25]: service.pop(0)
Out[25]: 'http'
In [26]: service
Out[26]: ['ssh']
In [27]: service.pop()
Out[27]: 'ssh'
In [28]: service.pop()
------------------------------------------------------------------------
IndexError Traceback (most recent call last)
<ipython-input-28-d8278ea86206> in <module>
----> 1 service.pop()
IndexError: pop from empty list
#remove:删除指定元素
In [30]: service = ['http','ssh','ftp','dns']
In [31]:
In [31]: a = service.remove('ssh')
In [32]: print(service)
['http', 'ftp', 'dns']
In [33]: print(a)
None
In [34]: print(service)
['http', 'ftp', 'dns']
#从内存中删除列表
In [35]: del service
In [36]: print(service)
service = ['ftp','ssh','ftp','dns']
#查看出现的次数
print(service.count('ftp'))
print(service.count('dns'))
#查看指定元素的索引值(可以指定索引范围查看)
print(service.index('ssh'))
print(service.index('ftp',1,4))
列表的排序
service = ['ftp','ssh','ftp','dns']
service.sort()
print(service)
import random
li = list(range(10))
print(li)
#讲原有的列表顺序打乱
random.shuffle(li)
print(li)