1. 程式人生 > >Python聽課筆記---內建結構list,tuple,set,dict

Python聽課筆記---內建結構list,tuple,set,dict

**

一、列表list

**

  1. append() 在末尾插入一個內容
  2. insert(index,data) 指定位置插入 如:a.insert(3,11)在第四個位置插入11
  3. del() 刪除
  4. pop()拿出最後一個元素,經常有賦值操作如:a=x.pop()
  5. remove()在列表中刪除指定值的元素,列表中必須有這個值,否則會報錯,應使用try或先進行判斷
  6. clear()清空,但列表的地址不變也就是說仍存在空間
  7. reverse()翻轉,列表地址不變
  8. extend()擴充套件列表,a.attend(b)把b加到a上
  9. count()查詢列表中指定元素的個數
  10. copy()淺拷貝傳值不傳址且只傳一層即二層列表傳址 【注】list操作中直接賦值相當於傳址操作

**

二、元組-tuple(不可更改)

**

建立:

  1. t=() print(type(t))
  2. t = 1,2,3,4 print(type(t))
  3. l = [1,2,3,4] t = tuple(l) list所有的特性,除了可以修改,元組都有 索引,分片,序相加,相乘,成員操作一模一樣 關於元組的函式
  4. len()獲取長度
  5. min()max()最大最小值
  6. 元組變數交換法 a=1 b=2 a,b=b,a print(a,b) 結果:2 1

**

三、集合-set

** 一堆確定的無序的唯一的資料,集合中每一個數據成為一個元素 定義:s=set() s={1,2,3,4,5} l = [1,2,3,5,5,5,5] s = set(l) 特徵:集合內資料無序,無法索引和分片 集合內資料元素具有唯一性,不能有重複資料 內部只能放可雜湊的資料 集合序列操作: in , not in 集合的遍歷操作:for i in s: ##帶有元組的集合遍歷 s = {(1,2,3),(‘i’,‘love’,‘you’),(4,5,6)} for k,m,n in s: print(k," “,m,” ",n) for k in s: print(k) 結果:4 5 6 i love you 1 2 3 (4, 5, 6) (‘i’, ‘love’, ‘you’) (1, 2, 3) 集合的內涵: ##普通集合的內涵,以下初始化後自動過濾掉重複的元素 s = {1,1,222,3,6,8,3} print(s) ss = {i for i in s} print(ss) 結果: {1, 3, 6, 8, 222} {1, 3, 6, 8, 222} ##帶條件的集合內涵 s = {1,2,333,453,2,1,776} ss = {i for i in s if i%2 == 0} print(ss) 結果:{776, 2} ##多迴圈的集合內涵 s1 = {1,2,3,4} s2 = {“i”,“love”,“you”} s = {m*n for m in s2 for n in s1 if n==2} print(s) 結果:{‘youyou’, ‘lovelove’, ‘ii’} 關於集合的函式: len(),max(),min()跟其他函式基本一致

  1. add()向集合內新增元素,向末尾新增
  2. clear清空,原地清空
  3. copy()函式,淺拷貝s1=s2.copy()
  4. remove()移除指定的值,直接改變原有值,如果要刪除的值不存在會報錯
  5. discard()移除集合中指定值,根remove()一樣,但是元素不存在不會報錯 6.pop()隨機移除一個元素
  6. intersection()交集
  7. difference()差集
  8. union()並集
  9. issubset()檢查一個集合是否為另一個集合的子集
  10. issuperset()檢查一個集合是否為另一個集合的超集 s1 = {1,2,3,4,5} s2 = {4,5,6,7,8} s_1 = s1.intersection(s2) print(s_1) s_2 = s1.difference(s2) print(s_2) s_3 = s1.union(s2) print(s_3) s_4 = s1.issubset(s2) print(s_4) 結果:{4, 5} {1, 2, 3} {1, 2, 3, 4, 5, 6, 7, 8} False
  11. frozenset冰凍集合,冰凍就是不可以任何修改的集合 ##建立 s = frozenset()

**

四、dict字典

** ##建立空字典 d = {} 或d = dict{} ##建立有值的字典,每一組用冒號分開,每一對鍵值對用逗號隔開 d = {“one”:1,“two”:2,“three”:3} ##用元組建立一個字典 d=dict([(“one”,1),(“two”,2),(“three”,3)]) print(d) 結果:{‘one’: 1, ‘two’: 2, ‘three’: 3} ##用關鍵字建立一個字典 d=dict(one=1,two=2,three=3) print(d) 結果:{‘one’: 1, ‘two’: 2, ‘three’: 3} ##字典的特徵 ##字典是序列型別,但是是無序序列,所以沒有分片和索引 ##字典中的資料是每一個鍵值對組成,即沒有kv對 ##key:必須是可雜湊的值,如int,char,float,但是list,set,dict不行 ##value:任何值 ##常見操作 ##訪問資料 d={“one”:1,“two”:2,“three”:3} print(d[“one”]) d={“one”:1,“two”:2,“three”:3} d[“one”]=“一”##修改 print(d) del d[“one”]##刪除 print(d) 結果:1 {‘one’: ‘一’, ‘two’: 2, ‘three’: 3} {‘two’: 2, ‘three’: 3} ##成員檢測in , not in ##檢測的不是值,也不是鍵值對,是鍵 d={“one”:1,“two”:2,“three”:3} if 2 in d: print(“值”) if “two” in d: print(“鍵”) if (“two”,2) in d: print(“鍵值對”) 結果:鍵 ##遍歷 d={“one”:1,“two”:2,“three”:3} for k in d.keys(): print(k, d[k]) 結果:one 1 two 2 three 3 ##字典生成式 d={“one”:1,“two”:2,“three”:3} dd={k:v for k,v in d.items() if v%2==0} print(dd) 結果:{‘two’: 2} ##str(字典)返回字典的字串格式 ##items返回字典對應組成的元組格式 d={“one”:1,“two”:2,“three”:3} i=d.items() print(type(i)) print(i) 結果:<class ‘dict_items’> dict_items([(‘one’, 1), (‘two’, 2), (‘three’, 3)]) ##get:根據指定鍵返回相應的值,好處是可以返回預設值 d={“one”:1,“two”:2,“three”:3} print(d.get(“aaa”))##會輸出none,並且預設值是可設定的如print(d.get(“aaa”,100))此時會返回100 print(d[“aaa”])##會報錯 ##fromkeys 使用指定的序列做為鍵,使用一個值作為字典的所有的鍵的值 s=[“一”,“二”,“三”] d=dict.fromkeys(s,1) print(d) 結果:{‘一’: 1, ‘二’: 1, ‘三’: 1}