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
Ginson
V2EX  ›  Python

此 Python2 脚本怎么修改为 Python3 运行

  •  
  •   Ginson · 2018-01-09 18:59:09 +08:00 · 3741 次点击
    这是一个创建于 2270 天前的主题,其中的信息可能已经有所发展或是发生改变。

    网上下载了一个 Python2 脚本,其中有一句非常 pythonic ,本人水平理解不能
    想请教下大家这段代码要如何修改才能在 Python3 中运行。

    def avhash(img):
        if not isinstance(img, Image.Image):
            img = Image.open(img)
        img = img.resize((8, 8), Image.ANTIALIAS).convert('L') #将 image 压缩为 8*8,转化为灰度图
        avg = reduce(lambda x, y: x + y, img.getdata()) / 64. #对每个像素点的灰度累和,最后除以 64,得到灰度的平均值
        return reduce(lambda x,(y,z): x | (z << y), enumerate(map(lambda i: 0 if i < avg else 1, img.getdata())), 0)
    

    就是这句

    reduce(lambda x,(y,z): x | (z << y), enumerate(map(lambda i: 0 if i < avg else 1, img.getdata())), 0)
    

    目前直接运行会报错,错误信息如下 报错

    第 1 条附言  ·  2018-01-09 19:32:00 +08:00
    17 条回复    2018-01-09 22:19:42 +08:00
    okletswin
        1
    okletswin  
       2018-01-09 19:07:59 +08:00
    laqow
        2
    laqow  
       2018-01-09 19:13:59 +08:00 via Android
    python3 自带的帮助里面有个 2to3 发生变化语法的介绍页,搜索一下关键字应该就有了
    Ginson
        3
    Ginson  
    OP
       2018-01-09 19:14:23 +08:00
    @okletswin 嗯,在发帖之前我尝试了几种方法
    1. 用在线的 2to3 工具,比如 http://www.pythonconverter.com/ ,但是转换的结果还是一样报错
    2. 搜索了一下 lambda 在 Python2 和 3 里面是否有语法上的区别,但是没有找到结果
    谢谢您的嘲讽,呵呵
    qsnow6
        4
    qsnow6  
       2018-01-09 19:17:05 +08:00
    lambda 只能 接受一个参数
    mkeith
        5
    mkeith  
       2018-01-09 19:17:31 +08:00 via iPhone
    lambda 改成普通函数试试
    okletswin
        6
    okletswin  
       2018-01-09 19:18:37 +08:00
    @Ginson sorry,仔细看是 lambda 的语法问题,跟 reduce 无关,抱歉了
    Ginson
        7
    Ginson  
    OP
       2018-01-09 19:20:33 +08:00
    @laqow 是这个把 http://sebastianraschka.com/Articles/2014_python_2_3_key_diff.html
    但是我没有看到 跟 lambda 或 reduce 相关的信息
    jotsai
        8
    jotsai  
       2018-01-09 19:21:17 +08:00
    lambda 可以接收多个参数。试试把
    lambda x,(y,z): x | (z << y)
    改成
    lambda x, y: x | (y[1] << y[0])
    Ginson
        9
    Ginson  
    OP
       2018-01-09 19:21:59 +08:00
    @okletswin 谢谢,我研究下
    ballshapesdsd
        10
    ballshapesdsd  
       2018-01-09 19:22:08 +08:00
    这个在 python2 运行不过呀
    no1xsyzy
        11
    no1xsyzy  
       2018-01-09 19:25:23 +08:00
    这个是 Python2 也是旧的吧
    函数定义里解 tuple 的功能被取消了
    Ginson
        12
    Ginson  
    OP
       2018-01-09 19:27:12 +08:00
    @jotsai 按您的方法成功了
    是因为 lambda 接受参数的方式在 2 和 3 中有区别?
    Ginson
        13
    Ginson  
    OP
       2018-01-09 19:30:29 +08:00
    @ballshapesdsd
    @no1xsyzy
    但是我在 Python2 环境下运行成功哎
    jotsai
        14
    jotsai  
       2018-01-09 19:33:31 +08:00
    DGChost
        15
    DGChost  
       2018-01-09 19:41:33 +08:00
    @Ginson #13 你给提示符打码了,但是窗口暴露了:)
    Ginson
        16
    Ginson  
    OP
       2018-01-09 19:43:55 +08:00
    @DGChost QAQ !!
    既然被发现了我就坦白吧,其实是全景图片网找到我们公司,说我们很多图片侵犯了他们的版权,所以从网上找了这个判断图片相似度脚本逐个判断下是否属实。
    thautwarm
        17
    thautwarm  
       2018-01-09 22:19:42 +08:00
    `2to3 -w <directory name>\`即可。(另: 不过有时候 encode 和 decode 可能需要手动改一下。报错进 step in 进去改。

    然后看了一下错在 lambda 参数解构。。
    吐槽一下 PEP 3113。。
    难受,想哭。你没法和某些人谈优雅,因为他要跟你谈 exception non characteristic, 功能弱小非要强行说"powerful"的 introspection。。。
    专门为 param destruction 写,顺便加入 dict, list 的解构,提供贴近自然的语法糖,这都是非常有意义的事情。
    不能因为一个东西的好提到了某些东西的不好,就砍掉这个好的。X 疼医嘴,人都傻了。
    怀念 python2 少有的先进事物...
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3110 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 33ms · UTC 12:54 · PVG 20:54 · LAX 05:54 · JFK 08:54
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.