V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
pimin
V2EX  ›  Python

怎么写更 Pythonic?

  •  
  •   pimin ·
    28sui · 2016-03-26 11:32:41 +08:00 via Android · 3557 次点击
    这是一个创建于 2946 天前的主题,其中的信息可能已经有所发展或是发生改变。
    最近学 Python ,先粗过了一遍 Python 简明教程
    这几天在看廖雪峰的 Python 教程
    生成器课后习题是生成杨辉三角
    我的代码可以工作,输出正确
    但是我感觉总是写得不到位。。
    def triangles():
    L=[]
    n=0
    while True:
    for x in range(0,n):
    if x+1<n:
    L[x]=L[x]+L[x+1]
    L.insert(0,1)
    n+=1
    yield L


    n = 0
    for t in triangles():
    print(t)
    n = n + 1
    if n == 10:
    break
    15 条回复    2016-03-30 11:35:08 +08:00
    pimin
        1
    pimin  
    OP
       2016-03-26 11:33:40 +08:00 via Android
    擦,手机发帖。。可读性太差了。。
    大家忽略吧
    pynix
        2
    pynix  
       2016-03-26 11:35:04 +08:00   ❤️ 1
    lambda ,尽量不要下标索引。
    lcj2class
        3
    lcj2class  
       2016-03-26 11:42:07 +08:00   ❤️ 1
    Sequencer
        4
    Sequencer  
       2016-03-26 11:45:06 +08:00 via iPhone
    +=
    quake0day
        5
    quake0day  
       2016-03-26 11:59:36 +08:00   ❤️ 1
    Leetcode 有这道题 https://leetcode.com/problems/pascals-triangle/

    贴一个别人写的看起来比较 Pythonic 的答案,我比较喜欢他代码里面用 zip 来做 L[x]+L[x+1]这步,省去了循环。

    https://gist.github.com/quake0day/48655a324e828c99bfd9
    xuboying
        6
    xuboying  
       2016-03-26 12:17:02 +08:00 via Android
    插个楼,现在都讲全栈多语言,太 Python IC 怎么方便的 port Python 到其他语言?
    pimin
        7
    pimin  
    OP
       2016-03-26 12:19:50 +08:00 via Android
    @xuboying
    我是看到数组就想用 c/c++那样遍历处理
    strahe
        8
    strahe  
       2016-03-26 13:16:27 +08:00
    只是贴一段代码说明不了什么.
    我也很在意自己的代码是否 Pythonic,其实写 Pythonic 的根本就是 Python 像 Python,而不是 Python 代码看起来跟 C 一样,建议看一下<<编写高质量代码:改善 Python 程序的 91 个建议>>
    和<<Python 3 面向对象编程>>,后者更深奥一些,还有一个不可小觑的方面就是多了解标准库,尽可能的发挥出 Python 本身的特性.
    wowpanda
        9
    wowpanda  
       2016-03-26 18:55:56 +08:00
    @strahe 《 effective python 》怎么样
    zhuangzhuang1988
        10
    zhuangzhuang1988  
       2016-03-26 21:36:49 +08:00
    不要太刻意, 接口要设计得好 至于代码,就多多看 flask, requests 这类的源码就好了。。
    yhylord
        11
    yhylord  
       2016-03-27 09:19:11 +08:00 via Android
    @lcj2class 请问这种 slide 是怎么样做出来的?
    strahe
        12
    strahe  
       2016-03-28 10:15:59 +08:00
    @wowpanda 这本书还没看过呢,看来我应该去看一下
    lcj2class
        13
    lcj2class  
       2016-03-28 10:16:59 +08:00 via iPhone
    @yhylord 同学,要善于查看网页源代码
    siteshen
        14
    siteshen  
       2016-03-30 01:05:45 +08:00
    @strahe 几年前貌似浏览过《编写高质量代码:改善 Python 程序的 91 个建议》,记得当时的感觉是很烂。。
    strahe
        15
    strahe  
       2016-03-30 11:35:08 +08:00
    @siteshen 可能那时候你已经很注意规范了,所以才会这么觉得,新手看下还是很有帮助的
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   922 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 20:45 · PVG 04:45 · LAX 13:45 · JFK 16:45
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.