﻿<?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>C++博客-andimeo-文章分类-Python</title><link>http://www.cppblog.com/andimeo/category/19218.html</link><description /><language>zh-cn</language><lastBuildDate>Fri, 27 Apr 2012 20:39:33 GMT</lastBuildDate><pubDate>Fri, 27 Apr 2012 20:39:33 GMT</pubDate><ttl>60</ttl><item><title>Python Cookbook Notes Chapter1</title><link>http://www.cppblog.com/andimeo/articles/172958.html</link><dc:creator>Andimeo</dc:creator><author>Andimeo</author><pubDate>Fri, 27 Apr 2012 13:27:00 GMT</pubDate><guid>http://www.cppblog.com/andimeo/articles/172958.html</guid><wfw:comment>http://www.cppblog.com/andimeo/comments/172958.html</wfw:comment><comments>http://www.cppblog.com/andimeo/articles/172958.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/andimeo/comments/commentRss/172958.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/andimeo/services/trackbacks/172958.html</trackback:ping><description><![CDATA[<p style="line-height: 20px; color: #0f1c38; font-family: Arial; font-size: 12px; letter-spacing: 1px; background-color: #e4e3e1; "><strong>1.2 Swapping Values Without Using aTemporary Variable</strong></p><p style="line-height: 20px; color: #0f1c38; font-family: Arial; font-size: 12px; letter-spacing: 1px; background-color: #e4e3e1; "><span style="line-height: normal; font-family: 宋体; ">利用元组赋值的特性：</span><span style="background-color: #eeeeee; font-size: 13px; ">a,&nbsp;b,&nbsp;c&nbsp;</span><span style="background-color: #eeeeee; font-size: 13px; ">=</span><span style="background-color: #eeeeee; font-size: 13px; ">&nbsp;b,&nbsp;c,&nbsp;a</span></p><p style="line-height: 20px; color: #0f1c38; font-family: Arial; font-size: 12px; letter-spacing: 1px; background-color: #e4e3e1; ">Tuples are often surrounded withparentheses, as in (b, c, a), but the parentheses are not necessary, exceptwhere the commas would otherwise have some other meaning (e.g., in a functioncall). The commas are what create a tuple, by packing the values that are thetuple's items.</p><p style="line-height: 20px; color: #0f1c38; font-family: Arial; font-size: 12px; letter-spacing: 1px; background-color: #e4e3e1; "><strong>1.3 Constructing a Dictionary WithoutExcessive Quoting</strong></p><p style="line-height: 20px; color: #0f1c38; font-family: Arial; font-size: 12px; letter-spacing: 1px; background-color: #e4e3e1; "><span style="line-height: normal; font-family: 宋体; ">利用</span>python<span style="line-height: normal; font-family: 宋体; ">的可变参数：</span><span style="color: red; ">*args&nbsp;</span><span style="line-height: normal; font-family: 宋体; ">和</span>&nbsp;<span style="color: red; ">**kwds</span></p><p style="line-height: 20px; color: #0f1c38; font-family: Arial; font-size: 12px; letter-spacing: 1px; background-color: #e4e3e1; "><span style="line-height: normal; ">*args</span><span style="line-height: normal; font-family: 宋体; ">是一个</span>list<span style="line-height: normal; font-family: 宋体; ">，</span><span style="line-height: normal; ">**kwds</span><span style="line-height: normal; font-family: 宋体; ">是一个</span>dict<span style="line-height: normal; font-family: 宋体; ">，如果两个一起出现，那么</span>**kwds<span style="line-height: normal; font-family: 宋体; ">必须在最后，其余有名称的变量必须在最前</span></p><p style="line-height: 20px; color: #0f1c38; font-family: Arial; font-size: 12px; letter-spacing: 1px; background-color: #e4e3e1; "><strong>1.4 Getting a Value from a Dictionary</strong></p><p style="line-height: 20px; color: #0f1c38; font-family: Arial; font-size: 12px; letter-spacing: 1px; background-color: #e4e3e1; "><span style="line-height: normal; font-family: 宋体; ">使用</span><span style="background-color: #eeeeee; font-size: 13px; ">d.get(&#8216;key&#8217;,&nbsp;&#8216;</span><span style="background-color: #eeeeee; font-size: 13px; color: #0000ff; ">not</span><span style="background-color: #eeeeee; font-size: 13px; ">&nbsp;found&#8217;)</span><span style="line-height: normal; font-family: 宋体; ">来解决，第二个参数未指定时，返回</span>None</p><p style="line-height: 20px; color: #0f1c38; font-family: Arial; font-size: 12px; letter-spacing: 1px; background-color: #e4e3e1; "><strong>1.5 Adding an Entry to a Dictionary</strong></p><p style="line-height: 20px; color: #0f1c38; font-family: Arial; font-size: 12px; letter-spacing: 1px; background-color: #e4e3e1; "><span style="line-height: normal; font-family: 宋体; ">使用</span><span style="background-color: #eeeeee; font-size: 13px; ">somedict.setdefault(somekey,&nbsp;[]).append(somevalue)</span><span style="font-family: 宋体; line-height: normal; ">来解决。</span></p><p style="line-height: 20px; color: #0f1c38; font-family: Arial; font-size: 12px; letter-spacing: 1px; background-color: #e4e3e1; "><span style="line-height: normal; font-family: 宋体; ">当</span>enry<span style="line-height: normal; font-family: 宋体; ">是</span>mutable<span style="line-height: normal; font-family: 宋体; ">的时候</span>setdefault<span style="line-height: normal; font-family: 宋体; ">函数很有用，它令</span>d[key]=value<span style="line-height: normal; font-family: 宋体; ">，然后返回</span>value<span style="line-height: normal; font-family: 宋体; ">。</span></p><p style="line-height: 20px; color: #0f1c38; font-family: Arial; font-size: 12px; letter-spacing: 1px; background-color: #e4e3e1; "><span style="line-height: normal; font-family: 宋体; ">当</span>entry<span style="line-height: normal; font-family: 宋体; ">是</span>immutable<span style="line-height: normal; font-family: 宋体; ">的时候，</span>setdefault<span style="line-height: normal; font-family: 宋体; ">则不是很有用</span></p><p style="line-height: 20px; color: #0f1c38; font-family: Arial; font-size: 12px; letter-spacing: 1px; background-color: #e4e3e1; "><strong>1.6 Associating Multiple Values with EachKey in a Dictionary</strong></p><p style="line-height: 20px; color: #0f1c38; font-family: Arial; font-size: 12px; letter-spacing: 1px; background-color: #e4e3e1; "><span style="line-height: normal; font-family: 宋体; ">有两种方法：</span></p><p style="color: #0f1c38; font-family: Arial; font-size: 12px; letter-spacing: 1px; background-color: #e4e3e1; "></p><div style="background-color:#eeeeee;font-size:13px;border:1px solid #CCCCCC;padding-right: 5px;padding-bottom: 4px;padding-left: 4px;padding-top: 4px;width: 98%;word-break:break-all"><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><span style="color: #008080; ">1</span>&nbsp;<span style="color: #000000; ">d1&nbsp;</span><span style="color: #000000; ">=</span><span style="color: #000000; ">&nbsp;{}<br /></span><span style="color: #008080; ">2</span>&nbsp;<span style="color: #000000; ">d1.setdefault(key,[]).append(value)<br /></span><span style="color: #008080; ">3</span>&nbsp;<span style="color: #000000; ">d2&nbsp;</span><span style="color: #000000; ">=</span><span style="color: #000000; ">&nbsp;{}<br /></span><span style="color: #008080; ">4</span>&nbsp;<span style="color: #000000; ">d2.setdefault(key,{})[value]&nbsp;</span><span style="color: #000000; ">=</span><span style="color: #000000; ">&nbsp;</span><span style="color: #000000; ">1</span></div><p>&nbsp;</p><p style="line-height: 20px; color: #0f1c38; font-family: Arial; font-size: 12px; letter-spacing: 1px; background-color: #e4e3e1; "><span style="line-height: normal; font-family: 宋体; ">第一种不会自动去重，第二种可以自动去除重复的元素。</span></p><p style="line-height: 20px; color: #0f1c38; font-family: Arial; font-size: 12px; letter-spacing: 1px; background-color: #e4e3e1; "><strong>1.7 Dispatching Using a Dictionary</strong></p><p style="line-height: 20px; color: #0f1c38; font-family: Arial; font-size: 12px; letter-spacing: 1px; background-color: #e4e3e1; "><span style="line-height: normal; font-family: 宋体; ">利用字典，实现其他语言中</span>switch<span style="line-height: normal; font-family: 宋体; ">、</span>case<span style="line-height: normal; font-family: 宋体; ">或</span>select<span style="line-height: normal; font-family: 宋体; ">的作用。</span><span style="background-color: #eeeeee; font-size: 13px; color: #008080; ">&nbsp;1</span><span style="background-color: #eeeeee; font-size: 13px; ">&nbsp;</span><span style="background-color: #eeeeee; font-size: 13px; ">animals&nbsp;</span><span style="background-color: #eeeeee; font-size: 13px; ">=</span><span style="background-color: #eeeeee; font-size: 13px; ">&nbsp;[]</span></p><div style="background-color:#eeeeee;font-size:13px;border:1px solid #CCCCCC;padding-right: 5px;padding-bottom: 4px;padding-left: 4px;padding-top: 4px;width: 98%;word-break:break-all"><span style="color: #008080; ">&nbsp;2</span>&nbsp;number_of_felines&nbsp;=&nbsp;0<br /><span style="color: #008080; ">&nbsp;3</span>&nbsp;<span style="color: #0000FF; ">def</span>&nbsp;deal_with_a_cat(&nbsp;):<br /><span style="color: #008080; ">&nbsp;4</span>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">global</span>&nbsp;number_of_felines<br /><span style="color: #008080; ">&nbsp;5</span>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">print</span>&nbsp;<span style="color: #800000; ">"</span><span style="color: #800000; ">meow</span><span style="color: #800000; ">"</span><br /><span style="color: #008080; ">&nbsp;6</span>&nbsp;&nbsp;&nbsp;&nbsp;animals.append(<span style="color: #800000; ">'</span><span style="color: #800000; ">feline</span><span style="color: #800000; ">'</span>)<br /><span style="color: #008080; ">&nbsp;7</span>&nbsp;&nbsp;&nbsp;&nbsp;number_of_felines&nbsp;+=&nbsp;1<br /><span style="color: #008080; ">&nbsp;8</span>&nbsp;<span style="color: #0000FF; ">def</span>&nbsp;deal_with_a_dog(&nbsp;):<br /><span style="color: #008080; ">&nbsp;9</span>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">print</span>&nbsp;<span style="color: #800000; ">"</span><span style="color: #800000; ">bark</span><span style="color: #800000; ">"</span><br /><span style="color: #008080; ">10</span>&nbsp;&nbsp;&nbsp;&nbsp;animals.append(<span style="color: #800000; ">'</span><span style="color: #800000; ">canine</span><span style="color: #800000; ">'</span>)<br /><span style="color: #008080; ">11</span>&nbsp;<span style="color: #0000FF; ">def</span>&nbsp;deal_with_a_bear(&nbsp;):<br /><span style="color: #008080; ">12</span>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">print</span>&nbsp;<span style="color: #800000; ">"</span><span style="color: #800000; ">watch&nbsp;out&nbsp;for&nbsp;the&nbsp;*HUG*!</span><span style="color: #800000; ">"</span><br /><span style="color: #008080; ">13</span>&nbsp;&nbsp;&nbsp;&nbsp;animals.append(<span style="color: #800000; ">'</span><span style="color: #800000; ">ursine</span><span style="color: #800000; ">'</span>)<br /><span style="color: #008080; ">14</span>&nbsp;tokenDict&nbsp;=&nbsp;{<br /><span style="color: #008080; ">15</span>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #800000; ">"</span><span style="color: #800000; ">cat</span><span style="color: #800000; ">"</span>:&nbsp;deal_with_a_cat,<br /><span style="color: #008080; ">16</span>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #800000; ">"</span><span style="color: #800000; ">dog</span><span style="color: #800000; ">"</span>:&nbsp;deal_with_a_dog,<br /><span style="color: #008080; ">17</span>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #800000; ">"</span><span style="color: #800000; ">bear</span><span style="color: #800000; ">"</span>:&nbsp;deal_with_a_bear,<br /><span style="color: #008080; ">18</span>&nbsp;}<br /><span style="color: #008080; ">19</span>&nbsp;<span style="color: #008000; ">#</span><span style="color: #008000; ">&nbsp;Simulate,&nbsp;say,&nbsp;somewords&nbsp;read&nbsp;from&nbsp;a&nbsp;file</span><span style="color: #008000; "><br /></span><span style="color: #008080; ">20</span>&nbsp;<span style="color: #008000; "></span>words&nbsp;=&nbsp;[<span style="color: #800000; ">"</span><span style="color: #800000; ">cat</span><span style="color: #800000; ">"</span>,<span style="color: #800000; ">"</span><span style="color: #800000; ">bear</span><span style="color: #800000; ">"</span>,&nbsp;<span style="color: #800000; ">"</span><span style="color: #800000; ">cat</span><span style="color: #800000; ">"</span>,&nbsp;<span style="color: #800000; ">"</span><span style="color: #800000; ">dog</span><span style="color: #800000; ">"</span>]<br /><span style="color: #008080; ">21</span>&nbsp;<span style="color: #0000FF; ">for</span>&nbsp;word&nbsp;<span style="color: #0000FF; ">in</span>&nbsp;words:<br /><span style="color: #008080; ">22</span>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #008000; ">#</span><span style="color: #008000; ">&nbsp;Look&nbsp;up&nbsp;the&nbsp;function&nbsp;to&nbsp;call&nbsp;for&nbsp;each&nbsp;word,&nbsp;then&nbsp;call&nbsp;it</span><span style="color: #008000; "><br /></span><span style="color: #008080; ">23</span>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #008000; "></span>functionToCall&nbsp;=&nbsp;tokenDict[word]<br /><span style="color: #008080; ">24</span>&nbsp;&nbsp;&nbsp;&nbsp;functionToCall(&nbsp;)<br /><span style="color: #008080; ">25</span>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #008000; ">#</span><span style="color: #008000; ">&nbsp;You&nbsp;could&nbsp;also&nbsp;do&nbsp;it&nbsp;in&nbsp;one&nbsp;step,&nbsp;tokenDict[word](&nbsp;)</span></div><p style="color: #0f1c38; font-family: Arial; font-size: 12px; letter-spacing: 1px; background-color: #e4e3e1; "><span style="line-height: normal;"><br /></span></p><img src ="http://www.cppblog.com/andimeo/aggbug/172958.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/andimeo/" target="_blank">Andimeo</a> 2012-04-27 21:27 <a href="http://www.cppblog.com/andimeo/articles/172958.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>