﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>BlogJava-Jeff Lee-随笔分类-python</title><link>http://www.blogjava.net/jefflee/category/32448.html</link><description /><language>zh-cn</language><lastBuildDate>Sat, 21 Jun 2008 05:22:39 GMT</lastBuildDate><pubDate>Sat, 21 Jun 2008 05:22:39 GMT</pubDate><ttl>60</ttl><item><title>Python 常用模块在线文档</title><link>http://www.blogjava.net/jefflee/archive/2008/06/21/209661.html</link><dc:creator>Jeff Lee</dc:creator><author>Jeff Lee</author><pubDate>Sat, 21 Jun 2008 03:10:00 GMT</pubDate><guid>http://www.blogjava.net/jefflee/archive/2008/06/21/209661.html</guid><wfw:comment>http://www.blogjava.net/jefflee/comments/209661.html</wfw:comment><comments>http://www.blogjava.net/jefflee/archive/2008/06/21/209661.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/jefflee/comments/commentRss/209661.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/jefflee/services/trackbacks/209661.html</trackback:ping><description><![CDATA[1、os：<a href="http://pydoc.org/2.5.1/os.html">http://pydoc.org/2.5.1/os.html</a><br /><br /><div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: rgb(0, 0, 0);">os.name的值：<br />windows：nt<br />linux：posix</span></div><br />2、sys: <a href="http://pydoc.org/2.5.1/sys.html">http://pydoc.org/2.5.1/sys.html</a><br /><img src ="http://www.blogjava.net/jefflee/aggbug/209661.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/jefflee/" target="_blank">Jeff Lee</a> 2008-06-21 11:10 <a href="http://www.blogjava.net/jefflee/archive/2008/06/21/209661.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Python Native Datatypes -- tuple</title><link>http://www.blogjava.net/jefflee/archive/2007/08/28/209670.html</link><dc:creator>Jeff Lee</dc:creator><author>Jeff Lee</author><pubDate>Tue, 28 Aug 2007 13:11:00 GMT</pubDate><guid>http://www.blogjava.net/jefflee/archive/2007/08/28/209670.html</guid><wfw:comment>http://www.blogjava.net/jefflee/comments/209670.html</wfw:comment><comments>http://www.blogjava.net/jefflee/archive/2007/08/28/209670.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/jefflee/comments/commentRss/209670.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/jefflee/services/trackbacks/209670.html</trackback:ping><description><![CDATA[
		<div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;">
				<img id="Code_Closed_Image_210817" onclick="this.style.display='none'; Code_Closed_Text_210817.style.display='none'; Code_Open_Image_210817.style.display='inline'; Code_Open_Text_210817.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ContractedBlock.gif" align="top" width="11" height="16" />
				<img id="Code_Open_Image_210817" style="display: none;" onclick="this.style.display='none'; Code_Open_Text_210817.style.display='none'; Code_Closed_Image_210817.style.display='inline'; Code_Closed_Text_210817.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedBlockStart.gif" align="top" width="11" height="16" />
				<span id="Code_Closed_Text_210817" style="border: 1px solid rgb(128, 128, 128); background-color: rgb(255, 255, 255);">Example 3.15 Defining a tuple</span>
				<span id="Code_Open_Text_210817" style="display: none;">
						<br />
						<!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>-->
						<span style="color: rgb(0, 0, 0);">&gt;&gt;&gt;</span>
						<span style="color: rgb(0, 0, 0);"> t </span>
						<span style="color: rgb(0, 0, 0);">=</span>
						<span style="color: rgb(0, 0, 0);"> (</span>
						<span style="color: rgb(128, 0, 0);">"</span>
						<span style="color: rgb(128, 0, 0);">a</span>
						<span style="color: rgb(128, 0, 0);">"</span>
						<span style="color: rgb(0, 0, 0);">, </span>
						<span style="color: rgb(128, 0, 0);">"</span>
						<span style="color: rgb(128, 0, 0);">b</span>
						<span style="color: rgb(128, 0, 0);">"</span>
						<span style="color: rgb(0, 0, 0);">, </span>
						<span style="color: rgb(128, 0, 0);">"</span>
						<span style="color: rgb(128, 0, 0);">mpilgrim</span>
						<span style="color: rgb(128, 0, 0);">"</span>
						<span style="color: rgb(0, 0, 0);">, </span>
						<span style="color: rgb(128, 0, 0);">"</span>
						<span style="color: rgb(128, 0, 0);">z</span>
						<span style="color: rgb(128, 0, 0);">"</span>
						<span style="color: rgb(0, 0, 0);">, </span>
						<span style="color: rgb(128, 0, 0);">"</span>
						<span style="color: rgb(128, 0, 0);">example</span>
						<span style="color: rgb(128, 0, 0);">"</span>
						<span style="color: rgb(0, 0, 0);">)<br /></span>
						<span style="color: rgb(0, 0, 0);">&gt;&gt;&gt;</span>
						<span style="color: rgb(0, 0, 0);"> t<br />(</span>
						<span style="color: rgb(128, 0, 0);">'</span>
						<span style="color: rgb(128, 0, 0);">a</span>
						<span style="color: rgb(128, 0, 0);">'</span>
						<span style="color: rgb(0, 0, 0);">, </span>
						<span style="color: rgb(128, 0, 0);">'</span>
						<span style="color: rgb(128, 0, 0);">b</span>
						<span style="color: rgb(128, 0, 0);">'</span>
						<span style="color: rgb(0, 0, 0);">, </span>
						<span style="color: rgb(128, 0, 0);">'</span>
						<span style="color: rgb(128, 0, 0);">mpilgrim</span>
						<span style="color: rgb(128, 0, 0);">'</span>
						<span style="color: rgb(0, 0, 0);">, </span>
						<span style="color: rgb(128, 0, 0);">'</span>
						<span style="color: rgb(128, 0, 0);">z</span>
						<span style="color: rgb(128, 0, 0);">'</span>
						<span style="color: rgb(0, 0, 0);">, </span>
						<span style="color: rgb(128, 0, 0);">'</span>
						<span style="color: rgb(128, 0, 0);">example</span>
						<span style="color: rgb(128, 0, 0);">'</span>
						<span style="color: rgb(0, 0, 0);">)<br /></span>
						<span style="color: rgb(0, 0, 0);">&gt;&gt;&gt;</span>
						<span style="color: rgb(0, 0, 0);"> t[0]<br /></span>
						<span style="color: rgb(128, 0, 0);">'</span>
						<span style="color: rgb(128, 0, 0);">a</span>
						<span style="color: rgb(128, 0, 0);">'</span>
						<span style="color: rgb(0, 0, 0);">
								<br />
						</span>
						<span style="color: rgb(0, 0, 0);">&gt;&gt;&gt;</span>
						<span style="color: rgb(0, 0, 0);"> t[−</span>
						<span style="color: rgb(0, 0, 0);">1</span>
						<span style="color: rgb(0, 0, 0);">]<br /></span>
						<span style="color: rgb(128, 0, 0);">'</span>
						<span style="color: rgb(128, 0, 0);">example</span>
						<span style="color: rgb(128, 0, 0);">'</span>
						<span style="color: rgb(0, 0, 0);">
								<br />
						</span>
						<span style="color: rgb(0, 0, 0);">&gt;&gt;&gt;</span>
						<span style="color: rgb(0, 0, 0);"> t[</span>
						<span style="color: rgb(0, 0, 0);">1</span>
						<span style="color: rgb(0, 0, 0);">:</span>
						<span style="color: rgb(0, 0, 0);">3</span>
						<span style="color: rgb(0, 0, 0);">]<br />(</span>
						<span style="color: rgb(128, 0, 0);">'</span>
						<span style="color: rgb(128, 0, 0);">b</span>
						<span style="color: rgb(128, 0, 0);">'</span>
						<span style="color: rgb(0, 0, 0);">, </span>
						<span style="color: rgb(128, 0, 0);">'</span>
						<span style="color: rgb(128, 0, 0);">mpilgrim</span>
						<span style="color: rgb(128, 0, 0);">'</span>
						<span style="color: rgb(0, 0, 0);">)</span>
				</span>
		</div>1、tuple没有任何方法<br />2、tuple和list可以相互转化<br />4、
