Differences between revisions 5 and 6
Revision 5 as of 2007-03-20 09:57:11
Size: 1644
Editor: ZoomQuiet
Comment:
Revision 6 as of 2007-03-20 09:57:27
Size: 1636
Editor: ZoomQuiet
Comment:
Deletions are marked like this. Additions are marked like this.
Line 20: Line 20:
 * 单行程序 扫描素数!{{{#!python  * 单行程序 扫描素数!{{{

Pythonic -- 蟒样 坚韧,灵活,柔韧......

::-- ZoomQuiet [DateTime(2005-02-24T12:01:02Z)] TableOfContents

汇集 Python 味道的技巧,思路,方式....

直觉

  • [wiki:PyCNmail/2005-June/011545.html 急:文本文件中有选择的读取并按照有关格式存储数据的问题]
    • 这样的问题,其实是Py 最擅长的!当年多个国家的人类基因确认工程中,不同格式的DNA数据的自动同步匹配合成就是Py 干的!
    • /EasyThinkAndDoneIt -- 如何简要的完成任务

内省

素数

  • 单行程序 扫描素数!

    from math import sqrt
    # 求100 以内的素数...
    [p for p in range(2, 100) if 0 not in [p % d for d in range(2, sqrt(p)+1)]]
     ^  ^     ^               ^    ^      ^  ^              ^       ^      ^
    ||  |     |               |    |      |  |              |       |      +- 跳过偶数
    ||  |     |               |    |      |  |              |       +- 通过平方精简尝试
    ||  |     |               |    |      |  |              +- 组织所有 2~p 之间可能为公因子的数列
    ||  |     |               |    |      |  +- 求余,尝试整除
    ||  |     |               |    |      +- 列表运算,直接将以上计算结果组成数组 返回 
    ||  |     |               |    +- 余数0 不在求余结果列表中
    ||  |     |               +- 即2~p 都不能整除 p 的p
    ||  |     +- 提取运算
    ||  +- for..in 循环取数,从2~p 的连续数组中
    |+- 素数!
    +- 列表计算组织所有结果为数组返回!

PythonIc (last edited 2009-12-25 07:08:34 by localhost)