注意:相關(guān)筆記或代碼段之間空一行玄坦,沒有聯(lián)系知識點之間的代碼段之間空兩行,以示區(qū)別;娉痢<彘埂!
技術(shù)交流QQ群:1027579432车伞,歡迎你的加入择懂!
筆記note9.md
關(guān)于元組的函數(shù)
- 以下看代碼
- 以下函數(shù),對list基本適用
元組的函數(shù)
- 基本跟list通用
元組變量交換法
- 兩個變量交換值
集合-set
- 集合是高中數(shù)學(xué)中的一個概念
- 一堆確定的無序的唯一的數(shù)據(jù)另玖,集合中每一個數(shù)據(jù)成為一個元素
集合的特征
- 集合內(nèi)數(shù)據(jù)無序困曙,即無法使用索引和分片
- 集合內(nèi)部數(shù)據(jù)元素具有唯一性,可以用來排除重復(fù)數(shù)據(jù)
- 集合內(nèi)的數(shù)據(jù)谦去,str, int, float, tuple,冰凍集合等慷丽,即內(nèi)部只能放置可哈希數(shù)據(jù)
集合序列操作
集合便利操作
集合的內(nèi)涵
集合函數(shù)/關(guān)于集合的函數(shù)
frozen set:冰凍集合
- 冰凍和就是不可以進行任何修改的集合
- frozenset是一種特殊集合
dict字典
- 字典是一種組合數(shù)據(jù),沒有順序的組合數(shù)據(jù)鳄哭,數(shù)據(jù)以鍵值對形式出現(xiàn)
字典的特征
- 字典是序列類型要糊,但是是無序序列,所以沒有分片和索引
- 字典中的數(shù)據(jù)每個都有鍵值對組成妆丘,即kv對
- key: 必須是可哈希的值锄俄,比如int,string,float,tuple, 但是,list,set,dict 不行
- value: 任何值
字典常見操作
代碼 9.內(nèi)置數(shù)據(jù)結(jié)構(gòu)list勺拣、set奶赠、dict、tuple(2).py
!/usr/bin/env python
-- coding: utf-8 --
@Date : 2018-10-09 20:09:55
@Author : cdl (1217096231@qq.com)
@Link : https://github.com/cdlwhm1217096231/python3_spider
@Version :
len: 獲取元組的長度
t = (1, 2, 3, 4, 5)
len(t)
max, min:最大最小值
如果药有,列表或元組中有多個最大最小值毅戈,則實際打印出哪個
print(max(t))
print(min(t))
tuple:轉(zhuǎn)化或創(chuàng)建元組
l = [1, 2, 3, 4, 5]
t = tuple(l)
print(t)
t = tuple()
print(t)
count: 計算制定數(shù)據(jù)出現(xiàn)的次數(shù)
t = (2, 1, 2, 3, 45, 1, 1, 2,)
print(t.count(2))
index:求制定元素在元組中的索引位置
print(t.index(45))
如果需要查找的數(shù)字是多個,則返回第一個
print(t.index(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)
集合的定義
s = set()
print(type(s))
print(s)
此時愤惰,大括號內(nèi)一定要有值苇经,否則定義出的是一個dict
s = {1, 2, 3, 4, 5, 6, 7}
print(s)
如果只是用大括號定義,則定義的是一個dict類型
d = {}
print(type(d))
print(d)
成員檢測
in, not in
s = {4, 5, "i", "love", "wangxiaojing"}
print(s)
if "love" in s:
print("愛呀")
if "haha" not in s:
print("挨個錘子")
for 循環(huán)
s = {4, 5, "i", "love", "wangxiaojing"}
for i in s:
print(i, end=" ")
帶有元組的集合遍歷
s = {(1, 2, 3), ("i", "love", "wangxiaojing"), (4, 5, 6)}
for k, m, n in s:
print(k, "--", m, "--", n)
for k in s:
print(k)
普通集合內(nèi)涵
以下集合在初始化后自動過濾掉重復(fù)元素
s = {23, 223, 545, 3, 1, 2, 3, 4, 3, 2, 3, 1, 2, 4, 3}
print(s)
普通集合內(nèi)涵
ss = {i for i in s}
print(ss)
帶條件的集合內(nèi)涵
sss = {i for i in s if i % 2 == 0}
print(sss)
多循環(huán)的集合內(nèi)涵
s1 = {1, 2, 3, 4}
s2 = {"i", "love", "wangxiaojing"}
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)
len, max, min:跟其他基本函數(shù)一致
s = {43, 23, 56, 223, 4, 2, 1222, 4, 323, 1}
print(len(s))
print(max(s))
print(min(s))
set:生成一個集合
l = [1, 2, 3, 4, 3, 23, 1, 2, 3, 4]
s = set(l)
print(s)
add:向集合內(nèi)添加元素
s = {1}
s.add(334)
print(s)
clear
s = {1, 2, 3, 4, 5}
print(id(s))
s.clear()
print(id(s))
結(jié)果表明clear函數(shù)是原地清空數(shù)據(jù)
copy:拷貝
remove:移除制定的值羊苟,直接改變原有值塑陵,如果要刪除的值不存在,報錯
discard:移除集合中指定的值蜡励,跟remvoe一樣令花,但是入股要刪除的話阻桅,不報錯
s = {23, 3, 4, 5, 1, 2, 3}
s.remove(4)
print(s)
s.discard(1)
print(s)
print("*" * 20)
s.discard(1100)
print(s)
s.remove(1100)
print(s)
pop 隨機移除一個元素
s = {1, 2, 3, 4, 5, 6, 7}
d = s.pop()
print(d)
print(s)
集合函數(shù)
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)
集合的數(shù)學(xué)操作
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)
創(chuàng)建
s = frozenset()
print(type(s))
print(s)
字典的創(chuàng)建
創(chuàng)建空字典1
d = {}
print(d)
創(chuàng)建空字典2
d = dict()
print(d)
創(chuàng)建有值的字典, 每一組數(shù)據(jù)用冒號隔開兼都, 每一對鍵值對用逗號隔開
d = {"one": 1, "two": 2, "three": 3}
print(d)
用dict創(chuàng)建有內(nèi)容字典1
d = dict({"one": 1, "two": 2, "three": 3})
print(d)
用dict創(chuàng)建有內(nèi)容字典2
利用關(guān)鍵字參數(shù)
d = dict(one=1, two=2, three=3)
print(d)
d = dict([("one", 1), ("two", 2), ("three", 3)])
print(d)
訪問數(shù)據(jù)
d = {"one": 1, "two": 2, "three": 3}
注意訪問格式
中括號內(nèi)是鍵值
print(d["one"])
d["one"] = "eins"
print(d)
刪除某個操作
使用del操作
del d["one"]
print(d)
成員檢測嫂沉, in, not in
成員檢測檢測的是key內(nèi)容
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")
遍歷在python2 和 3 中區(qū)別比較大扮碧,代碼不通用
按key來使用for循環(huán)
d = {"one": 1, "two": 2, "three": 3}
使用for循環(huán)趟章,直接按key值訪問
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)
字典生成式
d = {"one": 1, "two": 2, "three": 3}
常規(guī)字典生成式
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)
字典相關(guān)函數(shù)
通用函數(shù): len, max, min, dict
str(字典): 返回字典的字符串格式
d = {"one": 1, "two": 2, "three": 3}
print(str(d))
clear: 清空字典
items: 返回字典的鍵值對組成的元組格式
d = {"one": 1, "two": 2, "three": 3}
i = d.items()
print(type(i))
print(i)
keys:返回字典的鍵組成的一個結(jié)構(gòu)
k = d.keys()
print(type(k))
print(k)
values: 同理,一個可迭代的結(jié)構(gòu)
v = d.values()
print(type(v))
print(v)
get: 根據(jù)指定鍵返回相應(yīng)的值慎王, 好處是:可以設(shè)置默認(rèn)值
d = {"one": 1, "two": 2, "three": 3}
print(d.get("on333"))
get默認(rèn)值是None蚓土,可以設(shè)置
print(d.get("one", 100))
print(d.get("one333", 100))
體會以下代碼跟上面代碼的區(qū)別
print(d['on333'])
fromkeys: 使用指定的序列作為鍵,使用一個值作為字典的所有的鍵的值
l = ["eins", "zwei", "drei"]
注意fromkeys兩個參數(shù)的類型
注意fromkeys的調(diào)用主體
d = dict.fromkeys(l, "hahahahahah")
print(d)