I learned string.format() today. template = “{foo}, {bar}, {baz}” print template.format(foo=”FOO”, bar=”BAR”, baz=”BAZ”) >> FOO, BAR, BAZ
Python: True or False
I learned all python objects can be classified “True” or “False”. “”, [], {}, None are evaluated “False”. def check(data): if( filter(unicode.uppercase, data) and filter(unicode.lowercase, data) ): return True else: return False str = “aaa” print check(str) >>False str =…
Python: count
When I try CheckIO(http://www.checkio.org/), I couldn’t find a good way to count duplicate elements of a list. lst = [1, 3, 3, 4, 4, 5] lst.count(3) >>2 print [x for x in lst if lst.count(x) > 1] >>[3, 3, 4,…
Python: itertools.takewhile
from itertools import takewhile from itertools import count def count_up(start, step, stop): return takewhile(lambda x: x < stop, count(start, step)) print list(count_up(1, 3, 10)) # >>[1, 4, 7] print list(count_up(1, 6, 30)) # >>[1, 7, 13, 19, 25] https://gist.github.com/aminami1127/2e92331b71fedc1b6d47 By…
Python: itertools.count
By itertools.count, we can easily create count up generateor. def count_up(start, stop, step): cnt = itertools.count(start, step) for i in cnt: if i > stop: break: else: yield i print list(count_up(1, 10, 2)) >>[1, 3, 5, 7, 9] https://gist.github.com/aminami1127/e0e29b94ead611fd65f3
Python: functools.partial
Pythonを勉強中のため、日々自分用の技術メモを書いていこうと思います。 I’m studying python, and I try to write daily technical memo for myself. functools.partialについて学習しました。 I learned functools.partial today. partialを使うと簡単に関数の部分適用が行えます。 import functools import math #paramter PI and radius #引数として円周率と半径を受け取り、面積を返す circle_squre = lambda pi, r: pi * r * r #円周率の値をpartialによって3に束縛した関数…