Python學習筆記02——內置序列及其操作(數據結構)

1常用的數據結構及其特點

數據結構 特點
元組 靜態數據結構,只能查詢,無法修改、增加,刪除只能刪除整個元組
列表 有順序的數據結構,類似於數組,增刪改查都可以操作
字典 儲存鍵值對的數據結構,無序性
Set(集合) 無序性數據結構,且集合中的元素具有唯一性

2元組及其操作

2.1元組的創建

y1=(1,2,3,4)

2.2元組的查詢操作

①根據索引查詢(超界會報錯)

>>> y1=(1,2,3,4)
>>> y1[0]
1
>>> y1[-1]
4
>>> y1[10]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IndexError: tuple index out of range

②索引查詢(不存在會報錯)

>>> y1.index(2)
1
>>> y1.index(10)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: tuple.index(x): x not in tuple

③存在性查詢

>>> 1 in y1
True
>>> 10 in y1
False

④遍歷查詢

for i in range(len(y1)):
    print(y1[i])
...
1
2
3
4

2.3元組的刪除操作

①元組不能刪除元素,只能整體刪除

>>> del y1
>>> y1
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'y1' is not defined

2.4元組的其他操作

①切片 (注意切片、=、copy的異同點)
[::]三個參數分別爲起始索引、終止索引、步長
1.起始,終止爲左閉右開區間
2.步長可爲負值,不可爲0

>>> y1=(1,2,3,4)
>>> y1[::-1]
(4, 3, 2, 1)

②求長度、最大值、最小值、求和

>>> len(y1)#求長度
4
>>> max(y1)#最大值
4
>>> min(y1)#最小值
1
>>> sum(y1)#求和
10

3列表及其操作

3.1列表的創建

>>> x1=[1,2,3,4,5]

3.2列表的查詢

①根據索引查詢(超界會報錯)

>>> x1[0]
1
>>> x1[-1]
5
>>> x1[10]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IndexError: list index out of range

②索引查詢(不存在會報錯)(多個只查詢第一個索引)

>>> x1.index(1)
0
>>> x1.index(10)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: 10 is not in list

③存在性查詢

>>> x1
[1, 2, 3, 4, 5]
>>> 1 in x1
True
>>> 6 in x1
False

④遍歷查詢

 for i in range(len(x1)):
     print(x1[i])
...
1
2
3
4
5

3.3列表的修改

①根據索引賦值修改(超界報錯)

>>> x1[4]=6
>>> x1
[1, 2, 3, 4, 6]
>>> x1[4]=5
>>> x1
[1, 2, 3, 4, 5]

3.4列表的增加

①增加單個元素append

>>> x1.append(6)
>>> x1
[1, 2, 3, 4, 5, 6]
>>> x1.append([7,8])
>>> x1
[1, 2, 3, 4, 5, 6, [7, 8]]#增加一個列表也不會報錯,但是列表會作爲一個元素加到最後面

②指定位置插入元素
insert(插入索引,插入元素)

>>> x1
[1, 2, 3, 4, 5, 6]
>>> x1.insert(0,0)
>>> x1
[0, 1, 2, 3, 4, 5, 6]

③增加一個列表extend

>>> x1
[0, 1, 2, 3, 4, 5, 6]
>>> x1.extend([7,8,9])
>>> x1
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

3.5列表的刪除

①刪除指定索引
.pop(索引值) 默認刪除最後一個,插入參數可刪除指定索引的元素 ,返回該元素
del 列表[索引值]

>>> x1
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> x1.pop()
9
>>> x1
[0, 1, 2, 3, 4, 5, 6, 7, 8]
>>> x1.pop(-1)
8
>>> x1
[0, 1, 2, 3, 4, 5, 6, 7]
>>> del x1[-1]
>>> x1
[0, 1, 2, 3, 4, 5, 6]

②刪除指定元素 (刪除爲第一個指定元素,多個重複只刪除第一個)

>>> x1=[1,2,3,2,4,2]
>>> x1
[1, 2, 3, 2, 4, 2]
>>> x1.remove(2)
>>> x1
[1, 3, 2, 4, 2]

③刪除列表本身

>>> del x1

3.6列表的排序操作

①將原列表顛倒
[::-1]是切片出一個新的列表與原列表倒序
reverse()是將元列表本身進行倒敘操作,且沒有返回值

>>> x1=[1,2,3,4,1]
>>> x1[::-1]
[1, 4, 3, 2, 1]
>>> x1
[1, 2, 3, 4, 1]
>>> x1.reverse()
>>> x1
[1, 4, 3, 2, 1]

②將原列表按順序(可自定義)排序