<table border="1" cellpadding="4" cellspacing="0" rules="all" width="100%"><caption><h5 class="docTableTitle">Common tuple literals and operations</h5></caption><colgroup span="2"></colgroup><thead><tr><th class="docTableHeader"><p class="docText">Operation</p></th><th class="docTableHeader"><p class="docText">Interpretation</p></th></tr></thead><tbody><tr><td class="docTableCell"><p class="docText"><tt>( )</tt></p></td><td class="docTableCell"><p class="docText">An empty tuple</p></td></tr><tr><td class="docTableCell"><p class="docText"><tt>t1 = (0,)</tt></p></td><td class="docTableCell"><p class="docText">A one-item tuple (not an expression)</p></td></tr><tr><td class="docTableCell"><p class="docText"><tt>t2 = (0, 'Ni', 1.2, 3)</tt></p></td><td class="docTableCell"><p class="docText">A four-item tuple</p></td></tr><tr><td class="docTableCell"><p class="docText"><tt>t2 = 0, 'Ni', 1.2, 3</tt></p></td><td class="docTableCell"><p class="docText">Another four-item tuple (same as prior line)</p></td></tr><tr><td class="docTableCell"><p class="docText"><tt>t3 = ('abc', ('def', 'ghi'))</tt></p></td><td class="docTableCell"><p class="docText">Nested tuples</p></td></tr><tr><td class="docTableCell"><p class="docText"><tt>t1[i]t3[i][j]t1[i:j]len(t1)</tt></p></td><td class="docTableCell"><p class="docText">Index, slice, length</p></td></tr><tr><td class="docTableCell"><p class="docText"><tt>t1 + t2t2 * 3</tt></p></td><td class="docTableCell"><p class="docText">Concatenate, repeat</p></td></tr><tr><td class="docTableCell"><p class="docText"><tt>for x in t23 in t2</tt></p></td><td class="docTableCell"><p class="docText">Iteration, members</p></td></tr></tbody></table><br />tuple这些操作和list非常之象。。<br /><img src="/JafeLee/aggbug/140660.html" width="1" height="1" /><br /><br /><div align="right"><a style="text-decoration: none;" href="/JafeLee/" target="_blank">Jafe</a> 2007-08-28 21:11 <a href="/JafeLee/archive/2007/08/28/140660.html#Feedback" target="_blank" style="text-decoration: none;">发表评论</a></div><img src ="http://www.blogjava.net/jefflee/aggbug/209670.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/jefflee/" target="_blank">Jeff Lee</a> 2007-08-28 21:11 <a href="http://www.blogjava.net/jefflee/archive/2007/08/28/209670.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Python Native Datatypes -- list</title><link>http://www.blogjava.net/jefflee/archive/2007/08/27/209671.html</link><dc:creator>Jeff Lee</dc:creator><author>Jeff Lee</author><pubDate>Mon, 27 Aug 2007 13:11:00 GMT</pubDate><guid>http://www.blogjava.net/jefflee/archive/2007/08/27/209671.html</guid><wfw:comment>http://www.blogjava.net/jefflee/comments/209671.html</wfw:comment><comments>http://www.blogjava.net/jefflee/archive/2007/08/27/209671.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/jefflee/comments/commentRss/209671.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/jefflee/services/trackbacks/209671.html</trackback:ping><description><![CDATA[
		<div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;">
				<img id="Code_Closed_Image_193625" onclick="this.style.display='none'; Code_Closed_Text_193625.style.display='none'; Code_Open_Image_193625.style.display='inline'; Code_Open_Text_193625.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ContractedBlock.gif" align="top" width="11" height="16" />
				<img id="Code_Open_Image_193625" style="display: none;" onclick="this.style.display='none'; Code_Open_Text_193625.style.display='none'; Code_Closed_Image_193625.style.display='inline'; Code_Closed_Text_193625.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedBlockStart.gif" align="top" width="11" height="16" />
				<span id="Code_Closed_Text_193625" style="border: 1px solid rgb(128, 128, 128); background-color: rgb(255, 255, 255);">Example 3.6 Defining a List</span>
				<span id="Code_Open_Text_193625" style="display: none;">
						<br />
						<!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>-->
						<span style="color: rgb(0, 0, 0);">&gt;&gt;&gt;</span>
						<span style="color: rgb(0, 0, 0);"> li </span>
						<span style="color: rgb(0, 0, 0);">=</span>
						<span style="color: rgb(0, 0, 0);"> [</span>
						<span style="color: rgb(128, 0, 0);">"</span>
						<span style="color: rgb(128, 0, 0);">a</span>
						<span style="color: rgb(128, 0, 0);">"</span>
						<span style="color: rgb(0, 0, 0);">, </span>
						<span style="color: rgb(128, 0, 0);">"</span>
						<span style="color: rgb(128, 0, 0);">b</span>
						<span style="color: rgb(128, 0, 0);">"</span>
						<span style="color: rgb(0, 0, 0);">, </span>
						<span style="color: rgb(128, 0, 0);">"</span>
						<span style="color: rgb(128, 0, 0);">mpilgrim</span>
						<span style="color: rgb(128, 0, 0);">"</span>
						<span style="color: rgb(0, 0, 0);">, </span>
						<span style="color: rgb(128, 0, 0);">"</span>
						<span style="color: rgb(128, 0, 0);">z</span>
						<span style="color: rgb(128, 0, 0);">"</span>
						<span style="color: rgb(0, 0, 0);">, </span>
						<span style="color: rgb(128, 0, 0);">"</span>
						<span style="color: rgb(128, 0, 0);">example</span>
						<span style="color: rgb(128, 0, 0);">"</span>
						<span style="color: rgb(0, 0, 0);">]<br /></span>
						<span style="color: rgb(0, 0, 0);">&gt;&gt;&gt;</span>
						<span style="color: rgb(0, 0, 0);"> li<br />[</span>
						<span style="color: rgb(128, 0, 0);">'</span>
						<span style="color: rgb(128, 0, 0);">a</span>
						<span style="color: rgb(128, 0, 0);">'</span>
						<span style="color: rgb(0, 0, 0);">, </span>
						<span style="color: rgb(128, 0, 0);">'</span>
						<span style="color: rgb(128, 0, 0);">b</span>
						<span style="color: rgb(128, 0, 0);">'</span>
						<span style="color: rgb(0, 0, 0);">, </span>
						<span style="color: rgb(128, 0, 0);">'</span>
						<span style="color: rgb(128, 0, 0);">mpilgrim</span>
						<span style="color: rgb(128, 0, 0);">'</span>
						<span style="color: rgb(0, 0, 0);">, </span>
						<span style="color: rgb(128, 0, 0);">'</span>
						<span style="color: rgb(128, 0, 0);">z</span>
						<span style="color: rgb(128, 0, 0);">'</span>
						<span style="color: rgb(0, 0, 0);">, </span>
						<span style="color: rgb(128, 0, 0);">'</span>
						<span style="color: rgb(128, 0, 0);">example</span>
						<span style="color: rgb(128, 0, 0);">'</span>
						<span style="color: rgb(0, 0, 0);">]<br /></span>
						<span style="color: rgb(0, 0, 0);">&gt;&gt;&gt;</span>
						<span style="color: rgb(0, 0, 0);"> li[0]<br /></span>
						<span style="color: rgb(128, 0, 0);">'</span>
						<span style="color: rgb(128, 0, 0);">a</span>
						<span style="color: rgb(128, 0, 0);">'</span>
						<span style="color: rgb(0, 0, 0);">
								<br />
						</span>
						<span style="color: rgb(0, 0, 0);">&gt;&gt;&gt;</span>
						<span style="color: rgb(0, 0, 0);"> li[</span>
						<span style="color: rgb(0, 0, 0);">4</span>
						<span style="color: rgb(0, 0, 0);">]<br /></span>
						<span style="color: rgb(128, 0, 0);">'</span>
						<span style="color: rgb(128, 0, 0);">example</span>
						<span style="color: rgb(128, 0, 0);">'</span>
				</span>
		</div>1、list的元素是有序的<br />2、list可以当成数组来用，元素下标从0开始<br /><div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><img id="Code_Closed_Image_193934" onclick="this.style.display='none'; Code_Closed_Text_193934.style.display='none'; Code_Open_Image_193934.style.display='inline'; Code_Open_Text_193934.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ContractedBlock.gif" align="top" width="11" height="16" /><img id="Code_Open_Image_193934" style="display: none;" onclick="this.style.display='none'; Code_Open_Text_193934.style.display='none'; Code_Closed_Image_193934.style.display='inline'; Code_Closed_Text_193934.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedBlockStart.gif" align="top" width="11" height="16" /><span id="Code_Closed_Text_193934" style="border: 1px solid rgb(128, 128, 128); background-color: rgb(255, 255, 255);">Example 3.7 Negative List Indices</span><span id="Code_Open_Text_193934" style="display: none;"><br /><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: rgb(0, 0, 0);">&gt;&gt;&gt;</span><span style="color: rgb(0, 0, 0);"> li<br />[</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">a</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">, </span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">b</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">, </span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">mpilgrim</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">, </span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">z</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">, </span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">example</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">]<br /></span><span style="color: rgb(0, 0, 0);">&gt;&gt;&gt;</span><span style="color: rgb(0, 0, 0);"> li[−</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">]<br /></span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">example</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 0, 0);">&gt;&gt;&gt;</span><span style="color: rgb(0, 0, 0);"> li[−</span><span style="color: rgb(0, 0, 0);">3</span><span style="color: rgb(0, 0, 0);">]<br /></span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">mpilgrim</span><span style="color: rgb(128, 0, 0);">'</span></span></div>3、list的索引可以是负数，表达式L[n]的含义为：<br />（1）当0 &lt;= n &lt;= len(L)-1 时， 跟java、C中的数组元素索引一样<br />（2）当-len(L) &lt;= n &lt;= -1 时, L[n] = L[len(L)+n]<br />（3）n不再上述范围内是，程序引发IndexError<br /><div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><img id="Code_Closed_Image_204556" onclick="this.style.display='none'; Code_Closed_Text_204556.style.display='none'; Code_Open_Image_204556.style.display='inline'; Code_Open_Text_204556.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ContractedBlock.gif" align="top" width="11" height="16" /><img id="Code_Open_Image_204556" style="display: none;" onclick="this.style.display='none'; Code_Open_Text_204556.style.display='none'; Code_Closed_Image_204556.style.display='inline'; Code_Closed_Text_204556.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedBlockStart.gif" align="top" width="11" height="16" /><span id="Code_Closed_Text_204556" style="border: 1px solid rgb(128, 128, 128); background-color: rgb(255, 255, 255);">Slicing a list</span><span id="Code_Open_Text_204556" style="display: none;"><br /><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: rgb(0, 0, 0);">li </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> [</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">a</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">, </span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">b</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">, </span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">String</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">, </span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">list</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">, </span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">dictionary</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">]<br /></span><span style="color: rgb(0, 0, 255);">print</span><span style="color: rgb(0, 0, 0);"> li[0:</span><span style="color: rgb(0, 0, 0);">3</span><span style="color: rgb(0, 0, 0);">]<br /></span><span style="color: rgb(0, 0, 255);">print</span><span style="color: rgb(0, 0, 0);"> li[:]<br /></span><span style="color: rgb(0, 0, 255);">print</span><span style="color: rgb(0, 0, 0);"> li[:</span><span style="color: rgb(0, 0, 0);">3</span><span style="color: rgb(0, 0, 0);">]<br /></span><span style="color: rgb(0, 0, 255);">print</span><span style="color: rgb(0, 0, 0);"> li[</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">:]<br /></span><span style="color: rgb(0, 0, 255);">print</span><span style="color: rgb(0, 0, 0);"> li[0:</span><span style="color: rgb(0, 0, 0);">7</span><span style="color: rgb(0, 0, 0);">] </span><span style="color: rgb(0, 128, 0);">#</span><span style="color: rgb(0, 128, 0);"> won't cause IndexError</span><span style="color: rgb(0, 128, 0);"><br /></span><span style="color: rgb(0, 0, 255);">print</span><span style="color: rgb(0, 0, 0);"> li[</span><span style="color: rgb(0, 0, 0);">-</span><span style="color: rgb(0, 0, 0);">2</span><span style="color: rgb(0, 0, 0);">:</span><span style="color: rgb(0, 0, 0);">-</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">] </span><span style="color: rgb(0, 128, 0);">#</span><span style="color: rgb(0, 128, 0);"> identical to li[len(li)-2 : len(li)-1]=li[3:4]</span><span style="color: rgb(0, 128, 0);"><br /></span><span style="color: rgb(0, 0, 255);">print</span><span style="color: rgb(0, 0, 0);"> li[</span><span style="color: rgb(0, 0, 0);">-</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">:</span><span style="color: rgb(0, 0, 0);">-</span><span style="color: rgb(0, 0, 0);">2</span><span style="color: rgb(0, 0, 0);">] </span><span style="color: rgb(0, 128, 0);">#</span><span style="color: rgb(0, 128, 0);"> identical to li[4:3], so it will return a empty list</span><span style="color: rgb(0, 128, 0);"><br /></span><span style="color: rgb(0, 0, 255);">print</span><span style="color: rgb(0, 0, 0);"> li[</span><span style="color: rgb(0, 0, 0);">-</span><span style="color: rgb(0, 0, 0);">125</span><span style="color: rgb(0, 0, 0);">:</span><span style="color: rgb(0, 0, 0);">1159</span><span style="color: rgb(0, 0, 0);">]</span></span></div>运行结果：<br /><div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><img id="Code_Closed_Image_204535" onclick="this.style.display='none'; Code_Closed_Text_204535.style.display='none'; Code_Open_Image_204535.style.display='inline'; Code_Open_Text_204535.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ContractedBlock.gif" align="top" width="11" height="16" /><img id="Code_Open_Image_204535" style="display: none;" onclick="this.style.display='none'; Code_Open_Text_204535.style.display='none'; Code_Closed_Image_204535.style.display='inline'; Code_Closed_Text_204535.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedBlockStart.gif" align="top" width="11" height="16" /><span id="Code_Closed_Text_204535" style="border: 1px solid rgb(128, 128, 128); background-color: rgb(255, 255, 255);">运行结果</span><span id="Code_Open_Text_204535" style="display: none;"><br /><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: rgb(0, 0, 0);">[</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">a</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">, </span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">b</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">, </span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">String</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">]<br />[</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">a</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">, </span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">b</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">, </span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">String</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">, </span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">list</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">, </span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">dictionary</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">]<br />[</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">a</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">, </span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">b</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">, </span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">String</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">]<br />[</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">b</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">, </span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">String</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">, </span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">list</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">, </span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">dictionary</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">]<br />[</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">a</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">, </span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">b</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">, </span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">String</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">, </span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">list</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">, </span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">dictionary</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">]<br />[</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">list</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">]<br />[]<br />[</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">a</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">, </span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">b</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">, </span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">String</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">, </span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">list</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">, </span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">dictionary</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">]<br /></span></span></div>4、L[a:b]（其中a,b为整数）表示一个分片操作，注意a，b可以是任意的整数，而且分片操作永远不会引发IndexError。但是a，b取不同的值时，程序行为稍有不同。<br />（1）当 0 &lt;= a &lt; b &lt; len(L)时，L[a:b]返回一个由L[a], L[a+1]...L[b-1]组成的list<br />（2）当 0 &lt;= b &lt;= a &lt; len(L)时，L[a:b]返回空的list<br />（3）当a，b取值不在上述范围时，应用以下4条规则替换a，b的值，得到a，b新的值后再应用上面两条规则。<br /><ul><li>若 a &gt;= len(L), 则 a = len(L)-1</li><li>若 -len(L) &lt;= a &lt; 0, 则 a = len(L) + a</li><li>若 a &lt;  -len(L)， 则  a =  0</li><li>b的情况跟a一样 <br /></li><li>当 a没有指定时，a = 0, 当b没指定时，b=len(L)-1</li></ul>5、append和extend方法的不同，看下例就一目了然了。<br /><div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><img id="Code_Closed_Image_205408" onclick="this.style.display='none'; Code_Closed_Text_205408.style.display='none'; Code_Open_Image_205408.style.display='inline'; Code_Open_Text_205408.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ContractedBlock.gif" align="top" width="11" height="16" /><img id="Code_Open_Image_205408" style="display: none;" onclick="this.style.display='none'; Code_Open_Text_205408.style.display='none'; Code_Closed_Image_205408.style.display='inline'; Code_Closed_Text_205408.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedBlockStart.gif" align="top" width="11" height="16" /><span id="Code_Closed_Text_205408" style="border: 1px solid rgb(128, 128, 128); background-color: rgb(255, 255, 255);">the difference between extend and append</span><span id="Code_Open_Text_205408" style="display: none;"><br /><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: rgb(0, 0, 0);">&gt;&gt;&gt;</span><span style="color: rgb(0, 0, 0);"> li </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> [</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">a</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">, </span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">b</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">, </span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">c</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">]<br /></span><span style="color: rgb(0, 0, 0);">&gt;&gt;&gt;</span><span style="color: rgb(0, 0, 0);"> li.extend([</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">d</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">, </span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">e</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">, </span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">f</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">])<br /></span><span style="color: rgb(0, 0, 0);">&gt;&gt;&gt;</span><span style="color: rgb(0, 0, 0);"> li<br />[</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">a</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">, </span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">b</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">, </span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">c</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">, </span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">d</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">, </span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">e</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">, </span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">f</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">]<br /></span><span style="color: rgb(0, 0, 0);">&gt;&gt;&gt;</span><span style="color: rgb(0, 0, 0);"> len(li)<br /></span><span style="color: rgb(0, 0, 0);">6</span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 0, 0);">&gt;&gt;&gt;</span><span style="color: rgb(0, 0, 0);"> li[−</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">]<br /></span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">f</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 0, 0);">&gt;&gt;&gt;</span><span style="color: rgb(0, 0, 0);"> li </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> [</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">a</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">, </span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">b</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">, </span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">c</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">]<br /></span><span style="color: rgb(0, 0, 0);">&gt;&gt;&gt;</span><span style="color: rgb(0, 0, 0);"> li.append([</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">d</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">, </span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">e</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">, </span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">f</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">])<br /></span><span style="color: rgb(0, 0, 0);">&gt;&gt;&gt;</span><span style="color: rgb(0, 0, 0);"> li<br />[</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">a</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">, </span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">b</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">, </span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">c</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">, [</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">d</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">, </span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">e</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">, </span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">f</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">]]<br /></span><span style="color: rgb(0, 0, 0);">&gt;&gt;&gt;</span><span style="color: rgb(0, 0, 0);"> len(li)<br /></span><span style="color: rgb(0, 0, 0);">4</span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 0, 0);">&gt;&gt;&gt;</span><span style="color: rgb(0, 0, 0);"> li[−</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">]<br />[</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">d</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">, </span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">e</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">, </span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">f</span><span style="color: rgb(128, 0, 0);">'</span></span></div>注意 extend的参数不仅仅可以是list，也可以是一个tuple, dictionary等可以iterable的数据结构<br />6、
