Created
January 1, 2012 12:46
-
-
Save daneko/1547246 to your computer and use it in GitHub Desktop.
python 備忘 jinja2含む
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
for i, hoge in enumerate(hogelist): | |
print i | |
#的なことをjinja2でやるとこうなる | |
{% for hoge in hogelist %} | |
{{ loop.index0 }} # 0オリジン | |
{{ loop.index }} # 1オリジン | |
{% endfor %} | |
# http://ymotongpoo.appspot.com/jinja2_ja/templates.html#id19 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' | |
配列のスライスに関して | |
list[a:b:c] | |
a : start index | |
b : end index | |
c : step数 → 無くてもOK | |
list = [ i for i in range(10)] | |
として | |
list[3::] : 3-9 | |
list[:3:] : 0-2 | |
list[::3] : 0,3,6,9 | |
list[::-1] : 9-0 | |
''' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ふと忘れそうなので | |
list = [{a,1},{b,2},{c,3},…] #みたいなのをイメージ | |
list.sort() # NG sortは2.7(3かもしれん)から非推奨だったかなくなるだか | |
sorted( list, cmp, key, reverse) # NG cmpは2.7(3かもしれん)から非推奨だったかなくなるだか | |
sorted( list, key, reverse) | |
# 大体keyは無名関数を使うっぽい | |
sorted( list, key=lambda x:x[0]) | |
# 第一引数は当然ソート対象 | |
# 第二引数は以下のようなものと考える | |
# lambda x ← xは for x in list ぐらいのイメージ | |
# lambda x:hoge ← hogeを返却するというイメージ | |
# lambda x:x[0] ← 上の例だと{a,1}の'a',{b,2}の'b'が返却される | |
# keyで指定された値で比較となる | |
# なお lambda x:x[0],x[1] の場合は、上の例だと{a,1}の'a',{b,2}の'b'で比較し、 | |
# 同値の場合{a,1}の'1',{b,2}の'2'で比較すると考えればOK | |
#順番維持の場合 | |
sorted( list, key=list.index) | |
#set(list)等で重複をなくした場合順番が崩れるっぽい その時とかに使用するとおいしい |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment