Skip to content

Instantly share code, notes, and snippets.

@suzuki-hoge
Created August 23, 2016 00:07
Show Gist options
  • Save suzuki-hoge/39ce77c0f329b854004614703c4c44ae to your computer and use it in GitHub Desktop.
Save suzuki-hoge/39ce77c0f329b854004614703c4c44ae to your computer and use it in GitHub Desktop.
昔のpython置き場

なんか1年目末だか2年目だかに書いたのが出てきて面白かったから残しておく

# 関数をリストの要素にしてループするっておもしろいなと思って
# 適当に書いてみた
def echoHoge():
print 'hoge'
def echoFuga():
print 'fuga'
def echoPiyo():
print 'piyo'
# 関数を要素とする
functions = [echoHoge, echoFuga, echoPiyo]
for f in functions:
# 一つずつ取り出して呼ぶ
f()
# lambda式もいける 式だからかー
functions = [(lambda x: x * 2), (lambda x: x * 3), (lambda x: x * 4)]
for f in functions:
# もちろん引数も
print f(5) # 10, 15, 20
# pythonクックブックに面白いコードがあったので
# いろいろなバリエーション試してみた
# 関数内でフラグによって別の関数を定義して使うというもの
#
# 内容は何でもいいが、辞書から値をキーでひっぱてくるものにした
# dict.get(key, something)で、キーが存在しなかったときに
# なにか代わりに返してくれる
# dict[key]はキーがなかったときはそんなキーないよexceptionが発生
# フラグを受け取り内部で別関数定義(lambda)
# 本の記憶を頼りに書いた
def getter1(dict, key, safe = True):
if safe:
func = lambda dict, key: dict.get(key, 'boo...')
else:
func = lambda dict, key: dict[key]
return func(dict, key)
# フラグを受け取り内部で別処理
# getter1書いてる時にlambda程度なら別に定義しなくてよくね、と思って書いた
def getter2(dict, key, safe = True):
if safe:
return dict.get(key, 'boo...')
else:
return dict[key]
# 外から関数渡すのもありかなと思って書いた
def getter3(dict, key, func):
return func(dict, key)
# getter3のfunc引数ををlambdaで作るか関数返す関数で作るかの違い
def makeFunc(safe = True):
if safe:
return lambda dict, key: dict.get(key, 'boo...')
else:
return lambda dict, key: dict[key]
# getter1をちゃんと思い出した lambdaじゃあなくてdefだった
def getter4(dict, key, safe = True):
if safe:
def func(dict, key):
return dict.get(key, 'boo...')
else:
def func(dict, key):
return dict[key]
return func(dict, key)
dict = {'one' : 'hoge', 'two' : 'fuga'}
# 結果はすべて同じ boo...
print getter1(dict, 'three')
print getter2(dict, 'three')
print getter3(dict, 'three', lambda dict, key: dict.get(key, 'boo...'))
print getter3(dict, 'three', makeFunc())
print getter4(dict, 'three')
# getter1はlamdaなら定義するほどじゃあないと思ったけど、
# getter4みたいに関数定義する規模ならありかなと思った。
# getter3は外側で直接呼べよ、と。
# 結局規模と外の状況次第だよね。
# ごにょごにょやったけど、この規模ならgetter2.
# pythonのソート関数が面白いのでちょっと触ってみた
# sorted(iter, key, reverse) の形をしていて、keyの部分が面白い
# 引用 > keyパラメータは比較を行う前にリストの各要素に対して呼ばれる関数
# 要はソートする要素をちょいと整形できるということ
# いくつか試してみた
# イテレータブルならなんでもおk リスト、辞書、タプル、自作クラス等々...
tuples = [('jhon', 'B', 65), ('yo-ko', 'A', '85'), ('Kenny-G', 'C', 95)]
# 標準 1要素目となるので文字コード順
for tuple in sorted(tuples):
print tuple # Kenny-G, jhon, yo-ko ※略記 実際はtuple
# 標準の逆順
for tuple in sorted(tuples, reverse = True):
print tuple # yo-ko, jhon, Kenny-G
# ソート前にtupleをtuple[1]に置き換える
# -> 2要素目のアルファベットソート
for tuple in sorted(tuples, key = lambda tuple: tuple[1]):
print tuple # A, B, C
# ソート前にtupleをtuple[0]の小文字に置き換える
# -> 1要素目の大文字小文字を意識しないアルファベットソート
for tuple in sorted(tuples, key = lambda tuple: tuple[0].lower()):
print tuple # jhon, Kenny-G, yo-ko
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment