2014年9月4日木曜日

開発環境

Practical Programming: An Introduction to Computer Science Using Python 3 (Pragmatic Programmers) (Paul Gries (著)、Jennifer Campbell (著)、Jason Montojo (著)、Lynn Beighley (編集)、Pragmatic Bookshelf)のChapter 9(Repeating Code Using Loops)、9.10(Exercises) 3.を解いてみる。

9.10(Exercises) 3.

コード(BBEdit)

sample3.py

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

whales = [5,4,7,3,2,3,2,6,4,2,1,7,1,3]

more_whales = []
for whale in whales:
    more_whales.append(whale + 1)

more_whales1 = [whale + 1 for whale in whales]

more_whales2 = list(map(lambda whale: whale + 1, whales))

print(whales, more_whales, more_whales1, more_whales2, sep='\n')

入出力結果(Terminal, IPython)

$ ./sample3.py
[5, 4, 7, 3, 2, 3, 2, 6, 4, 2, 1, 7, 1, 3]
[6, 5, 8, 4, 3, 4, 3, 7, 5, 3, 2, 8, 2, 4]
[6, 5, 8, 4, 3, 4, 3, 7, 5, 3, 2, 8, 2, 4]
[6, 5, 8, 4, 3, 4, 3, 7, 5, 3, 2, 8, 2, 4]
$

0 コメント:

コメントを投稿