2012年3月30日金曜日

開発環境

『初めてのPython 第3版』(Mark Lutz 著、夏目 大 訳、オライリー・ジャパン、2009年、ISBN978-4-87311-393-7) のIII部(ステートメント)のまとめ演習3(ディクショナリのソート)を解いてみる。

3.

入出力結果(Terminal)

$ python
Python 2.7.2 (default, Feb 12 2012, 23:50:38) 
[GCC 4.2.1 Compatible Apple Clang 3.0 (tags/Apple/clang-211.12)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> d={'a':1,'b':2,'c':3,'d':4,'e':5}
>>> for k in d.keys():
...     print k,' => ',d[k]
... 
a  =>  1
c  =>  3
b  =>  2
e  =>  5
d  =>  4
>>> keys = d.keys()
>>> for k in keys:
...     print k,' => ',d[k]
... 
a  =>  1
c  =>  3
b  =>  2
e  =>  5
d  =>  4
>>> keys.sort()
>>> for k in keys:
...     print k,' => ',d[k]
... 
a  =>  1
b  =>  2
c  =>  3
d  =>  4
e  =>  5
>>> keys
['a', 'b', 'c', 'd', 'e']
>>> d
{'a': 1, 'c': 3, 'b': 2, 'e': 5, 'd': 4}
>>> d1=sorted(d)
>>> d1
['a', 'b', 'c', 'd', 'e']
>>> for k in sorted(d):
...     print k,' => ',d[k]
... 
a  =>  1
b  =>  2
c  =>  3
d  =>  4
e  =>  5
>>> d
{'a': 1, 'c': 3, 'b': 2, 'e': 5, 'd': 4}
>>> quit()
$

0 コメント:

コメントを投稿