標籤:val lex enc padding ase new [1] 線性 xtend
內建資料結構分類:
數值型
int , float , complex , bool
序列對象
字串 str
列表 list
tuple(元組)
索引值對
集合 set
字典 dict
數字型
int ,float , complex , bool都是class, 1,5.0 , 2+3j都是對象的執行個體
int : python3的int就是長整型,沒有大小限制,受限於記憶體大小
float:有整數部分和小數部分組成,支援十進位和科學計數法表示,只有雙精確度型
complex:有實數和虛部組成,實部和虛部都是浮點數,3+4j
bool:int 的子類,僅有2個執行個體True和False 對應 1和0,可以和整數直接運算
列表 list()
列表是可變的,連續的(sequence),可以進行索引的,線性資料結構,可迭代的資料結構
區分:
list列表: 尋找快...但是從修改(增/刪)的時候,很麻煩很慢
link鏈表: 尋找慢...但是修改快,尋找很慢
queue(隊列): 先進先出~
stack(棧): 先進後出,後進先出(堆棧)
列表list定義: 初始化
list() ->new empty list
list(iterable) -> new list initialized from iterable's items
list不能一開始就定義大小
lst = list()lst = []lst = [2,5,6,'ab']lst = list(range(5))
索引 index: 也叫下標,從0?開始
正索引: 從左至右,從0開始,為列表中每個單元進行編號
負索引:從右至左,從-1開始
正負索引不可超界,否則發生一場:Index Error
列表通過索引訪問:list[index],index就是索引,使用中包括訪問
列表查詢方法:
1.L.index(valve,[start,stop])
通過元素值,從指定區間尋找列表內的元素是否匹配
匹配到第一就立刻返回索引
匹配不到,拋出異常valveError
2.count(valve)
返回列表中匹配到valve的次數
時間複雜度:
index和count方法都是O(n)[遍曆]
隨著列表資料規模的增大,效率下降
len():輸出資料行表的長度
列表元素修改
索引訪問修改 ,索引不要超界
list[index] = valve
列表增加,插入列表
返回None意味著沒有新的列表產生,就地修改
1.L.append(object) -> None
列表尾部追加元素,返回None
實際複雜度是O(1)
2.L.insert(index,object) -> None
在指定的索引 index處插入語元素
時間複雜度是O(n)
此處index可以超界:
超越上界,尾部追加;
超越下界,頭部追加
3.L.extend(iterable) -> None
返回 list ,意味著產生了新的list
1. + -> list
串連操作,將兩個列表串連起來
產生新的列表,原列表不變
本質上調用的是__add___()方法
2. * -> list
重複操作,將本列表元素重複n次,返回新的列表
列表 * 重複的坑:
x = [[1,2,3]] * 3print(x)x[0][1] = 20print(x)[[1, 2, 3], [1, 2, 3], [1, 2, 3]][[1, 20, 3], [1, 20, 3], [1, 20, 3]]y = [1] * 5y[0] = 6y[1] = 7print(y)[6, 7, 1, 1, 1]
列表刪除元素
1. L.remove(valve) -> None
2. L.pop([index]) -> item
不指定索引 index,就從列表尾部彈出一個元素
指定索引 index,就從index處彈出一個元素,索引超界拋出IndexError錯誤
效率:不指定索引 Index時 時間複雜度= O(1) ,指定索引(從頭,或中間), 時間複雜度=O(n)
3. L.clear() -> None
其它列表操作
1. L.reserve() -> None
2. L.sort(key=None,reserve=Flase) -> None
3. in , not in
[3,4] in [1,2,[3,4]]
[5] not in [1,2,[3,4]]
for x in [1,2,[3,4]]
列表複製
L.copy() -> List
1.淺拷貝shadow copy:
影子拷貝,也叫淺拷貝,遇到參考型別,只是複製了一個引用而已
lst0 = [1,[2,3,4],5]lst5 = lst0.copy()lst5[1][1] = 20print(lst5)print(lst0)[1, [2, 20, 4], 5][1, [2, 20, 4], 5]
2.深拷貝deepcopy
copy模組提供了deepcopy
import copylst0 = [1,[2,3,4],5]lst5 = copy.deepcopy(lst0)lst5[1][1] = 20lst5 ! == lst0print(lst5)print(lst0)[1, [2, 20, 4], 5][1, [2, 3, 4], 5]
隨機數 random模組
1. random.randint(a,b) -> item
2. random.randrange([start],stop,[step]) -> item
從指定範圍內,按指定基數遞增的集合中擷取一個隨機數, 基數預設值為1. random.randrange(1,7,2)
3. random.choice() -> item
從非空序列的元素中隨機抽取一個元素,exp: random.choice(range(10)) 從0到9中隨機挑選一個整數.random.choice([1,3,5,7])
4. random.shuffle(list) -> none
5. random.sample(population,k) -> list
random.sample(['a','b','c','d'],2)random.sample(['a','b'] ,2
元組
一個有序的元素組成的集合
使用小括弧()表示
元組是不可變對象
元組的定義 初始化
定義:
tuple() -> empty tuple
tuple(iterable) -> tuple initialized from iterable's items
t = tuple()
t = ()
t = tuple(range(1,7,2)) 可迭代對象
t = (1,) # 一個元素元組的定義,注意要有個逗號
t = (1,) * 5
t = (1,2,3) * 6
元組元素的訪問
元組查詢
1.t.index(valve,[start,stop])
2.count(valve)
index和count方法都是O(n)[遍曆]
隨著列表資料規模的增大,效率下降
len():返回元素的個數
元組不可變,唯讀,所以沒有增,刪,改的方法
命名元組namedtuple
namedtuple(typename,field_names,verbose= False,rename=False)
from collections import namedtuplepoint = namedtuple("_point",["x", "y"]) # point為返回的類p = point(11,22)Exp:form collections import namedtupleStudent = namedtuple("Student","name age")tom = Student("tom",20)jerry = Student("jerry,18")tome.name
Python內建資料結構——列表list,元組tuple