Pythonの高度な使い方
式のトリプレット
x = 10
y = 20
print(x if x > y else y)
x = 100
y = 20
print(x if x > y else y)
20
100
リスト内包
print([i for i in range(10)])
print([i*2 for i in range(10)])
print([i-1 for i in range(10)])
辞書・ビルダー
ZIP()内部要素タプルであるZIPオブジェクトを返し、またはタプルのリストに変換することができます。
keys = ['name', 'age', 'gender']
values = ['nick', 19, 'male']
res = zip(keys, values)
print(res)
for i in res:
print(i)
print(F"zip(keys,values): {zip(keys,values)}")
info_dict = {k: v for k, v in res}
print(f"info_dict: {info_dict}")
<0x000001D6D7870E08にジップオブジェクト>
( '名前'、 'ニック')
('年齢'、19)
('性別'、 '男性')
ZIP(キー、値):<0x000001D6D7870E88にジップオブジェクト>
info_dict:{}
無名関数
匿名関数は、変数名、関数オブジェクトではありません
res = (lambda x, y: x+y)(1, 2)
print(res)
print(lambda x, y: x+y)
3
<機能
アプリケーション(および内蔵典型的に関連する機能)
組み合わせにおける一般匿名関数MAX()、ソート()、フィルタ()、ソート()メソッド。
たとえば、匿名をフィルタリング
name_list = ['nick', 'jason sb', 'tank sb', 'sean sb']
filter_res = filter(lambda name: name.endswith('sb'), name_list)
print(f"list(filter_res): {list(filter_res)}")
リスト(filter_res):[ 'ジェイソンSB'、 'タンクSB'、 'ショーンSB']
正常な機能
name_list = ['nick', 'jason sb', 'tank sb', 'sean sb']
def zx(name):
return name.endswith('sb')
filter_res = filter(zx, name_list)
print(f"list(filter_res): {list(filter_res)}")
リスト(filter_res):[ 'ジェイソンSB'、 'タンクSB'、 'ショーンSB']