Aidemy Python入門
lambda式
無名関数の作成
code:Python
a = 4
def func1(a):
return 2*(a**2)-3*a+1
func2 = lambda a: 2*(a**2)-3*a+1
print(func1(a))
print(func2(a))
--------------------------------------------------------------------------
21
21
--------------------------------------------------------------------------
lambdaによる計算
code: Python
x = 5
y = 6
z = 2
def func3(x, y, z):
return x*y+z
func4 = lambda x, y, z: x*y+z
# 出力
print(func3(x, y, z))
print(func4(x, y, z))
--------------------------------------------------------------------------
32
32
--------------------------------------------------------------------------
ifを用いたlambda
code: Python
a1 = 13
a2 = 32
func5 = lambda a: a ** 2 - 40 * a + 350 if 10 <= a < 30 else 50
print(func5(a1))
print(func5(a2))
--------------------------------------------------------------------------
-1
50
--------------------------------------------------------------------------
リスト内包括
listの分割(split)
code: Python
self_data = "My name is Yamada"
list = self_data.split(" ")
--------------------------------------------------------------------------
Yamada
--------------------------------------------------------------------------
listの分割(re.split)
code: Python
import re
time_data = "2017/4/1_22:15"
list = re.split("/_:", time_data) --------------------------------------------------------------------------
4
22
--------------------------------------------------------------------------
高階関数(map)
code: Python
import re
time_list = [
"2006/11/26_2:40",
"2009/1/16_23:35",
"2014/5/4_14:26",
"2017/8/9_7:5",
"2017/4/1_22:15"
]
# 文字列から"時"を取り出す関数
def getHour(data):
time_list = re.split("/_:", data) # lambda式なら
# getHour = lambda data: re.split("/_:", data)3 hour_list = list(map(getHour, time_list))
print(hour_list)
--------------------------------------------------------------------------
--------------------------------------------------------------------------
filter
code: Python
import re
time_list = [
"2006/11/26_2:40",
"2009/1/16_23:35",
"2014/5/4_14:26",
"2017/8/9_7:5",
"2017/4/1_22:15"
]
# 文字列の"月"が条件を満たすときにTrueを返す関数
get_month_one_to_six = lambda data: 1 <= int(re.split("/_:", data)1) <= 6 month_list = list(filter(get_month_one_to_six, time_list))
print(month_list)
--------------------------------------------------------------------------
--------------------------------------------------------------------------
sorted
code: Python
time_data = [
]
# "時"をキーとしてソートする
list = sorted(time_data, key=lambda x: x3) print(list)
--------------------------------------------------------------------------
2006, 11, 26, 2, 40], 2017, 8, 9, 7, 5, 2014, 5, 4, 14, 26, 2017, 4, 1, 22, 15, [2009, 1, 16, 23, 35 --------------------------------------------------------------------------
リストの生成
code: Python
# minute_data、単位は分
# リスト内包表記
print(h_m_data)
--------------------------------------------------------------------------
0, 30], 2, 35, 3, 0, 1, 14, 0, 11, 1, 0, [1, 22 --------------------------------------------------------------------------
if文を用いたループ
code: Python
# minute_data、単位は分
# リスト内包表記
print(just_hour_data)
--------------------------------------------------------------------------
--------------------------------------------------------------------------
複数配列の同時ループ
code: Python
# 時間データhour, 分データminute
# 時, 分を引数に、分に換算する関数
convert_to_minute = lambda hour, minute: hour * 60 + minute
# リスト内包表記
print(minute_data)
--------------------------------------------------------------------------
--------------------------------------------------------------------------
多重ループ
code: Python
# 二進数の桁
# リスト内包表記の多重ループを用いて0から7までの整数を配列に
print(digit)
--------------------------------------------------------------------------
--------------------------------------------------------------------------
辞書オブジェクト
defaultdict
code: Python
from collections import defaultdict
# 文字列description
description = \
"Artificial intelligence (AI, also machine intelligence, MI) is " + \
"intelligence exhibited by machines, rather than " + \
"humans or other animals (natural intelligence, NI)."
# defaultdictを定義
char_freq = defaultdict(int)
# 文字の出現回数を記録
for i in description:
# ソートし、上位10要素を出力
# sorted関数は、sorted(ソート対象、ソートに使うkey、ソートオプション)で呼び出す
# ソートオプションはreverse=Trueで降順に
# ソートに使うkeyはitemsを指定して(アルファベット、出現回数)のリスト形式で取り出し、lambdaで「リストの二番目」、つまり出現回数を、x1として指定 print(sorted(char_freq.items(), key=lambda x: x1, reverse=True):10) --------------------------------------------------------------------------
', 20), ('e', 18), ('i', 17), ('n', 14), ('l', 12), ('a', 11), ('t', 10), ('c', 7), ('h', 7), ('r', 6)
--------------------------------------------------------------------------
value内の要素の追加
code: Python
from collections import defaultdict
# まとめたいデータprice
price = [
("apple", 50),
("banana", 120),
("grape", 500),
("apple", 70),
("lemon", 150),
("grape", 1000)
]
# defaultdictを定義
d = defaultdict(list)
# 上記の例と同様にvalueの要素に値段を追加
for key, value in price:
# 各valueの平均値を計算し、配列にして出力
--------------------------------------------------------------------------
--------------------------------------------------------------------------
Counter
code: Python
from collections import Counter
# 文字列description
description = \
"Artificial intelligence (AI, also machine intelligence, MI) is " + \
"intelligence exhibited by machines, rather than " + \
"humans or other animals (natural intelligence, NI)."
# Counterを定義
char_freq = Counter(description)
# ソートし、上位10要素を出力
print(char_freq.most_common(10))
--------------------------------------------------------------------------
', 20), ('e', 18), ('i', 17), ('n', 14), ('l', 12), ('a', 11), ('t', 10), ('c', 7), ('h', 7), ('r', 6)
--------------------------------------------------------------------------