Python的數據容器
Python的數據容器
大一下學期選修的Python課程,以後就用發日誌的方式總結一些Python的知識點,當作自己的學習筆記了。
列表
列表:由一系列按特定順序排列的元素組成。可以創建包含字符、數字的列表,只要你想,任何東西都可以裝進列表,其中的元素也可以沒有任何關係。列表讓我們可以在一個地方存儲成組的信息,其中可以只包含幾個元素,也可以包含數百萬個元素。
訪問列表和列表元素
對於一個已經定義的列表,可以直接用print(List)輸出,但是輸出結果會有[]和’’。
大多數時候,我們只需要訪問列表中的某些元素。可以通過方括號[]和元素的索引來實現這一目的,特別注意:列表的索引是從0開始的。
List=['abc','bcd','cde','def','efg','fgh']
print(List)
print(List[0])
print(List[4])
對列表元素的操作
修改列表元素
列表與元組最大的區別就在於列表的元素是可修改的,而元組的元素不能隨意修改(如果元組的某一元素是列表,是否可以修改這個列表的元素?答案是:可以!)
列表元素的修改,只需要用=重新對元素賦值
List=['abc','bcd','cde','def','efg','fgh']
print(List[2])
List[2]='hello world'
print(List[2])
在列表中添加元素
名稱 | 作用 | 用法 |
---|---|---|
append() | 在列表末尾添加元素 | List.append(‘new’) |
insert() | 在列表的任意位置插入元素 | List.insert(num,‘new’) |
List=['abc','bcd','cde','def','efg','fgh']
print(List)
List.append('new')
print(List)
List.insert(1,'hello world')
print(List)
從列表中刪除元素
名稱 | 作用 | 用法 | 備註 |
---|---|---|---|
del | 刪除位置已知的元素 | del List[num] | del 刪除的元素,不可再訪問 |
pop() | 刪除並使用位置已知的元素 | List.pop() | 使用pop()刪除元素,如果不加索引,默認爲刪除列表的最後一個元素。 |
remove() | 根據值刪除元素 | List.remove(‘name’) | 使用remove()刪除元素時,也可以接着使用它的值。remove()只能刪除第一個指定的值,如果要刪除的值在列表中出現多次,需要用循環來實現全部刪除。 |
List = ['abc','bcd','cde','def','efg','fgh']
print(List)
del List[0]
print(List)
a = List.pop()
print(a)
print(List)
b = List.pop(2)
print(b)
print(List)
c = List.remove('efg')
print(c)#可以看見此處remove使用被刪除元素的用法有別於pop
print(List)
d = 'bcd'
List.remove('bcd')
print(d)
print(List)
總結,如果你要從列表中刪除一個元素,且不在以任何方式使用它,就使用del 語句,否則應使用pop()或remove()。del和pop()是根據元素的索引來刪除,remove()是根據元素具體的值。
列表元素的排序
名稱 | 作用 | 用法 |
---|---|---|
sort() | 對列表進行永久性排序(按字母順序) | List.sort() |
反向排序 | List.sort(reverse=True) | |
sorted | 對列表進行臨時排序臨時排序 | sorted(List) |
反向排序 | sorted(List,reverse=True) | |
reverse() | 對列表進行永久性的前後反轉(注意是反轉不是反向排序!) | List.reverse() |
cars = ['bmw','audi','toyota','subaru']
cars.sort()
print(cars)
cars = ['bmw','audi','toyota','subaru']
cars.sort(reverse = True)
print(cars)
cars=['bmw','audi','toyota','subaru']
print(sorted(cars))
cars=['bmw','audi','toyota','subaru']
print(sorted(cars,reverse = True))
cars=['bmw','audi','toyota','subaru']
cars.reverse()
print(cars)
確定列表的長度
使用函數len()
List = ['abc','bcd','cde','def','efg','fgh']
print(len(List))
對列表的操作
列表的遍歷
使用for循環
List = ['abc','bcd','cde','def','efg','fgh']
for item in List:
print(item)
數字列表的創建
使用range()函數,或採用列表解析
使用方法:list(range(起始數值,終止數值,步長))
注:最終生成的列表的最後一個值小於終止數值;步長默認爲1
List = list(range(1,11,2))
print(List)
或利用循環生成列表
List = []
for value in range(1,11):
List.append(value**2)
print(List)
還可以利用列表解析,這種方法顯然簡潔的多
List = [value**2 for value in range(1,11)]
print(List)
列表切片
列表切片–處理列表的部分元素
要創建切片,需要指定要使用的第一個元素和最後一個元素。而且結果中,不包含上述最後一個元素。
List = ['abc','bcd','cde','def','efg','fgh']
print(List[2:5])
逆向切片
List = ['abc','bcd','cde','def','efg','fgh']
print(List[-5:-2])
同時列表切片也可以在最後加一個步長參數,這一特點可用於輸出一個反轉的列表(不改變原有列表)
List = ['abc','bcd','cde','def','efg','fgh']
print(List[0:5:2])
print(List[::-1])
元組
元組本質上是不可更改元素的列表。
定義元組
dim1 = (1,2,3,4,5)
print(dimention[0])
print(dimention[3])
#dimention[1] = 10 #如果嘗試修改元組的元素值,會報錯
list = [1,2,3,4]
dim2 = (list,2,3,4)
list[2] = 10 #值得注意的是,如果元組的元素是列表,那麼該列表的元素是可以更改的
修改元組變量
雖然元組的元素是不可更改的,但是可以給存儲元組的變量重新賦值,類似於重新定義整個元組
dim = (200,50)
print(dim)
dim = (100,30)
print(dim)
遍歷元組
元組的遍歷方法類似於列表,仍然可以使用for
dim = (1,2,3,4,5)
for i in dim:
print(i)
判斷元組是否存在某個值
dim = (1,3,5,7,9)
print(5 in dim)
print(0 in dim)
字典
- 字典的本質是一種哈希映射。
- 字典是一種大小可變的鍵值對集,其中的鍵key與值value都是Python對象
- 應用:高速查找
字典的創建
一般方法
dict = {1:"hello",4:"hello",2:"world",3:"!"}
print(dict)
**字典的數據元素是無序的,並不會按照初始化的順序排列**
**不同的key可以對應相同的value,但一個value只能對應一個key**
利用for循環和zip()函數快速創建字典
dict_key = [1,2,3,4]
dict_value = ['hello','world','hello','BUPT']
dict = {}
for key,value in zip(dict_key,dict_value):
dict[key] = value
print(dict)
**注意{}只可用於創建空字典,不能用來創建空集合**
字典元素的訪問
字典的元素訪問、插入、設置與列表相同。但是,列表和元組的索引是按順序自動生成的,而字典的索引號就是鍵key。
dict = {1:"hello",4:"hello",2:"world",3:"!"}
print(dict[4])
訪問字典元素,除了用索引,還可以通過get()方法,如果字典不包含某個鍵,會返回None,或自己指定的值。
dict = {1:"hello",4:"hello",2:"world",3:"!"}
print(dict.get(4))
print(dict.get(1,2))
字典元素的刪減
名稱 | 作用 |
---|---|
del函數 | 刪除單一元素或整個字典 |
pop()方法 | 刪除單一元素 |
clear()方法 | 清空字典所有元素 |
dict = {1:"hello",4:"hello",2:"world",3:"!"}
del dict[1]
print(dict)
del dic
print(dict)
dict = {1:"hello",4:"hello",2:"world",3:"!"}
dict.pop(4)
print(dict)
dict = {1:"hello",4:"hello",2:"world",3:"!"}
dict.clear()
print(dict)
判斷字典是否存在某個鍵
dict = {1:"hello",4:"hello",2:"world",3:"!"}
print(5 in dict)
print(1 in dict)
#也可以使用內置的 has_key() 方法
print(dict.das_key(1))
#可以用 keys() 方法和 values() 方法獲取所有的key和value
print('keys are: ',dict.keys())
print('values are: ',dict.values())
將字典轉化爲元組列表
可以通過items()方法取出字典中所有的key-value對,並保存爲一個元組列表。
dict = {1:"hello",4:"hello",2:"world",3:"!"}
dict.items()
集合
- 集合是一種無序集,它是一組鍵的集合,不存儲值
- 集合的元素是互異的,將列表轉化爲集合可用來去除列表中的重複值
- 集合也可以進行相應的數學運算:交集、並集、差集、補集
集合的創建
集合的創建有兩種方法:利用set()函數,或直接用 {}
myset1 = set()
myset2 = set(["hello","1","2","world"])
myset3 = {"hello","1","2","world"}
print(myset1)
print(myset2)
print(myset3)
**創建空集合只能用 set()**
集合的運算
set1 = {'abc','hello','world','i','u'}
set2 = {'i','love','u','123'}
print(set1)
print(set2)
print('並集-或 ',set1 | set2)
print('交集-與 ',set1 & set2)
print('差集 ',set1 - set2)
print('對稱差 ',set1 ^ set2)