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

<channel>
	<title>梁剑的Blog &#187; Python</title>
	<atom:link href="http://icomes.net/tag/an-alternative-shell-or-script-or-language/feed/" rel="self" type="application/rss+xml" />
	<link>http://icomes.net</link>
	<description>做有趣的事，做有用的人</description>
	<lastBuildDate>Fri, 20 Aug 2010 16:36:35 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>初学Python</title>
		<link>http://icomes.net/2009/01/24/%e5%88%9d%e5%ad%a6python/</link>
		<comments>http://icomes.net/2009/01/24/%e5%88%9d%e5%ad%a6python/#comments</comments>
		<pubDate>Fri, 23 Jan 2009 18:22:57 +0000</pubDate>
		<dc:creator>梁剑</dc:creator>
				<category><![CDATA[Python]]></category>
		<category><![CDATA[技术笔记]]></category>
		<category><![CDATA[数学]]></category>
		<category><![CDATA[概率]]></category>

		<guid isPermaLink="false">http://icomes.net/?p=292</guid>
		<description><![CDATA[这两天时间比较充裕，可以有比较长的连续时间集中精神，我用来学Python了。 有C++、Perl、Bash和PHP的基础，要入门其实并不太难。 没有关注太多的细节，套用很远古一句话，语言是用来写程序的，不是用来理解的。（粗体部分请用适当的词替换） 的确很方便，而且比Perl要容易，很多。 不过似乎Python不支持++运算符，无语。 另外，Python3和Python2不完全兼容，但对我是没有丝毫影响的：） 我主要的学习资料是网上的两个教程： Python 绝对简明手册 简明 Python 教程 都写得很好，如果是有其他语言的基础，看过应该就可以了解Python的基本用法了。 至于进一步的参考，找到了《Dive Into Python》的中文版，已经下载，作为工具书翻阅：） 趁着还记得一点，把前两天那个进化论中的概率论用Python重写了一次。 当然，结果一样，因为这个问题的数学期望是有理论保证的。 #!/usr/bin/python &#160; import os import random &#160; RandomCount=0; GenerateCount=0; Target = list&#40;&#34;tobeornottobe&#34;&#41;; &#160; def getRandomChar&#40;&#41;: global RandomCount; RandomCount = RandomCount+1; return random.choice&#40;&#34;abcdefghijklmnopqrstuvwxyz&#34;&#41;; &#160; def compareList&#40;list1, list2&#41;: if&#40; len&#40;list1&#41; != len&#40;list2&#41; &#41;: raise Exception; DifferentCount = 0; for [...]]]></description>
			<content:encoded><![CDATA[<p>这两天时间比较充裕，可以有比较长的连续时间集中精神，我用来学Python了。<br />
有C++、Perl、Bash和PHP的基础，要入门其实并不太难。<br />
没有关注太多的细节，套用很远古一句话，<strong>语言</strong>是用来<strong>写程序</strong>的，不是用来<strong>理解</strong>的。（粗体部分请用适当的词替换）<br />
的确很方便，而且比Perl要容易，很多。<br />
不过似乎Python不支持++运算符，无语。<br />
另外，Python3和Python2不完全兼容，但对我是没有丝毫影响的：）</p>
<p>我主要的学习资料是网上的两个教程：</p>
<ol>
<li><a href="http://wiki.woodpecker.org.cn/moin/PyAbsolutelyZipManual" target="_blank">Python 绝对简明手册</a></li>
<li><a href="http://linux.chinaitlab.com/manual/Python_chinese/index.html" target="_blank"><span class="header">简明 Python 教程</span></a></li>
</ol>
<p><span class="header">都写得很好，如果是有其他语言的基础，看过应该就可以了解Python的基本用法了。</span></p>
<p><span class="header">至于进一步的参考，找到了<a href="http://www.woodpecker.org.cn/diveintopython/index.html" target="_blank">《Dive Into Python》的中文版</a>，已经下载，作为工具书翻阅：）<br />
</span></p>
<p>趁着还记得一点，把前两天那个<a href="http://icomes.net/?p=276" target="_blank">进化论中的概率论</a>用Python重写了一次。<br />
当然，结果一样，因为这个问题的数学期望是有理论保证的。</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">#!/usr/bin/python</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">os</span>
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">random</span>
&nbsp;
RandomCount=<span style="color: #ff4500;">0</span><span style="color: #66cc66;">;</span>
GenerateCount=<span style="color: #ff4500;">0</span><span style="color: #66cc66;">;</span>
Target = <span style="color: #008000;">list</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;tobeornottobe&quot;</span><span style="color: black;">&#41;</span><span style="color: #66cc66;">;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> getRandomChar<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>:
	<span style="color: #ff7700;font-weight:bold;">global</span> RandomCount<span style="color: #66cc66;">;</span>
	RandomCount = RandomCount+<span style="color: #ff4500;">1</span><span style="color: #66cc66;">;</span>
	<span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #dc143c;">random</span>.<span style="color: black;">choice</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;abcdefghijklmnopqrstuvwxyz&quot;</span><span style="color: black;">&#41;</span><span style="color: #66cc66;">;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> compareList<span style="color: black;">&#40;</span>list1, list2<span style="color: black;">&#41;</span>:
	<span style="color: #ff7700;font-weight:bold;">if</span><span style="color: black;">&#40;</span> <span style="color: #008000;">len</span><span style="color: black;">&#40;</span>list1<span style="color: black;">&#41;</span> <span style="color: #66cc66;">!</span>= <span style="color: #008000;">len</span><span style="color: black;">&#40;</span>list2<span style="color: black;">&#41;</span> <span style="color: black;">&#41;</span>:
		<span style="color: #ff7700;font-weight:bold;">raise</span> <span style="color: #008000;">Exception</span><span style="color: #66cc66;">;</span>
	DifferentCount = <span style="color: #ff4500;">0</span><span style="color: #66cc66;">;</span>
	<span style="color: #ff7700;font-weight:bold;">for</span> i <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">range</span><span style="color: black;">&#40;</span><span style="color: #008000;">len</span><span style="color: black;">&#40;</span>list2<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>:
		<span style="color: #ff7700;font-weight:bold;">if</span><span style="color: black;">&#40;</span> list1<span style="color: black;">&#91;</span>i<span style="color: black;">&#93;</span> == list2<span style="color: black;">&#91;</span>i<span style="color: black;">&#93;</span> <span style="color: black;">&#41;</span>:
			<span style="color: #ff7700;font-weight:bold;">continue</span><span style="color: #66cc66;">;</span>
		<span style="color: #ff7700;font-weight:bold;">else</span>:
			DifferentCount = DifferentCount+<span style="color: #ff4500;">1</span><span style="color: #66cc66;">;</span>
	<span style="color: #ff7700;font-weight:bold;">return</span> DifferentCount<span style="color: #66cc66;">;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> generateRandomList<span style="color: black;">&#40;</span>now, target<span style="color: black;">&#41;</span>:
	<span style="color: #ff7700;font-weight:bold;">global</span> GenerateCount<span style="color: #66cc66;">;</span>
	GenerateCount = GenerateCount + <span style="color: #ff4500;">1</span><span style="color: #66cc66;">;</span>
	result = <span style="color: black;">&#91;</span><span style="color: black;">&#93;</span><span style="color: #66cc66;">;</span>
	<span style="color: #ff7700;font-weight:bold;">for</span> i <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">range</span><span style="color: black;">&#40;</span><span style="color: #008000;">len</span><span style="color: black;">&#40;</span>target<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>:
		<span style="color: #ff7700;font-weight:bold;">if</span><span style="color: black;">&#40;</span> now<span style="color: black;">&#91;</span>i<span style="color: black;">&#93;</span> <span style="color: #66cc66;">!</span>= target<span style="color: black;">&#91;</span>i<span style="color: black;">&#93;</span> <span style="color: black;">&#41;</span>:
			result.<span style="color: black;">append</span><span style="color: black;">&#40;</span> getRandomChar<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span> <span style="color: black;">&#41;</span><span style="color: #66cc66;">;</span>
			<span style="color: #ff7700;font-weight:bold;">if</span><span style="color: black;">&#40;</span> result<span style="color: black;">&#91;</span>i<span style="color: black;">&#93;</span> == target<span style="color: black;">&#91;</span>i<span style="color: black;">&#93;</span> <span style="color: black;">&#41;</span>:
				now<span style="color: black;">&#91;</span>i<span style="color: black;">&#93;</span> = target<span style="color: black;">&#91;</span>i<span style="color: black;">&#93;</span><span style="color: #66cc66;">;</span>
		<span style="color: #ff7700;font-weight:bold;">else</span>:
			result.<span style="color: black;">append</span><span style="color: black;">&#40;</span> <span style="color: #483d8b;">' '</span> <span style="color: black;">&#41;</span><span style="color: #66cc66;">;</span>
	<span style="color: #ff7700;font-weight:bold;">return</span> result<span style="color: #66cc66;">;</span>
<span style="color: #808080; font-style: italic;">#	print result;</span>
&nbsp;
&nbsp;
Now = <span style="color: #008000;">list</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot; &quot;</span> <span style="color: #66cc66;">*</span> <span style="color: #008000;">len</span><span style="color: black;">&#40;</span>Target<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span><span style="color: #66cc66;">;</span>
<span style="color: #ff7700;font-weight:bold;">while</span> compareList<span style="color: black;">&#40;</span>Now, Target<span style="color: black;">&#41;</span> <span style="color: #66cc66;">!</span>= <span style="color: #ff4500;">0</span>:
	Result = generateRandomList<span style="color: black;">&#40;</span>Now, Target<span style="color: black;">&#41;</span><span style="color: #66cc66;">;</span>
	<span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;%6d&quot;</span> <span style="color: #66cc66;">%</span> GenerateCount, <span style="color: #483d8b;">&quot;|&quot;</span>, <span style="color: #483d8b;">&quot;&quot;</span>.<span style="color: black;">join</span><span style="color: black;">&#40;</span>Now<span style="color: black;">&#41;</span>, <span style="color: #483d8b;">&quot;|&quot;</span>, <span style="color: #483d8b;">&quot;&quot;</span>.<span style="color: black;">join</span><span style="color: black;">&#40;</span>Result<span style="color: black;">&#41;</span><span style="color: #66cc66;">;</span>
<span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;RandomCount: &quot;</span>, RandomCount<span style="color: #66cc66;">;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://icomes.net/2009/01/24/%e5%88%9d%e5%ad%a6python/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
