﻿<?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++博客-pzz-文章分类-读书</title><link>http://www.cppblog.com/panzhizhou/category/20628.html</link><description /><language>zh-cn</language><lastBuildDate>Thu, 22 May 2014 13:50:22 GMT</lastBuildDate><pubDate>Thu, 22 May 2014 13:50:22 GMT</pubDate><ttl>60</ttl><item><title>pythondoc_tutorial_note</title><link>http://www.cppblog.com/panzhizhou/articles/python.html</link><dc:creator>pzz</dc:creator><author>pzz</author><pubDate>Wed, 21 May 2014 07:21:00 GMT</pubDate><guid>http://www.cppblog.com/panzhizhou/articles/python.html</guid><wfw:comment>http://www.cppblog.com/panzhizhou/comments/207043.html</wfw:comment><comments>http://www.cppblog.com/panzhizhou/articles/python.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/panzhizhou/comments/commentRss/207043.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/panzhizhou/services/trackbacks/207043.html</trackback:ping><description><![CDATA[<div><strong>p25: &nbsp;</strong><br />def cheeseshop(kind,*arguments,**keywords):</div><div>&nbsp; &nbsp; &nbsp; &nbsp;for arg in arguments:</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print arg</div><div>&nbsp; &nbsp; &nbsp; &nbsp;keys=sorted(keywords.keys())</div><div>&nbsp; &nbsp; &nbsp; &nbsp;for kw in keys:</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;print kw,":",keywords[kw]</div><div></div><div>that *arguments must occur before **keywords.</div><div>cheeseshop("Limburger", "It&#8217;s very runny, sir.","It&#8217;s really very, VERY runny, sir.",</div><div>shopkeeper=&#8217;Michael Palin&#8217;,client="John Cleese",sketch="Cheese Shop Sketch")</div><div></div><div><strong>2.sort usage:</strong></div><div>pairs = [(1, &#8217;one&#8217;), (2, &#8217;two&#8217;), (3, &#8217;three&#8217;), (4, &#8217;four&#8217;)]</div><div>pairs.sort(key=lambda pair: pair[1])</div><div></div><div></div><div><strong>p29:</strong></div><div>list 可以作为一个队列，数组，栈来用。支持下标索引，排序，逆序，插入，删除，拓展等功能</div><div>当然了，list作为一个队列来用的时候，效率并不高，因为涉及大量的元素移位操作，因此我们可以使用collections.deque</div><div></div><div></div><div></div><div>(1) filter(function,sequence) &nbsp;return a sequence ..that function(item) is true</div><div>(2) map(function,sequence) &nbsp; &nbsp; return a sequence</div><div>(3）reduce(function,sequence) &nbsp;returns a single value constructed by calling the binary function function</div><div>on the first two items of the sequence, then on the result and the next item, and so on.</div><div></div><div></div><div><strong>p35:</strong></div><div>(1) tuple</div><div>不可更改，操作与list类似</div><div>&nbsp;</div><div>(2) set</div><div>没有重复元素，</div><div></div><div>(3) dictionaries</div><div></div><div><strong>p36:</strong></div><div>enumerate() &nbsp;返回索引和索引值的pair 对的迭代器</div><div>for q, a in zip(questions, answers):</div><div>&nbsp; &nbsp; print &#8217;What is your {0}? It is {1}.&#8217;.format(q, a)</div><div></div><div></div><div>*当循环使用字典的时候，我们可以使用iteritems()函数来代替上面的enumerate()</div><div></div><div>p47:</div><div>(4) str</div><div>&nbsp;转换成字符串的函数：str()和repr()</div><div>&nbsp;str.format()</div><div>&nbsp;eg. &nbsp;print '{0} and {1}'.format('span','eggs')</div><div></div><div><strong>p50:</strong></div><div>文件读写:</div><div>f.read()</div><div>f.readline()</div><div></div><div>f.readlines()</div><div></div><div><strong>p52:</strong></div><div>用json来储存复杂结构的文件内容</div><div>json.dumps([1,'simple'.'list'])</div><div>json.dump(x,f) &nbsp;//储存x到文件f中</div><div>x=json.load(f) &nbsp;//导出文件的结构数据到x</div><div></div><div>json可以处理列表和字典，对于任意类的实例，json需要做点额外的工作，这个时候我们可以下面这个包：</div><div>pickle</div><div></div><div>---------------------------------------------------------------------------------------</div><div></div><div>(5) <strong>关于python的类，我想说明如下</strong>：</div><div>&nbsp; &nbsp; 与c++类似，可以有多继承。但是没有所谓的虚函数机制，另外在c++中隐含的this指针在python中相对应的就是self，而且有内置的一些数据成员和函数，对于一个类__init__(self)这样的形式的函数，我们可以看作是内置函数，可以看成是构造函数，同样也有重载的形式，而且在对象生成时，这些函数就有了，也就是说这个是绑定于类对象的，至于隐式自动构造函数，可以从c++中得到启发，从使用的角度来说，python更容易理解，而且也不需要手动释放内存做析构这样的操作，总的来说，可以从c++的类的思维习惯来看待这个python</div><div>----------------------------------------------------------------------------------------</div><div></div><div></div><div>p71：</div><div><strong>各类包的简单回顾：</strong></div><div>（1） os</div><div>&nbsp; &nbsp; &nbsp; os.getcwd() &nbsp; &nbsp; &nbsp;//return working directory</div><div>&nbsp; &nbsp; &nbsp; os.chdir()</div><div>&nbsp; &nbsp; &nbsp; os.system(command) &nbsp;//执行系统命令</div><div></div><div>(2) shutil</div><div>&nbsp; &nbsp; 拷贝，移动文件</div><div>&nbsp; &nbsp; copyfile，move</div><div>(3) glob</div><div>&nbsp; &nbsp; 列出当前文件下的所有文件</div><div>&nbsp; &nbsp; glob.glob('*.py')</div><div></div><div>p72:</div><div>(1) re</div><div>&nbsp;re.findall(patern,string)</div><div>&nbsp;'tea for too'.replace('roo','two')</div><div>(2) random()</div><div>&nbsp; random.choice(['apple','pear','banana'])</div><div>&nbsp; random.sample(xrange(100),10)</div><div>&nbsp; random.random()</div><div>&nbsp; random.randrange()</div><div>(3) Internet 接入</div><div>&nbsp; &nbsp;urllib2,smtplib两个包</div><div>(4) 日期和时间的包</div><div>&nbsp; &nbsp; &nbsp;datetime</div><div>(5) 数据压缩的包</div><div>&nbsp; &nbsp; zlib</div><div>&nbsp; &nbsp; t=zlib.compress(s)</div><div>&nbsp; &nbsp; zlib.decompress(t)</div><div>(6) 计时的包</div><div>&nbsp; &nbsp; timeit</div><div>&nbsp; &nbsp; from timeit import Timer</div><div>&nbsp; &nbsp; Timer(express).timeit() &nbsp; //计时</div><div>(7) 质量控制的两个包</div><div>&nbsp; &nbsp; doctest，unittest</div><div></div><div>&nbsp; &nbsp;&nbsp;</div><div></div><div></div><div></div><div></div><div></div><div>&nbsp;</div><div></div><img src ="http://www.cppblog.com/panzhizhou/aggbug/207043.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/panzhizhou/" target="_blank">pzz</a> 2014-05-21 15:21 <a href="http://www.cppblog.com/panzhizhou/articles/python.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>