2014年3月30日日曜日

開発環境

Learning Python (Mark Lutz (著)、Oreilly & Associates Inc)のPART III.(Functions and Generators)、Test Your Knowledge: Part IV Exercises 、9.(Iterations and comprehensions)を解いてみる。

その他参考書籍

9.(Iterations and comprehensions)

コード(BBEdit)

sample.py

#!/usr/bin/env python3
#-*- coding: utf-8 -*-

import math

squares = [2, 4, 9, 16, 25]

roots_for_loop = []
for x in squares:
    roots_for_loop.append(math.sqrt(x))

roots_map = list(map(math.sqrt, squares))

roots_list_comprehension = [math.sqrt(x) for x in squares]

roots_generator = list(math.sqrt(x) for x in squares)

for roots in [roots_for_loop, roots_map, roots_list_comprehension,
              roots_generator]:
    print(roots)

入出力結果(Terminal)

$ ./sample.py
[1.4142135623730951, 2.0, 3.0, 4.0, 5.0]
[1.4142135623730951, 2.0, 3.0, 4.0, 5.0]
[1.4142135623730951, 2.0, 3.0, 4.0, 5.0]
[1.4142135623730951, 2.0, 3.0, 4.0, 5.0]
$

0 コメント:

コメントを投稿