推荐学习书目
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
css3
V2EX  ›  Python

求助,怎么用最简单的方便实现 list 内的数值运算需求?

  •  
  •   css3 · Sep 13, 2018 · 2997 views
    This topic created in 2811 days ago, the information mentioned may be changed or developed.

    有这么一个字典,现在需要 把 height,y 分别乘以 1080,然后取整(应该需要四舍五入), 把 width,x分别乘以1920,然后取整(应该需要四舍五入), 最后放到一个 list 里边, 然后还需要做一个加法,有什么简单的办法吗,写 for 循环要 2 大坨,不甘心,看看万能的 v 友,有什么快速简单的方法没有

    a = {'height': 0.724074, 'width': 0.226042, 'x': 0.295833, 'y': 0.275926}
    

    最后输出一个:

    [568, 298, 1002, 1080]  # 顺序和数值有些变化 [x, y, x + width, y + height]
    
    10 replies    2018-09-13 14:07:08 +08:00
    zhengyongtao
        1
    zhengyongtao  
       Sep 13, 2018
    总共就四个数值为什么要写 for 循环?直接列表推导式不就好了
    css3
        2
    css3  
    OP
       Sep 13, 2018
    @zhengyongtao 列表推导也不好直接生成我想要的结果啊,只能取到 values, 再循环?
    epicnoob
        3
    epicnoob  
       Sep 13, 2018
    >>> k = numpy.array([[0,0,1920,0],[0,0,0,1080],[0,1920,1920,0],[1080,0,0,1080]])
    >>> numpy.dot(k, numpy.array(list(a.values())).reshape(4,1))
    cbiqih
        4
    cbiqih  
       Sep 13, 2018
    from operator import mul

    a = {'height': 0.724074, 'width': 0.226042, 'x': 0.295833, 'y': 0.275926}
    b = {'height': 1080, 'width': 1920, 'x': 1920, 'y': 1080}
    result = sum(map(lambda x: mul(*x), zip(a.values(), b.values())))
    xpresslink
        5
    xpresslink  
       Sep 13, 2018
    就四个数还折腾什么。
    真够懒的,虽然是程序员三大美德之一吧,但是也得注意一下 kiss 原则
    cbiqih
        6
    cbiqih  
       Sep 13, 2018
    考虑到 dict 是无序的,获取的 values 不一定能一一对应上.

    a = {'height': 0.724074, 'width': 0.226042, 'x': 0.295833, 'y': 0.275926}
    b = {'height': 1080, 'width': 1920, 'x': 1920, 'y': 1080}
    result = sum(list(value * b[key] for key,value in a.items()))
    cbiqih
        7
    cbiqih  
       Sep 13, 2018   ❤️ 1
    看来我理解错了你的加法~
    a = {'height': 0.724074, 'width': 0.226042, 'x': 0.295833, 'y': 0.275926}
    b = {'height': 1080, 'width': 1920, 'x': 1920, 'y': 1080}
    d = {key:int(value * b[key]) for key,value in a.items()}
    return [d['x'], d['y'], d['width'] + d['x'], d['height'] + d['y']]
    css3
        8
    css3  
    OP
       Sep 13, 2018
    @cbiqih 多谢,你这个办法真比我简单些,引入字典比,也是一绝啊,我把 int 换成 round 了,可以四舍五入
    css3
        9
    css3  
    OP
       Sep 13, 2018
    @cbiqih key:int(value * b[key])请教一下这个是什么意思啊,看不太懂
    cbiqih
        10
    cbiqih  
       Sep 13, 2018
    @css3 字典推导式
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   1354 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 36ms · UTC 17:03 · PVG 01:03 · LAX 10:03 · JFK 13:03
    ♥ Do have faith in what you're doing.