開発環境
- OS X Lion - Apple(OS)
- BBEdit - Bare Bones Software, Inc.(Text Editor)
- プログラミング言語: Python
『初めてのPython 第3版』(Mark Lutz 著、夏目 大 訳、オライリー・ジャパン、2009年、ISBN978-4-87311-393-7) のIII部(ステートメント)のまとめ演習3.(ディクショナリのソート)を解いてみる。
その他参考書籍
3.(ディクショナリのソート)
コード(BBEdit)
sample.py
#!/usr/bin/env python3.3
#-*- coding: utf-8 -*-
d = {'a':0,'b':0,'c':0,'d':0,'e':0}
for key in sorted(d):
print("{0} => {1}".format(key, d[key]))
print("ソートせずに表示")
for k, v in d.items():
print("{0} => {1}".format(k, v))
入出力結果(Terminal)
$ ./sample.py a => 0 b => 0 c => 0 d => 0 e => 0 ソートせずに表示 a => 0 c => 0 b => 0 e => 0 d => 0 $
ちなみにJavaScriptの場合。
コード(BBEdit)
var o = {'a':0,'b':0,'c':0,'d':0,'e':0},
a = ['a','b','c','d','e'],
result = "",
i, max;
a.sort();
for (i = 0, max = a.length; i < max; i += 1) {
result += a[i] + " => " + o[a[i]] + "\n";
}
$('#pre0').text(result);
0 コメント:
コメントを投稿