列表:列表是python中最常用的數據類型,列表中的數據不需要具有相同的數據類型,它是一個有序的序列
1.列表的創建
空列表的創建:
>>> li = []
>>> print(type(li))
<class 'list'>
>>> li1 = list()
>>> print(type(li1))
<class 'list'>
注意: 用此方式創建的空列表,長度爲0,初始化時不能直接通過下標賦值,
即:li[0] = 'test',只能通過append()或extend()添加元素
創建並初始化列表:
此列表可以在索引範圍內對列表元素進行賦值,但是超過索引即給列表中添加新的元素需要使用append()或extend()
li = [None] * 5
li1 = ['hello', 'python', 'hello']
# 列表裏也可以嵌套列表
>>> list2 = [1,2,3,4,[1,1.2,True,'python']]
>>> print(list2, type(list2))
[1, 2, 3, 4, [1, 1.2, True, 'python']] <class 'list'>
>>> print(list2[-1])
[1, 1.2, True, 'python']
2.列表的特性
1.索引
>>> service = ['http','ftp','ssh']
正向索引
>>> print(service[0])
>>> print(service[0])
http
反向索引
>>> print(service[-1])
ssh
2.切片
>>> print(service[::-1])
['ssh', 'ftp', 'http']
>>> print(service[1:])
['ftp', 'ssh']
>>> print(service[:1])
['http']
>>> print(service[:-1])
['http', 'ftp']
3.重複
>>> print(service * 2)
['http', 'ftp', 'ssh', 'http', 'ftp', 'ssh']
4.成員操作符
>>> print('http' in service)
True
>>> print('https' in service)
False
5.for 循環
>>> for i in service:
... print(i)
...
http
ftp
ssh
練習:
輸入某年某月某日(yyyy-MM-dd),判斷這一天是這一年的第幾天?
實現:
s = input('Please input Year-Month-Day:')
date = s.split('-')
year = int(date[0])
month = int(date[1])
day = int(date[2])
ard = [0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
if ((year % 4 == 0 and year % 100 != 0) or (year % 400 == 0)):
ard[2] = 29
num = 0
for i in range(1, len(ard)):
if month > i:
num += ard[i]
else:
num += day
print(num)
運行結果:
3.列表元素的增加
1.append()
>>> service = ['http','ftp','ssh']
append()將單個元素追加到列表末尾
>>> service.append('nfs')
>>> service
['http', 'ftp', 'ssh', 'nfs']
2.extend()
extend()將多個元素追加到列表末尾
>>> service.extend(['mariadb', 'crontab'])
>>> service
['http', 'ftp', 'ssh', 'nfs', 'mariadb', 'crontab']
3.insert()
insert():在指定索引處插入元素
>>> service.insert(2, 'dns')
>>> service
['http', 'ftp', 'dns', 'ssh', 'nfs', 'mariadb', 'crontab']
4.列表元素的刪除
1.pop()
>>> service = ['http','ftp','ssh']
pop()默認刪除列表中最後一個元素並返回此元素,當有參數傳遞時,會刪除列表中指定下標的元素
>>> service = ['http','ftp','ssh']
>>> service.pop()
'ssh'
>>> service
['http', 'ftp']
>>> service.pop(0)
'http'
>>> service
['ftp']
2.remove()
remove()刪除列表中指定的元素,且不返回此元素
>>> service = ['http','ftp','ssh']
>>> service.remove('ssh')
>>> service
['http', 'ftp']
3.在內存中刪除一個元素:del
>>> service = ['http','ftp','ssh']
>>> del service[1]
>>> service
['http', 'ssh']
5.列表元素的查看
1.count()
service = ['http','ftp','ssh','mysql','ssh','http']
count()統計列表中指定元素的數目,元素存在則返回元素數目,元素不存在則返回0
>>> service.count('ssh')
2
>>> service.count('sshs')
0
2.index()
index()查看指定元素的索引值,返回所查找元素的第一個索引值,元素不存在則報錯
當指定搜索範圍時,只在搜索範圍內查找,傳遞的參數的是元素的下標範圍
>>> service.index('ssh')
2
>>> service.index('sshs')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: 'sshs' is not in list
>>> service.index('ssh', 3, 5)
4
6.列表元素的排序
1.sort() & sotred()
sort()默認按照首字母的ascii碼值進行升序排列
sort()與sorted()的區別:
sorted()函數不會改變原列表的內容,執行完畢後返回一個新的列表;
sort()方法會改變原列表的內容,執行完畢後返回none
>>> service = ['ftp','ssh','http','mysql','http','ssh']
>>> sorted(service)
['ftp', 'http', 'http', 'mysql', 'ssh', 'ssh']
>>> service
['ftp', 'ssh', 'http', 'mysql', 'http', 'ssh']
>>> print(service.sort())
None
>>> service.sort()
>>> service
['ftp', 'http', 'http', 'mysql', 'ssh', 'ssh']
>>> li = ['132', '123', '256', '365']
>>> li.sort()
>>> li
['123', '132', '256', '365']
倒序排序:
>>> li = ['132', '123', '256', '365']
>>> li.sort(reverse=True)
>>> li
['345', '234', '132', '123']
key函數:list.sort()和sorted()函數都有一個key參數,可以用來指定一個函數來確定排序的一個優先級
# 下例即按照小寫字母優先進行排列
>>> sorted("This is a test string from Andrew".split(), key=str.lower)
['a', 'Andrew', 'from', 'is', 'string', 'test', 'This']
2.shuffle()
需要注意的是,此方法不是列表的方法
>>> import random
>>> li = list(range(20,30))
>>> li
[20, 21, 22, 23, 24, 25, 26, 27, 28, 29]
>>> random.shuffle(li)
>>> li
[24, 20, 22, 26, 27, 29, 25, 23, 21, 28]
練習:
有列表是[1,2,3,4,5,6,7,8,9,0],將此列表循環左移一次
實現:
num = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0]
numlen=len(num)
tmp=num[0]
for i in range(numlen-1):
num[i]=num[i+1]
num[-1]=tmp
print(num)
運行結果:
7.列表元素的修改
通過索引,重新賦值:
>>> service = ['https','ftp','ssh','mysql']
>>> service[0] = 'http'
>>> service
['http', 'ftp', 'ssh', 'mysql']
通過切片賦值
>>> service[:2] = ['nfs', 'samba']
>>> service
['nfs', 'samba', 'ssh', 'mysql']