<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Yukun&#039;s Blog &#187; ASC</title>
	<atom:link href="http://www.yukun.info/blog/tag/asc/feed" rel="self" type="application/rss+xml" />
	<link>http://www.yukun.info</link>
	<description>難しいことは分かりやすく、簡単なことは面白く紹介</description>
	<lastBuildDate>Wed, 25 Aug 2010 15:38:18 +0000</lastBuildDate>
	<language>ja</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Python: 辞書の全てのキーと値をソートしてたどる - sorted()関数</title>
		<link>http://www.yukun.info/blog/2008/06/python-sorted.html</link>
		<comments>http://www.yukun.info/blog/2008/06/python-sorted.html#comments</comments>
		<pubDate>Wed, 04 Jun 2008 15:00:00 +0000</pubDate>
		<dc:creator>yukun</dc:creator>
				<category><![CDATA[Python]]></category>
		<category><![CDATA[ASC]]></category>
		<category><![CDATA[DESC]]></category>
		<category><![CDATA[Dictionary]]></category>
		<category><![CDATA[Sort]]></category>

		<guid isPermaLink="false">http://www.yukun.info/trump/19700101/python-%e8%be%9e%e6%9b%b8%e3%81%ae%e5%85%a8%e3%81%a6%e3%81%ae%e3%82%ad%e3%83%bc%e3%81%a8%e5%80%a4%e3%82%92%e3%82%bd%e3%83%bc%e3%83%88%e3%81%97%e3%81%a6%e3%81%9f%e3%81%a9%e3%82%8b-sorted%e9%96%a2</guid>
		<description><![CDATA[				ソースコード
				
#!/usr/bin/python
# coding: UTF-8

# 辞書の全てのキーと値をソートしてたどる &#124; sorted()関数の使い方

# 辞書の初期化(名前:年齢)
pro [...]]]></description>
			<content:encoded><![CDATA[				<h3>ソースコード</h3>
				<pre class="brush: python">
#!/usr/bin/python
# coding: UTF-8

# 辞書の全てのキーと値をソートしてたどる | sorted()関数の使い方

# 辞書の初期化(名前:年齢)
profile = { &#039;Hana&#039;:19, &#039;Toru&#039;:26, &#039;Katori&#039;:15, &#039;Sato&#039;:45}

# 初期化の確認: 辞書のitems()メソッドで全てのキー(key), 値(value)をたどる
for (k, v) in profile.items():
    print &#039;キー: %-6s, 値: %2d&#039; % (k, v)
print
# ↑items()はキーと値のタプルを返している。
#   (丸括弧)は省略してもOK→ for k, v in profile.items(): とも書ける

# 辞書の全てのキーをソートしてたどる
for k in sorted(profile.keys()):
    print &#039;キー: %-6s, 値: %2d&#039; % (k, profile[k])
print
# キーである文字列がここではアルファベット順にソートされる(文字コードでの昇順)
# 組み込み関数sorted()は第1引数にリストを取り、それをソートしたリストを返す。

# 辞書の全ての値をソートしてたどる
for v in sorted(profile.values()):
    print &#039;値:&#039;, v
print
# 値である整数が昇順にソートされて表示される

# 辞書の全てのキーを降順(逆順)にソートしてたどる
for k in sorted(profile.keys(), reverse=True):
    print &#039;キー: %-6s, 値: %2d&#039; % (k, profile[k])
print
# 関数sorted()の第4引数(ここではオプション引数として)に
# ソート順序を逆順にするフラグを設定 reverse=True

# 辞書の全ての値を降順(逆順)ソートしてたどる
for v in sorted(profile.values(), reverse=True):
    print &#039;値:&#039;, v
print
</pre>
				<h3>実行結果</h3>
				<pre>
キー: Toru  , 値: 26
キー: Hana  , 値: 19
キー: Katori, 値: 15
キー: Sato  , 値: 45

キー: Hana  , 値: 19
キー: Katori, 値: 15
キー: Sato  , 値: 45
キー: Toru  , 値: 26

値: 15
値: 19
値: 26
値: 45

キー: Toru  , 値: 26
キー: Sato  , 値: 45
キー: Katori, 値: 15
キー: Hana  , 値: 19

値: 45
値: 26
値: 19
値: 15
</pre>
				<h3>リファレンス</h3>
				<ul>
				<li><a href="http://docs.python.org/lib/typesmapping.html" target="_blank">3.8 Mapping Types -- dict</a></li>
				</ul>
				<h3>チュートリアル</h3>
				<ul>
				<li><a href="http://docs.python.org/tut/node6.html" target="_blank">4. More Control Flow Tools</a>
				<ul>
				<li><a href="http://docs.python.org/tut/node6.html#SECTION006200000000000000000" target="_blank">4.2 for Statements</a></li>
				</ul>
				</li>
				<li><a href="http://docs.python.org/tut/node7.html" target="_blank">5. Data Structures</a>
				<ul>
				<li><a href="http://docs.python.org/tut/node7.html#SECTION007500000000000000000" target="_blank">5.5 Dictionaries</a></li>
				<li><a href="http://docs.python.org/tut/node7.html#SECTION007600000000000000000" target="_blank">5.6 Looping Techniques</a></li>
				</ul>
				</li>
				</ul>
				<h4>関連記事</h4>
				<ul class="similar-posts">
				<li><a href="http://www.yukun.info/blog/2008/06/python-dict2.html" rel="bookmark" title="2008年6月4日">Python: 辞書の全てのキーと値をたどる - items(), keys(), values()メソッド</a></li>
				<li><a href="http://www.yukun.info/blog/2008/06/python-dict.html" rel="bookmark" title="2008年6月3日">Python: 辞書の初期化・出力・代入</a></li>
				<li><a href="http://www.yukun.info/blog/2008/08/python-if-for-in.html" rel="bookmark" title="2008年8月2日">Python: if/for文でのin演算子の各オブジェクト毎の評価</a></li>
				<li><a href="http://www.yukun.info/blog/2008/11/mysql-sort-record-order-by-asc-desc.html" rel="bookmark" title="2008年11月19日">MySQL: レコードを昇順・降順にソートして出力 - ORDER BY句</a></li>
				<li><a href="http://www.yukun.info/blog/2008/06/python-list.html" rel="bookmark" title="2008年6月1日">Python: リストの初期化・出力・代入</a></li>
				</ul>
				<p><!-- Similar Posts took 13.174 ms --></p>
]]></content:encoded>
			<wfw:commentRss>http://www.yukun.info/blog/2008/06/python-sorted.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