<p></p><table cellpadding="4" cellspacing="0" rules="all" width="100%" frame="hsides"><caption><h5 class="docTableTitle">List object methods</h5></caption><colgroup span="2"><col /><col /></colgroup><thead><tr><th class="thead" scope="col" align="left"><p class="docText">Method</p></th><th class="thead" scope="col" align="left"><p class="docText">Description</p></th></tr></thead><tbody><tr><td class="docTableCell" align="left"><p class="docText"><span class="docEmphStrong">Nonmutating methods</span></p></td><td class="docTableCell"> </td></tr><tr><td class="docTableCell" align="left"><p class="docText"><tt><i>L</i></tt><tt>.count(</tt><tt><i>x</i></tt><tt>)</tt></p></td><td class="docTableCell" align="left"><p class="docText">Returns the number of items of <tt><i>L</i></tt> that are equal 
to <tt><i>x</i></tt>.</p></td></tr><tr><td class="docTableCell" align="left"><p class="docText"><tt><i>L</i></tt><tt>.index(</tt><tt><i>x</i></tt><tt>)</tt></p></td><td class="docTableCell" align="left"><p class="docText">Returns the index of the first occurrence of an item in 
<tt><i>L</i></tt> that is equal to <tt><i>x</i></tt>, or raises an exception if 
<tt><i>L</i></tt> has no such item.</p></td></tr><tr><td class="docTableCell" align="left"><p class="docText"><span class="docEmphStrong">Mutating methods</span></p></td><td class="docTableCell"> </td></tr><tr><td class="docTableCell" align="left"><p class="docText"><tt><i>L</i></tt>.append(<tt><i>x</i></tt>)</p></td><td class="docTableCell" align="left"><p class="docText">Appends item <tt><i>x</i></tt> to the end of <tt><i>L</i></tt> 
; e.g., 
<tt><i>L</i></tt><tt>[len(</tt><tt><i>L</i></tt><tt>):]=[</tt><tt><i>x</i></tt><tt>]</tt>.</p></td></tr><tr><td class="docTableCell" align="left"><p class="docText"><tt><i>L</i></tt>.extend(<tt><i>s</i></tt>)</p></td><td class="docTableCell" align="left"><p class="docText">Appends all the items of iterable <tt><i>s</i></tt> to the end 
of <tt><i>L</i></tt>; e.g., 
<tt><i>L</i></tt><tt>[len(</tt><tt><i>L</i></tt><tt>):]=</tt><tt><i>s</i></tt>.</p></td></tr><tr><td class="docTableCell" align="left"><p class="docText"><tt><i>L</i></tt>.insert(<tt><i>i, x</i></tt>)</p></td><td class="docTableCell" align="left"><p class="docText">Inserts item <tt><i>x</i></tt> in <tt><i>L</i></tt> before the 
item at index <tt><i>i</i></tt>, moving following items of <tt><i>L</i></tt> (if 
any) "rightward" to make space (increases 
<tt>len(</tt><tt><i>L</i></tt><tt>)</tt> by one, does not replace any item, does 
not raise exceptions: acts just like 
<tt><i>L</i></tt><tt>[</tt><tt><i>i</i></tt><tt>:</tt><tt><i>i</i></tt><tt>]=[</tt><tt><i>x</i></tt><tt>]</tt>).</p></td></tr><tr><td class="docTableCell" align="left"><p class="docText"><tt><i>L</i></tt>.remove(<tt><i>x</i></tt>)</p></td><td class="docTableCell" align="left"><p class="docText">Removes from <tt><i>L</i></tt> the first occurrence of an item 
in <tt><i>L</i></tt> that is equal to <tt><i>x</i></tt>, or raises an exception 
if <tt><i>L</i></tt> has no such item.</p></td></tr><tr><td class="docTableCell" align="left"><p class="docText"><tt><i>L</i></tt>.pop([<tt><i>i</i></tt>])</p></td><td class="docTableCell" align="left"><p class="docText">Returns the value of the item at index <tt><i>i</i></tt> and 
removes it from <tt><i>L</i></tt>; if <tt><i>i</i></tt> is omitted, removes and 
returns the last item; raises an exception if <tt><i>L</i></tt> is empty or 
<tt><i>i</i></tt> is an invalid index in <tt><i>L</i></tt>.</p></td></tr><tr><td class="docTableCell" align="left"><p class="docText"><tt><i>L</i></tt>.reverse( )</p></td><td class="docTableCell" align="left"><p class="docText">Reverses, in place, the items of 
<tt><i>L</i></tt>.</p></td></tr><tr><td class="docTableCell" align="left"><p class="docText"><tt><i>L</i></tt><tt>.sort([</tt><tt><i>f</i></tt><tt>])</tt> 
(2.3)</p></td><td class="docTableCell" align="left"><p class="docText">Sorts, in place, the items of <tt><i>L</i></tt>, comparing 
items pairwise via function <tt><i>f</i></tt>; if <tt><i>f</i></tt> is omitted, 
comparison is via the built-in function <tt>cmp</tt>. For more details, see "<a class="docLink" href="#pythonian-CHP-4-SECT-6.4.4">Sorting a list</a>" on page 
57.</p></td></tr><tr><td class="docTableCell" align="left"><p class="docText"><tt><i>L</i></tt><tt>.sort(cmp=cmp, key=None, 
reverse=False)</tt>(2.4)</p></td><td class="docTableCell" align="left"><p class="docText">Sorts, in-place, the items of <tt><i>L</i></tt>, comparing 
items pairwise via the function passed as <tt><i>cmp</i></tt> (by default, the 
built-in function <tt>cmp</tt>). When argument <tt><i>key</i></tt> is not 
<tt>None</tt>, what gets compared for each item <tt><i>x</i></tt> is 
<tt><i>key</i></tt><tt>(</tt><tt><i>x</i></tt><tt>)</tt>, not <tt><i>x</i></tt> 
itself. For more details, see "<a class="docLink" href="#pythonian-CHP-4-SECT-6.4.4">Sorting a list</a>" on page 
57.</p></td></tr></tbody></table>7、<div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><img id="Code_Closed_Image_210549" onclick="this.style.display='none'; Code_Closed_Text_210549.style.display='none'; Code_Open_Image_210549.style.display='inline'; Code_Open_Text_210549.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ContractedBlock.gif" align="top" width="11" height="16" /><img id="Code_Open_Image_210549" style="display: none;" onclick="this.style.display='none'; Code_Open_Text_210549.style.display='none'; Code_Closed_Image_210549.style.display='inline'; Code_Closed_Text_210549.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedBlockStart.gif" align="top" width="11" height="16" /><span id="Code_Closed_Text_210549" style="border: 1px solid rgb(128, 128, 128); background-color: rgb(255, 255, 255);">List Operators</span><span id="Code_Open_Text_210549" style="display: none;"><br /><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: rgb(0, 0, 0);">&gt;&gt;&gt;</span><span style="color: rgb(0, 0, 0);"> li </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> [</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">a</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">, </span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">b</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">, </span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">mpilgrim</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">]<br /></span><span style="color: rgb(0, 0, 0);">&gt;&gt;&gt;</span><span style="color: rgb(0, 0, 0);"> li </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> li </span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);"> [</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">example</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">, </span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">new</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">]<br /></span><span style="color: rgb(0, 0, 0);">&gt;&gt;&gt;</span><span style="color: rgb(0, 0, 0);"> li<br />[</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">a</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">, </span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">b</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">, </span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">mpilgrim</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">, </span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">example</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">, </span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">new</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">]<br /></span><span style="color: rgb(0, 0, 0);">&gt;&gt;&gt;</span><span style="color: rgb(0, 0, 0);"> li </span><span style="color: rgb(0, 0, 0);">+=</span><span style="color: rgb(0, 0, 0);"> [</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">two</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">]<br /></span><span style="color: rgb(0, 0, 0);">&gt;&gt;&gt;</span><span style="color: rgb(0, 0, 0);"> li<br />[</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">a</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">, </span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">b</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">, </span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">mpilgrim</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">, </span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">example</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">, </span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">new</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">, </span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">two</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">]<br /></span><span style="color: rgb(0, 0, 0);">&gt;&gt;&gt;</span><span style="color: rgb(0, 0, 0);"> li </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> [</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">, </span><span style="color: rgb(0, 0, 0);">2</span><span style="color: rgb(0, 0, 0);">] </span><span style="color: rgb(0, 0, 0);">*</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">3</span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 0, 0);">&gt;&gt;&gt;</span><span style="color: rgb(0, 0, 0);"> li<br />[</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">, </span><span style="color: rgb(0, 0, 0);">2</span><span style="color: rgb(0, 0, 0);">, </span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">, </span><span style="color: rgb(0, 0, 0);">2</span><span style="color: rgb(0, 0, 0);">, </span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">, </span><span style="color: rgb(0, 0, 0);">2</span><span style="color: rgb(0, 0, 0);">]</span></span></div>注意下面的例子：<br /><div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><img id="Code_Closed_Image_211019" onclick="this.style.display='none'; Code_Closed_Text_211019.style.display='none'; Code_Open_Image_211019.style.display='inline'; Code_Open_Text_211019.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ContractedBlock.gif" align="top" width="11" height="16" /><img id="Code_Open_Image_211019" style="display: none;" onclick="this.style.display='none'; Code_Open_Text_211019.style.display='none'; Code_Closed_Image_211019.style.display='inline'; Code_Closed_Text_211019.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedBlockStart.gif" align="top" width="11" height="16" /><span id="Code_Closed_Text_211019" style="border: 1px solid rgb(128, 128, 128); background-color: rgb(255, 255, 255);">Another Example</span><span id="Code_Open_Text_211019" style="display: none;"><br /><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: rgb(0, 0, 0);">li </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> [</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">a</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">]<br /></span><span style="color: rgb(0, 0, 255);">print</span><span style="color: rgb(0, 0, 0);"> li<br /><br /></span><span style="color: rgb(0, 128, 0);">#</span><span style="color: rgb(0, 128, 0);">li = li + 'b'  TypeError: can only concatenate list (not "str") to list</span><span style="color: rgb(0, 128, 0);"><br /></span><span style="color: rgb(0, 0, 0);"><br />li </span><span style="color: rgb(0, 0, 0);">+=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">b</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 0, 255);">print</span><span style="color: rgb(0, 0, 0);"> li<br /><br />li </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> li </span><span style="color: rgb(0, 0, 0);">*</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">2</span><span style="color: rgb(0, 0, 0);">                      <br /></span><span style="color: rgb(0, 0, 255);">print</span><span style="color: rgb(0, 0, 0);"> li<br /><br />li </span><span style="color: rgb(0, 0, 0);">*=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">2</span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 0, 255);">print</span><span style="color: rgb(0, 0, 0);"> li</span></span></div>运行结果：<br /><div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><img id="Code_Closed_Image_211049" onclick="this.style.display='none'; Code_Closed_Text_211049.style.display='none'; Code_Open_Image_211049.style.display='inline'; Code_Open_Text_211049.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ContractedBlock.gif" align="top" width="11" height="16" /><img id="Code_Open_Image_211049" style="display: none;" onclick="this.style.display='none'; Code_Open_Text_211049.style.display='none'; Code_Closed_Image_211049.style.display='inline'; Code_Closed_Text_211049.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedBlockStart.gif" align="top" width="11" height="16" /><span id="Code_Closed_Text_211049" style="border: 1px solid rgb(128, 128, 128); background-color: rgb(255, 255, 255);">运行结果</span><span id="Code_Open_Text_211049" style="display: none;"><br /><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: rgb(0, 0, 0);">[</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">a</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">]<br />[</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">a</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">, </span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">b</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">]<br />[</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">a</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">, </span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">b</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">, </span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">a</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">, </span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">b</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">]<br />[</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">a</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">, </span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">b</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">, </span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">a</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">, </span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">b</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">, </span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">a</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">, </span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">b</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">, </span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">a</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">, </span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(128, 0, 0);">b</span><span style="color: rgb(128, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">]<br /></span></span></div><br /><img src="/JafeLee/aggbug/140131.html" width="1" height="1" /><br /><br /><div align="right"><a style="text-decoration: none;" href="/JafeLee/" target="_blank">Jafe</a> 2007-08-27 21:11 <a href="/JafeLee/archive/2007/08/27/140131.html#Feedback" target="_blank" style="text-decoration: none;">发表评论</a></div><img src ="http://www.blogjava.net/jefflee/aggbug/209671.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/jefflee/" target="_blank">Jeff Lee</a> 2007-08-27 21:11 <a href="http://www.blogjava.net/jefflee/archive/2007/08/27/209671.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Python Native Datatypes -- dictionary</title><link>http://www.blogjava.net/jefflee/archive/2007/08/26/209673.html</link><dc:creator>Jeff Lee</dc:creator><author>Jeff Lee</author><pubDate>Sun, 26 Aug 2007 03:31:00 GMT</pubDate><guid>http://www.blogjava.net/jefflee/archive/2007/08/26/209673.html</guid><wfw:comment>http://www.blogjava.net/jefflee/comments/209673.html</wfw:comment><comments>http://www.blogjava.net/jefflee/archive/2007/08/26/209673.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/jefflee/comments/commentRss/209673.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/jefflee/services/trackbacks/209673.html</trackback:ping><description><![CDATA[
		<div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;">
				<img id="Code_Closed_Image_104922" onclick="this.style.display='none'; Code_Closed_Text_104922.style.display='none'; Code_Open_Image_104922.style.display='inline'; Code_Open_Text_104922.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ContractedBlock.gif" align="top" width="11" height="16" />
				<img id="Code_Open_Image_104922" style="display: none;" onclick="this.style.display='none'; Code_Open_Text_104922.style.display='none'; Code_Closed_Image_104922.style.display='inline'; Code_Closed_Text_104922.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedBlockStart.gif" align="top" width="11" height="16" />
				<span id="Code_Closed_Text_104922" style="border: 1px solid rgb(128, 128, 128); background-color: rgb(255, 255, 255);">Example 3.1 Defining a Dictionary</span>
				<span id="Code_Open_Text_104922" style="display: none;">
						<br />
						<!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>-->
						<span style="color: rgb(0, 0, 0);">&gt;&gt;&gt;</span>
						<span style="color: rgb(0, 0, 0);"> d </span>
						<span style="color: rgb(0, 0, 0);">=</span>
						<span style="color: rgb(0, 0, 0);"> {</span>
						<span style="color: rgb(128, 0, 0);">"</span>
						<span style="color: rgb(128, 0, 0);">server</span>
						<span style="color: rgb(128, 0, 0);">"</span>
						<span style="color: rgb(0, 0, 0);">:</span>
						<span style="color: rgb(128, 0, 0);">"</span>
						<span style="color: rgb(128, 0, 0);">mpilgrim</span>
						<span style="color: rgb(128, 0, 0);">"</span>
						<span style="color: rgb(0, 0, 0);">, </span>
						<span style="color: rgb(128, 0, 0);">"</span>
						<span style="color: rgb(128, 0, 0);">database</span>
						<span style="color: rgb(128, 0, 0);">"</span>
						<span style="color: rgb(0, 0, 0);">:</span>
						<span style="color: rgb(128, 0, 0);">"</span>
						<span style="color: rgb(128, 0, 0);">master</span>
						<span style="color: rgb(128, 0, 0);">"</span>
						<span style="color: rgb(0, 0, 0);">}<br /></span>
						<span style="color: rgb(0, 0, 0);">&gt;&gt;&gt;</span>
						<span style="color: rgb(0, 0, 0);"> d<br />{</span>
						<span style="color: rgb(128, 0, 0);">'</span>
						<span style="color: rgb(128, 0, 0);">server</span>
						<span style="color: rgb(128, 0, 0);">'</span>
						<span style="color: rgb(0, 0, 0);">: </span>
						<span style="color: rgb(128, 0, 0);">'</span>
						<span style="color: rgb(128, 0, 0);">mpilgrim</span>
						<span style="color: rgb(128, 0, 0);">'</span>
						<span style="color: rgb(0, 0, 0);">, </span>
						<span style="color: rgb(128, 0, 0);">'</span>
						<span style="color: rgb(128, 0, 0);">database</span>
						<span style="color: rgb(128, 0, 0);">'</span>
						<span style="color: rgb(0, 0, 0);">: </span>
						<span style="color: rgb(128, 0, 0);">'</span>
						<span style="color: rgb(128, 0, 0);">master</span>
						<span style="color: rgb(128, 0, 0);">'</span>
						<span style="color: rgb(0, 0, 0);">}<br /></span>
						<span style="color: rgb(0, 0, 0);">&gt;&gt;&gt;</span>
						<span style="color: rgb(0, 0, 0);"> d[</span>
						<span style="color: rgb(128, 0, 0);">"</span>
						<span style="color: rgb(128, 0, 0);">server</span>
						<span style="color: rgb(128, 0, 0);">"</span>
						<span style="color: rgb(0, 0, 0);">]<br /></span>
						<span style="color: rgb(128, 0, 0);">'</span>
						<span style="color: rgb(128, 0, 0);">mpilgrim</span>
						<span style="color: rgb(128, 0, 0);">'</span>
						<span style="color: rgb(0, 0, 0);">
								<br />
						</span>
						<span style="color: rgb(0, 0, 0);">&gt;&gt;&gt;</span>
						<span style="color: rgb(0, 0, 0);"> d[</span>
						<span style="color: rgb(128, 0, 0);">"</span>
						<span style="color: rgb(128, 0, 0);">database</span>
						<span style="color: rgb(128, 0, 0);">"</span>
						<span style="color: rgb(0, 0, 0);">]<br /></span>
						<span style="color: rgb(128, 0, 0);">'</span>
						<span style="color: rgb(128, 0, 0);">master</span>
						<span style="color: rgb(128, 0, 0);">'</span>
						<span style="color: rgb(0, 0, 0);">
								<br />
						</span>
						<span style="color: rgb(0, 0, 0);">&gt;&gt;&gt;</span>
						<span style="color: rgb(0, 0, 0);"> d[</span>
						<span style="color: rgb(128, 0, 0);">"</span>
						<span style="color: rgb(128, 0, 0);">mpilgrim</span>
						<span style="color: rgb(128, 0, 0);">"</span>
						<span style="color: rgb(0, 0, 0);">]<br />Traceback (innermost last):<br />  File </span>
						<span style="color: rgb(128, 0, 0);">"</span>
						<span style="color: rgb(128, 0, 0);">&lt;interactive input&gt;</span>
						<span style="color: rgb(128, 0, 0);">"</span>
						<span style="color: rgb(0, 0, 0);">, line </span>
						<span style="color: rgb(0, 0, 0);">1</span>
						<span style="color: rgb(0, 0, 0);">, </span>
						<span style="color: rgb(0, 0, 255);">in</span>
						<span style="color: rgb(0, 0, 0);"> ?<br />KeyError: mpilgrim</span>
				</span>
		</div>1、每一个dictionary中的每一个item就是一对值：key-value。可以通过key引用value，但不能通过value引用key。<br />2、每一个key是大小写敏感的<br />3、给一个dictionary添加一个新item：dictName[newKey] = newValue。如果newKey存在，则newValue会覆盖原来的value。这也表明添加、修改item用的是相同的语法。<br />4、value的类型可以是任意的，同一个dictionary中的value的类型并不需要一致。key的类型必须是所有不可变的类型，tuple有时也可以作为key，这时候tuple不能包含可变类型，不管是直接包含还是间接包含。<br />5、del D[k]删除一个key是k的item。<br />6、操作 k in D 可以用来检查<font color="#ff0000"><b>key</b></font> k 是否包含在D的所有keys中<br />7<br /><table cellpadding="4" cellspacing="0" rules="all" width="100%" frame="hsides"><caption><h5 class="docTableTitle">Dictionary object methods</h5></caption><colgroup span="2"><col /><col /></colgroup><thead><tr><th class="thead" scope="col" align="left"><p class="docText">Method</p></th><th class="thead" scope="col" align="left"><p class="docText">Description</p></th></tr></thead><tbody><tr><td class="docTableCell" align="left"><p class="docText"><span class="docEmphStrong">Nonmutating methods</span></p></td><td class="docTableCell"> </td></tr><tr><td class="docTableCell" align="left"><p class="docText"><tt><i>D</i></tt><tt>.copy( )</tt></p></td><td class="docTableCell" align="left"><p class="docText">Returns a shallow copy of the dictionary (a copy whose items 
are the same objects as <tt><i>D</i></tt>'s, not copies thereof)</p></td></tr><tr><td class="docTableCell" align="left"><p class="docText"><tt><i>D</i></tt><tt>.has_key(</tt><tt><i>k</i></tt><tt>)</tt></p></td><td class="docTableCell" align="left"><p class="docText">Returns <tt>TRue</tt> if <tt><i>k</i></tt> is a key in 
<tt><i>D</i></tt>; otherwise, returns <tt>False</tt>, just like 
<tt><i>k</i></tt><tt>in</tt><tt><i>D</i></tt></p></td></tr><tr><td class="docTableCell" align="left"><p class="docText"><tt><i>D</i></tt><tt>.items( )</tt></p></td><td class="docTableCell" align="left"><p class="docText">Returns a new list with all items (key/value pairs) in 
<tt><i>D</i></tt></p></td></tr><tr><td class="docTableCell" align="left"><p class="docText"><tt><i>D</i></tt><tt>.keys( )</tt></p></td><td class="docTableCell" align="left"><p class="docText">Returns a new list with all keys in 
<tt><i>D</i></tt></p></td></tr><tr><td class="docTableCell" align="left"><p class="docText"><tt><i>D</i></tt><tt>.values( )</tt></p></td><td class="docTableCell" align="left"><p class="docText">Returns a new list with all values in 
<tt><i>D</i></tt></p></td></tr><tr><td class="docTableCell" align="left"><p class="docText"><tt><i>D</i></tt><tt>.iteritems( )</tt></p></td><td class="docTableCell" align="left"><p class="docText">Returns an iterator on all items (key/value pairs) in 
<tt><i>D</i></tt></p></td></tr><tr><td class="docTableCell" align="left"><p class="docText"><tt><i>D</i></tt><tt>.iterkeys( )</tt></p></td><td class="docTableCell" align="left"><p class="docText">Returns an iterator on all keys in 
<tt><i>D</i></tt></p></td></tr><tr><td class="docTableCell" align="left"><p class="docText"><tt><i>D</i></tt><tt>.itervalues( )</tt></p></td><td class="docTableCell" align="left"><p class="docText">Returns an iterator on all values in 
<tt><i>D</i></tt></p></td></tr><tr><td class="docTableCell" align="left"><p class="docText"><tt><i>D</i></tt><tt>.get(</tt><tt><i>k</i></tt><tt>[,</tt><tt><i>x</i></tt><tt>])</tt></p></td><td class="docTableCell" align="left"><p class="docText">Returns <tt><i>D</i></tt><tt>[</tt><tt><i>k</i></tt><tt>]</tt> 
if <tt><i>k</i></tt> is a key in <tt><i>D</i></tt>; otherwise, returns 
<tt><i>x</i></tt> (or <tt>None</tt>, if <tt><i>x</i></tt> is not 
given)</p></td></tr><tr><td class="docTableCell" align="left"><p class="docText"><span class="docEmphStrong">Mutating methods</span></p></td><td class="docTableCell"> </td></tr><tr><td class="docTableCell" align="left"><p class="docText"><tt><i>D</i></tt><tt>.clear( )</tt></p></td><td class="docTableCell" align="left"><p class="docText">Removes all items from <tt><i>D</i></tt>, leaving 
<tt><i>D</i></tt> empty</p></td></tr><tr><td class="docTableCell" align="left"><p class="docText"><tt><i>D</i></tt><tt>.update(</tt><tt><i>D1</i></tt><tt>)</tt></p></td><td class="docTableCell" align="left"><p class="docText">For each <tt><i>k</i></tt> in <tt><i>D1</i></tt>, sets 
<tt><i>D</i></tt><tt>[</tt><tt><i>k</i></tt><tt>]</tt> equal to 
<tt><i>D1</i></tt><tt>[</tt><tt><i>k</i></tt><tt>]</tt></p></td></tr><tr><td class="docTableCell" align="left"><p class="docText"><tt><i>D</i></tt><tt>.setdefault(</tt><tt><i>k</i></tt><tt>[,</tt><tt><i>x</i></tt><tt>])</tt></p></td><td class="docTableCell" align="left"><p class="docText">Returns <tt><i>D</i></tt><tt>[</tt><tt><i>k</i></tt><tt>]</tt> 
if <tt><i>k</i></tt> is a key in <tt><i>D</i></tt>; otherwise, sets 
<tt><i>D</i></tt><tt>[</tt><tt><i>k</i></tt><tt>]</tt> equal to 
<tt><i>x</i></tt> and returns <tt><i>x</i></tt></p></td></tr><tr><td class="docTableCell" align="left"><p class="docText"><tt><i>D</i></tt><tt>.pop(</tt><tt><i>k</i></tt><tt>[,</tt><tt><i>x</i></tt><tt>])</tt></p></td><td class="docTableCell" align="left"><p class="docText">Removes and returns 
<tt><i>D</i></tt><tt>[</tt><tt><i>k</i></tt><tt>]</tt> if <tt><i>k</i></tt> is a 
key in <tt><i>D</i></tt>; otherwise, returns <tt><i>x</i></tt> (or raises an 
exception if <tt><i>x</i></tt> is not given)</p></td></tr><tr><td class="docTableCell" align="left"><p class="docText"><tt><i>D</i></tt><tt>.popitem( )</tt></p></td><td class="docTableCell" align="left"><p class="docText">Removes and returns an arbitrary item (key/value pair) </p></td></tr></tbody></table><br /><br /><img src="/JafeLee/aggbug/139420.html" width="1" height="1" /><br /><br /><div align="right"><a style="text-decoration: none;" href="/JafeLee/" target="_blank">Jafe</a> 2007-08-26 11:31 <a href="/JafeLee/archive/2007/08/26/139420.html#Feedback" target="_blank" style="text-decoration: none;">发表评论</a></div><img src ="http://www.blogjava.net/jefflee/aggbug/209673.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/jefflee/" target="_blank">Jeff Lee</a> 2007-08-26 11:31 <a href="http://www.blogjava.net/jefflee/archive/2007/08/26/209673.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Python: Your First Python Program</title><link>http://www.blogjava.net/jefflee/archive/2007/08/25/209672.html</link><dc:creator>Jeff Lee</dc:creator><author>Jeff Lee</author><pubDate>Sat, 25 Aug 2007 14:46:00 GMT</pubDate><guid>http://www.blogjava.net/jefflee/archive/2007/08/25/209672.html</guid><wfw:comment>http://www.blogjava.net/jefflee/comments/209672.html</wfw:comment><comments>http://www.blogjava.net/jefflee/archive/2007/08/25/209672.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/jefflee/comments/commentRss/209672.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/jefflee/services/trackbacks/209672.html</trackback:ping><description><![CDATA[
		<div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;">
				<img id="Code_Closed_Image_222652" onclick="this.style.display='none'; Code_Closed_Text_222652.style.display='none'; Code_Open_Image_222652.style.display='inline'; Code_Open_Text_222652.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ContractedBlock.gif" align="top" width="11" height="16" />
				<img id="Code_Open_Image_222652" style="display: none;" onclick="this.style.display='none'; Code_Open_Text_222652.style.display='none'; Code_Closed_Image_222652.style.display='inline'; Code_Closed_Text_222652.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedBlockStart.gif" align="top" width="11" height="16" />
				<span id="Code_Closed_Text_222652" style="border: 1px solid rgb(128, 128, 128); background-color: rgb(255, 255, 255);">Example 2.1 odbchelper.py</span>
				<span id="Code_Open_Text_222652" style="display: none;">
						<br />
						<!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>-->
						<span style="color: rgb(0, 0, 255);">def</span>
						<span style="color: rgb(0, 0, 0);"> buildConnectionString(params):<br />    </span>
						<span style="color: rgb(128, 0, 0);">"""</span>
						<span style="color: rgb(128, 0, 0);">Build a connection string from a dictionary of parameters.<br />    Return string.</span>
						<span style="color: rgb(128, 0, 0);">"""</span>
						<span style="color: rgb(0, 0, 0);">
								<br />    </span>
						<span style="color: rgb(0, 0, 255);">return</span>
						<span style="color: rgb(0, 0, 0);"> </span>
						<span style="color: rgb(128, 0, 0);">"</span>
						<span style="color: rgb(128, 0, 0);">;</span>
						<span style="color: rgb(128, 0, 0);">"</span>
						<span style="color: rgb(0, 0, 0);">.join([</span>
						<span style="color: rgb(128, 0, 0);">"</span>
						<span style="color: rgb(128, 0, 0);">%s=%s</span>
						<span style="color: rgb(128, 0, 0);">"</span>
						<span style="color: rgb(0, 0, 0);"> </span>
						<span style="color: rgb(0, 0, 0);">%</span>
						<span style="color: rgb(0, 0, 0);"> (k, v) </span>
						<span style="color: rgb(0, 0, 255);">for</span>
						<span style="color: rgb(0, 0, 0);"> k, v </span>
						<span style="color: rgb(0, 0, 255);">in</span>
						<span style="color: rgb(0, 0, 0);"> params.items()])<br />    <br /></span>
						<span style="color: rgb(0, 0, 255);">if</span>
						<span style="color: rgb(0, 0, 0);">  </span>
						<span style="color: rgb(128, 0, 128);">__name__</span>
						<span style="color: rgb(0, 0, 0);"> </span>
						<span style="color: rgb(0, 0, 0);">==</span>
						<span style="color: rgb(0, 0, 0);"> </span>
						<span style="color: rgb(128, 0, 0);">"</span>
						<span style="color: rgb(128, 0, 0);">__main__</span>
						<span style="color: rgb(128, 0, 0);">"</span>
						<span style="color: rgb(0, 0, 0);">:<br />    myParams </span>
						<span style="color: rgb(0, 0, 0);">=</span>
						<span style="color: rgb(0, 0, 0);"> {</span>
						<span style="color: rgb(128, 0, 0);">"</span>
						<span style="color: rgb(128, 0, 0);">server</span>
						<span style="color: rgb(128, 0, 0);">"</span>
						<span style="color: rgb(0, 0, 0);">: </span>
						<span style="color: rgb(128, 0, 0);">"</span>
						<span style="color: rgb(128, 0, 0);">mpilgrim</span>
						<span style="color: rgb(128, 0, 0);">"</span>
						<span style="color: rgb(0, 0, 0);">, \<br />                 </span>
						<span style="color: rgb(128, 0, 0);">"</span>
						<span style="color: rgb(128, 0, 0);">database</span>
						<span style="color: rgb(128, 0, 0);">"</span>
						<span style="color: rgb(0, 0, 0);">: </span>
						<span style="color: rgb(128, 0, 0);">"</span>
						<span style="color: rgb(128, 0, 0);">master</span>
						<span style="color: rgb(128, 0, 0);">"</span>
						<span style="color: rgb(0, 0, 0);">, \<br />                 </span>
						<span style="color: rgb(128, 0, 0);">"</span>
						<span style="color: rgb(128, 0, 0);">uid</span>
						<span style="color: rgb(128, 0, 0);">"</span>
						<span style="color: rgb(0, 0, 0);">: </span>
						<span style="color: rgb(128, 0, 0);">"</span>
						<span style="color: rgb(128, 0, 0);">sa</span>
						<span style="color: rgb(128, 0, 0);">"</span>
						<span style="color: rgb(0, 0, 0);">, \<br />                 </span>
						<span style="color: rgb(128, 0, 0);">"</span>
						<span style="color: rgb(128, 0, 0);">pwd</span>
						<span style="color: rgb(128, 0, 0);">"</span>
						<span style="color: rgb(0, 0, 0);">: </span>
						<span style="color: rgb(128, 0, 0);">"</span>
						<span style="color: rgb(128, 0, 0);">secret</span>
						<span style="color: rgb(128, 0, 0);">"</span>
						<span style="color: rgb(0, 0, 0);">}<br />    </span>
						<span style="color: rgb(0, 0, 255);">print</span>
						<span style="color: rgb(0, 0, 0);"> buildConnectionString(myParams)    <br />    </span>
						<span style="color: rgb(0, 0, 255);">print</span>
						<span style="color: rgb(0, 0, 0);"> buildConnectionString.</span>
						<span style="color: rgb(128, 0, 128);">__doc__</span>
						<span style="color: rgb(0, 0, 0);">    <br />        </span>
				</span>
		</div>1、函数（function)定义无需指定返回类型。事实上，所有python函数都会返回一个值。当你的函数定义没有return语句时，函数将返回None。多个函数参数(function argument)用逗号隔开。（这里有一个疑问，为什么是function argument而不是function parameter呢？@@）<br />2、Python是一个动态的、<b><font color="#ff0000">强类型</font></b>的语言（dynamically  &amp; strongly type language)<br />3、注释函数：（documenting function)以"""开始，以"""结束，可以通过内建属性 __doc__ 获得。而且注释必须在函数最开始部分先定义，否则调用__doc__时你将得到None.<br />4、Everything is a object!<br />5、if __name__ == "__main__":<br />这是在测试模块。在上例中当模块独立运行时，模块的内建属性 __name__ == "__main__"，当模块被import时，__name__ == "odbchelper"<br /><br /><img src="/JafeLee/aggbug/139333.html" width="1" height="1" /><br /><br /><div align="right"><a style="text-decoration: none;" href="/JafeLee/" target="_blank">Jafe</a> 2007-08-25 22:46 <a href="/JafeLee/archive/2007/08/25/139333.html#Feedback" target="_blank" style="text-decoration: none;">发表评论</a></div><img src ="http://www.blogjava.net/jefflee/aggbug/209672.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/jefflee/" target="_blank">Jeff Lee</a> 2007-08-25 22:46 <a href="http://www.blogjava.net/jefflee/archive/2007/08/25/209672.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>