# len:獲取元組的長度 t = (1,2,3,4,5) len(t)
5
# max,min:最大最小值 print(max(t)) print(min(t))
5 1
# tuple:轉化或建立元組 l = (1,2,3,4,5) t = tuple(l) print(t) t = tuple() print(t)
(1, 2, 3, 4, 5) ()
# count:計算指定數據出現的次數 t = (2,1,2,3,45,1,1,2,) print(t.count(2)) # index:求指定元素在元組中的索引位置 print(t.index(45)) # 若是須要的查找的數字是多個,則返回第一個 print(t.index(1))
3 4 1
# 兩個變量交換值 a = 1 b = 3 print(a) print(b) print("*" * 20) # java程序員會這麼寫: c = a a = b b = c print(a) print(b) print("*" * 20) # python寫法 a,b = b,a print(a) print(b)
1 3 ******************** 3 1 ******************** 1 3
# 集合的定義 s = set() print(type(s)) print(s) # 此時,大括號內必定要有值,不然定義出的是一個dict s = {1,2,3,4,5,6,7} print(type(s)) print(s)
<class 'set'> set() <class 'set'> {1, 2, 3, 4, 5, 6, 7}
# 若是隻是用大括號定義,則定義的是一個dict類型 d = {} print(type(d)) print(d)
<class 'dict'> {}
# 成員檢測 # in,not in s = {4,5,"i", "love", "you"} print(s) if "love" in s: print("Yes") if "haha" not in s: print("Yes")
{'you', 4, 5, 'love', 'i'} Yes Yes
# for 循環 s = {4,5,"i", "love", "you"} for i in s: print(i)
you 4 5 love i
# 帶有元組的集合遍歷 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)
i -- love -- you 4 -- 5 -- 6 1 -- 2 -- 3 ('i', 'love', 'you') (4, 5, 6) (1, 2, 3)
# 普通集合內涵 # 如下集合在初始化後自動過濾掉重複元素 s = {23,223,233,2,4,5,6,3,4,1,5,3} print(s) # 普通集合內涵 ss = {i for i in s} print(ss)
{1, 2, 3, 4, 5, 6, 233, 23, 223} {1, 2, 3, 4, 5, 6, 233, 23, 223}
# 帶條件的集合內涵 sss = {i for i in s if i % 2 == 0} print(sss)
{2, 4, 6}
# 多循環的集合內涵 s1 = {1,2,3,4} s2 = {"i", "love", "you"} s = {m*n for m in s2 for n in s1} print(s) s = {m*n for m in s2 for n in s1 if n == 2} print(s)
{'you', 'youyou', 'love', 'lovelovelovelove', 'lovelovelove', 'lovelove', 'iii', 'youyouyouyou', 'ii', 'i', 'iiii', 'youyouyou'} {'lovelove', 'youyou', 'ii'}
# len,max,min:跟其餘基本的函數一致 s = {23,54,72,3,5,3,3,6,1,543} print(len(s)) print(max(s)) print(min(s))
8 543 1
# set:生成一個集合 l = {1,2,3,4,5,4,3,2,1} s = set(l) print(s)
{1, 2, 3, 4, 5}
# add:向集合內添加元素 s = {1} s.add(3) print(s)
{1, 3}
# clear s = {1,2,3,4,5} print(id(s)) s.clear() print(id(s)) # 結果代表clear函數是原地清空數據
1370773843528 1370773843528
# copy:拷貝 # remove:移除指定的值,直接改變原有值,若是要刪除的值不存在,報錯 # discard:移除集合中指定的值跟remove同樣,可是若是要刪除的話,不報錯 s = {23,4,3,5,1,2,3} s.remove(4) print(s) s.discard(1) print(s) print("*" * 20) s.discard(100) print(s) s.remove(100) print(s)
{1, 2, 3, 5, 23} {2, 3, 5, 23} ******************** {2, 3, 5, 23} --------------------------------------------------------------------------- KeyError Traceback (most recent call last) <ipython-input-35-0113522ad176> in <module> 12 print(s) 13 ---> 14 s.remove(100) 15 print(s) KeyError: 100
# pop 隨機移除一個元素 s = {1,2,3,4,5,6,7} d = s.pop() print(d) print(s)
1 {2, 3, 4, 5, 6, 7}
# 集合函數 # intersection:交集 # difference:差集 # union:並集 # issubset:檢查一個集合是否爲另外一個子集 # issuperset:檢查一個集合是否爲另外一個超集 s1 = {1,2,3,4,5,6} s2 = {5,6,7,8,9} s_1 = s1.intersection(s2) print(s_1) s_2 = s1.difference(s2) print(s_2) s_3 = s1.issubset(s2) print(s_3) s_4 = s1.issuperset(s2) print(s_4)
{5, 6} {1, 2, 3, 4} False False
# 集合數學操做 s1 = {1,2,3,4,5,6} s2 = {5,6,7,8,9} # 如下不支持 s_1 = s1 - s2 print(s_1) s_2 = s1 + s2 print(s_2)
{1, 2, 3, 4} --------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-45-fac787d752ea> in <module> 7 print(s_1) 8 ----> 9 s_2 = s1 + s2 10 print(s_2) TypeError: unsupported operand type(s) for +: 'set' and 'set'
# 建立 s = frozenset() print(type(s)) print(s)
<class 'frozenset'> frozenset()
# 字典的建立 # 建立空字典1 d = {} print(type(d)) print(d) # 建立空字典2 d = dict() print(d) # 建立有值的字典,每一組數據用冒號隔開,每一對鍵值對用逗號隔開 d = {"one":1, "two":2, "three":3} print(d) # 用dict建立有內容字典1 d = dict({"one":1, "two":2, "three":3}) print(d) # 用dict建立有內字典2 # 利用關鍵參數 d = dict(one=1, two=2, three=3) print(d) # d = dict( [("one",1), ("two",2), ("three",3)]) print(d)
<class 'dict'> {} {} {'one': 1, 'two': 2, 'three': 3} {'one': 1, 'two': 2, 'three': 3} {'one': 1, 'two': 2, 'three': 3} {'one': 1, 'two': 2, 'three': 3}
字典中的數據每一個都有鍵值對組成,即kv對java
# 訪問數據 d = {"one":1, "two":2, "three":3} # 注意訪問格式 # 中括號內是鍵值 print(d["one"]) d["one"] = "eins" print(d) # 刪除某個操做 # 使用del操做 del d["one"] print(d)
1 {'one': 'eins', 'two': 2, 'three': 3} {'two': 2, 'three': 3}
# 成員檢測:in,not in # 成員檢測檢測時的key內容 d = {"one":1, "two":2, "three":3} if 2 in d: print("value") if "two" in d: print("key") if ("two,2") in d: print("kv")
key
# 遍歷在python2 和 3 中區別比較大,代碼不通用 # 按key值使用for循環 d = {"one":1, "two":2, "three":3} # 使用for循環,直接按keu值訪問 for k in d: print(k, d[k]) # 上述代碼能夠改寫以下 for k in d.keys(): print(k, d[k]) # 只訪問字典的值 for v in d.values(): print(v) # 注意如下特殊用法 for k,v in d.items(): print(k, "--", v)
one 1 two 2 three 3 one 1 two 2 three 3 1 2 3 one -- 1 two -- 2 three -- 3
d = {"one":1, "two":2, "three":3} # 常規字典生成式 dd = {k:v for k,v in d.items()} print(dd) # 加限制條件的字典生成式 dd = {k:v for k,v in d.items() if v % 2 == 0} print(dd)
{'one': 1, 'two': 2, 'three': 3} {'two': 2}
# 通用函數:len,max,min,dict # str(字典):用於返回字典的字符串格式 d = {"one":1, "two":2, "three":3} print(str(d))
{'one': 1, 'two': 2, 'three': 3}
# clear:清空字典 # 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)])
# keys:返回字典的鍵組成的一個結構 k = d.keys() print(type(k)) print(k)
<class 'dict_keys'> dict_keys(['one', 'two', 'three'])
# values:同理,一個可迭代的結構 v = d.values() print(type(v)) print(v)
<class 'dict_values'> dict_values([1, 2, 3])
# get:根據指定鍵返回相應的值,好處是,能夠生成默認值 d = {"one":1, "two":2, "three":3} print(d.get("oner")) # get默認值是None,能夠設置 print(d.get("one", 100)) print(d.get("one33", 100)) print(d['on333'])
None 1 100 --------------------------------------------------------------------------- KeyError Traceback (most recent call last) <ipython-input-86-f8c01a58018e> in <module> 8 print(d.get("one33", 100)) 9 ---> 10 print(d['on333']) KeyError: 'on333'
# fromkeys:使用指定的序列做爲鍵,使用一個值做爲字典的全部鍵的值 l = ["eins", "zwei", "dree"] # 注意fromkeys兩個參數的類型 # 注意fromkeys的調用主體 d = dict.fromkeys(l, "hahahahaha") print(d)
{'eins': 'hahahahaha', 'zwei': 'hahahahaha', 'dree': 'hahahahaha'}