Differences between revisions 4 and 12 (spanning 8 versions)
Revision 4 as of 2007-03-20 09:30:14
Size: 804
Editor: ZoomQuiet
Comment:
Revision 12 as of 2009-12-25 07:08:34
Size: 973
Editor: localhost
Comment: converted to 1.6 markup
Deletions are marked like this. Additions are marked like this.
Line 6: Line 6:
::-- ZoomQuiet [[[DateTime(2005-02-24T12:01:02Z)]]]
[[TableOfContents]]
::-- ZoomQuiet [<<DateTime(2005-02-24T12:01:02Z)>>]
<<TableOfContents>>
Line 10: Line 10:
 * [[http://blog.csdn.net/lanphaday/archive/2008/08/03/2762251.aspx|Pythonic到底是什么玩意儿?]] - 赖勇浩(恋花蝶)的博客 - CSDNBlog
 * [[http://blog.donews.com/limodou/archive/2005/08/07/498175.aspx|What is Pythonic?]] - limodou的学习记录 - DonewsBlog
Line 12: Line 15:
 * [wiki:PyCNmail/2005-June/011545.html 急:文本文件中有选择的读取并按照有关格式存储数据的问题]  * [[PyCNmail:2005-June/011545.html|急:文本文件中有选择的读取并按照有关格式存储数据的问题]]
Line 20: Line 23:
 * 单行程序 扫描素数!{{{#!python
from math import sqrt
p = 100
[p for p in range(2, 30) if 0 not in [p % d for d in range(2, p)]]
}}}
<<Include(PyPrimeNumberGenerator)>>

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

::-- ZoomQuiet [2005-02-24 12:01:02]

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

直觉

内省

素数

  • 单行程序 扫描素数!

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

   1 # SOP 式的排版:
   2 [ p for p in range(2, N) 
   3     if 0 not in [ p%d 
   4         for d in range(2, int(sqrt(p))+1)
   5                 ] 
   6 ]

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