>>> x1
[1, 4, 3, 2, 1]
>>> x1.sort()
>>> x1
[1, 1, 2, 3, 4]

3.7列表的其它操作

①求長度、最大值、最小值、求和、統計次數

>>> x1
[1, 1, 2, 3, 4]
>>> len(x1)
5
>>> max(x1)
4
>>> min(x1)
1
>>> sum(x1)
11
>>> x1.count(1)
2

4字典及其操作

4.1字典的創建

①直接創建

 english={"we":"我們","world":"世界","company":"公司"}
>>> english
{'we': '我們', 'world': '世界', 'company': '公司'}

②通過一個序列創建(列表、元組、SET)
dict.fromkeys(參數1,參數2) 參數1爲鍵值序列對象,參數2爲默認值,可寫可不寫

>>> shunxu=dict.fromkeys(x1,"默認值")
>>> shunxu
{1: '默認值', 2: '默認值', 3: '默認值', 4: '默認值', 5: '默認值'}

4.2字典的查詢

①根據關鍵字查詢對應值
1.english[""]直接查詢如果沒有對應鍵,則會報錯
2.english.get()方式查詢,如果沒有對應鍵,則返回空

>>> english["we"]
'我們'
>>> english["city"]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
KeyError: 'city'
>>> english.get("we")
'我們'
>>> english.get("city")

②查詢所有鍵值

 english.keys()
dict_keys(['we', 'world', 'company'])

③查詢所有values值

 english.values()
dict_values(['我們', '世界', '公司'])

④查詢所有鍵值對

english.items()
dict_items([('we', '我們'), ('world', '世界'), ('company', '公司')])

⑤遍歷字典
for k,v in english.items()

 for k,v in english.items():    print("鍵:"+k+"值:"+v)
...
鍵:we值:我們
鍵:world值:世界
鍵:company值:公司

4.3字典的增加/修改

①直接通過賦值增加/修改

>>> english["city"]="家"#不存在該鍵則爲增加
>>> english
{'we': '我們', 'world': '世界', 'company': '公司', 'city': '家'}
>>> english["city"]="城市"#存在對應鍵則爲修改
>>> english
{'we': '我們', 'world': '世界', 'company': '公司', 'city': '城市'}

4.4字典的刪除

①刪除單個元素和刪除字典本身都用del

>>> del english["city"]
>>> english
{'we': '我們', 'world': '世界', 'company': '公司'}
>>> del english
>>> english
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'english' is not defined

4.5字典的其它操作

①清空字典

>>> english
{'we': '我們', 'world': '世界', 'company': '公司'}
>>> english.clear()
>>> english
{}

②字典的複製(與直接賦值的區別)(字典不可切片)

>>> english={"we":"我們","world":"世界","company":"公司"}
>>> english
{'we': '我們', 'world': '世界', 'company': '公司'}
>>> english1=english
>>> english3=english.copy()
>>> english
{'we': '我們', 'world': '世界', 'company': '公司'}
>>> english1
{'we': '我們', 'world': '世界', 'company': '公司'}
>>> english3
{'we': '我們', 'world': '世界', 'company': '公司'}
>>>
>>>
>>> english["we"]="你們"
>>> english
{'we': '你們', 'world': '世界', 'company': '公司'}
>>> english1
{'we': '你們', 'world': '世界', 'company': '公司'}#通過賦值複製的對象隨着原對象的改變而改變
>>> english3
{'we': '我們', 'world': '世界', 'company': '公司'}

5集合及其操作

5.1集合的創建

注意空集合不能用{}創建,只能用set()創建

empty=set()
>>> empty
set()
>>> mix={1,2,3,"Hello"}
>>> mix
{1, 2, 3, 'Hello'}

5.2集合的增加和刪除

>>> mix.add(4)
>>> mix
{1, 2, 3, 4, 'Hello'}
>>> mix.remove("Hello")
>>> mix
{1, 2, 3, 4}

6列表推導式、字典推導式、集合推導式

形式:
[變量 for 條件]
{k:v for 條件}
{變量 for 條件}

>>> l1=[x for x in range(10) if x%2==0]
>>> l1
[0, 2, 4, 6, 8]
>>> d1={1:1,2:4,3:9,4:16}
>>> d1
{1: 1, 2: 4, 3: 9, 4: 16}
>>> d2={v:k for k,v in d1.items()}
>>> d2
{1: 1, 4: 2, 9: 3, 16: 4}
>>>
>>>
>>> s1={i**2 for i in range(10)}
>>> s1
{0, 1, 64, 4, 36, 9, 16, 49, 81, 25}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章