3.22python學習筆記
阿新 • • 發佈:2022-03-22
三元表示式
# 是if,else的另外一種寫法,合適在兩個選擇的情況下使用
def max(a,b):
if a>b:
return a
else:
return b
# 三元表示式的寫法
max = a if a > b else b
"""
三元表示式
值1 if 條件 else 值2
條件如果成立則使用值1(if前面的資料)
條件如果不成立則使用值2(else後面的資料)
三元表示式只用於二選一的情況 最好不要巢狀使用(語法不簡潔)
'''
各種生成式
用於快捷生成想要的資料型別,如列表,字典,集合。
1.列表生成式
現在想要生成一個1到10的列表
平常方法:
list1 = []
for i in range(11)
list1.append(i)
print(list1)
#生成式寫法
list2 = [i for i in range(11)]
#結果 [0,1,2,3,4,5,6,7,8,9,10]
還可以新增條件
list2 = [i for i in range(11) if i > 3]
#結果 [4,5,6,7,8,9,10]
2.字典生成式
#定義兩個列表(索引值必須相等) list1 = ['a','b','c','d'] list2 = ['1','2','3','4'] 字典生成式 dic = {list1[i]:list2[i] for i in range(len(list1))} print(dic) 執行結果: {'a': '1', 'b': '2', 'c': '3', 'd': '4'} #實現字典的key和value快速交換: dic1={'a':1,'b':2} dic2={v: k for k, v in dic1.items()} print(dic2) #{1: 'a', 2: 'b'}
3.集合生成器
a = {i for i in range(10)}
print(a)
#{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
a = {i for i in range(10) if i != 2}
print(a)
#{0, 1, 3, 4, 5, 6, 7, 8, 9}
匿名函式
#匿名函式不需要顯示地定義函式名,使用【lambda + 引數 +表示式】的方式,即:lambda 形參:返回值 現在寫兩個數相乘的函式 def f(x,y): return x*y f(2,3) # 6 用匿名函式來寫 func = lambda x,y:x*y 可以看到,上面我們把匿名函式物件賦給一個變數,只要直接呼叫該物件就可以使用匿名函式: func(2,3) # 6 你也可以給匿名函式傳入一個引數: func_2 = lambda x:x^2 func_2(3) # 9 #優點: #不用取名稱,因為給函式取名是比較頭疼的一件事,特別是函式比較多的時候 #可以直接在使用的地方定義,如果需要修改,直接找到修改即可,方便以後程式碼的維護工作 #語法結構簡單,不用使用def 函式名(引數名):這種方式定義,直接使用lambda 引數:返回值 定義即可
常見重要內建函式
map對映函式
l1 = [11, 22, 33, 44, 55]
需求:元素全部自增10
方式1:列表生成式
方式2:內建函式
def index(n):
return n + 10
res = map(index,l1)
print(res) # 迭代器(節省空間的 目前不用考慮)
print(list(res)) # [21, 32, 43, 54, 65]
res = map(lambda x: x + 10, l1)
print(list(res)) # [21, 32, 43, 54, 65]
zip拉鍊
l1 = [11, 22, 33, 44]
l2 = ['jason','kevin','tony','oscar']
需求:將兩個列表中的元素一一對應成對即可
res = zip(l1,l2) # 結果是一個迭代器
print(res) # 目前想看裡面的資料 用list轉換一下即可
print(list(res)) # [(11, 'jason'), (22, 'kevin'), (33, 'tony'), (44, 'oscar')]
'''zip可以整合多個數據集'''
l1 = [11, 22, 33, 44]
l2 = ['jason','kevin','tony','oscar']
l3 = [1,2,3,4]
l4 = [55,66,77,88]
res = zip(l1,l2,l3,l4)
print(list(res))
不使用zip也可以
res1 = [(l1[i],l2[i],l3[i],l4[i]) for i in range(len(l1))]
print(res1)
filter過濾
方式1:列表生成式
方式2:內建函式
def index(x):
return x > 30
res = filter(index,l1)
print(list(res)) # [33, 44, 55, 66]
res = filter(lambda x:x>30, l1)
print(list(res))
reduce歸總
'''以前是內建函式 現在是某個模組下面的子函式(後面講)'''
from functools import reduce
l1 = [11, 22, 33]
'''需求:講列表中所有的元素相加'''
# def index(x,y):
# return x + y
# res = reduce(index,l1)
# print(res) # 66
res = reduce(lambda x, y: x + y, l1)
print(res) # 66
res = reduce(lambda x, y: x + y, l1, 100)
print(res) # 166
"""掌握到能夠語言表達出大致意思"""
一些小方法
1.abs() 獲取絕對值(不考慮正負號)
print(abs(-10)) #10
2.all()與any()
l1 = [0, 0, 1, 0, True]
print(all(l1)) # False 資料集中必須所有的元素對應的布林值為True返回的結果才是True
print(any(l1)) # True 資料集中只要所有的元素對應的布林值有一個為True 返回的結果就是True
3.bin() oct() hex() 產生對應的進位制數
bin是2進位制 oct是8進位制 hex是16進位制
4.bytes() 型別轉換
s = '你好啊 hello world!'
print(s.encode('utf8')) # b'\xe4\xbd\xa0\xe5\xa5\xbd\xe5\x95\x8a hello world!'
print(bytes(s, 'utf8')) # b'\xe4\xbd\xa0\xe5\xa5\xbd\xe5\x95\x8a hello world!'
'''針對編碼解碼 可以使用關鍵字encode與decode 也可以使用bytes和str'''
5.callable() 判斷當前物件是否可以加括號呼叫
name = 'jason'
def index():pass
print(callable(name)) # False 變數名不能加括號呼叫
print(callable(index)) # True 函式名可以加括號呼叫
6.chr()、ord() 字元與數字的對應轉換
print(chr(65)) # A 根據數字轉字元 依據ASCII碼
print(ord('A')) # 65 根據字元轉數字 依據ASCII碼
7.dir() 返回資料型別可以呼叫的內建方法(檢視物件內部可呼叫的屬性)
print(dir(123))
print(dir('jason'))
8.divmod() 返回整數和餘數
print(divmod(250,25)) # (10, 0) 第一個引數是整數部分 第二個是餘數部分
print(divmod(251,25)) # (10, 1)
print(divmod(249,25)) # (9, 24)
9.enumerate() 列舉
name_list = ['jason', 'kevin', 'oscar', 'tony']
for name in name_list:
print(name)
for i,j in enumerate(name_list):
print(i,j) # i類似於是計數 預設從0開始
for i,j in enumerate(name_list,start=1):
print(i,j) # 還可以控制起始位置
10.eval() exec() 識別字符串中的python程式碼 使用頻率很低
eval(res):只能識別簡單邏輯的python程式碼
exec(res):能夠識別具有與一定邏輯的python程式碼