開発環境
- OS X Lion - Apple(OS)
- TextWrangler(Text Editor) (BBEditの無料機能制限版、light版)
- Script言語: Python
『初めてのPython 第3版』(Mark Lutz 著、夏目 大 訳、オライリー・ジャパン、2009年、ISBN978-4-87311-393-7) のIII部(ステートメント)13章(whileループとforループ)4を解いてみる。
4.
リスト内包表記はちょっと書き方の違うforループ。
厳密にはforループよりリスト内包表記の方が実行速度が早いという利点がある。
あと、リスト内包表記の方がコードが簡潔になる。
コード(TextWrangler)
sample.py
#!/usr/bin/env python #encoding: utf-8 L = [1,2,3,4,5]; M = [] for x in L: M.append(pow(x,2)) N = [pow(x,2) for x in L] for l in [L,M,N]: print(l)
入出力結果(Terminal)
$ ./sample.py [1, 2, 3, 4, 5] [1, 4, 9, 16, 25] [1, 4, 9, 16, 25] $
0 コメント:
コメントを投稿