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

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

  •  
  •   css3 · 2018-09-13 11:45:41 +08:00 · 2128 次点击
    这是一个创建于 2023 天前的主题,其中的信息可能已经有所发展或是发生改变。

    有这么一个字典,现在需要 把 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 条回复    2018-09-13 14:07:08 +08:00
    zhengyongtao
        1
    zhengyongtao  
       2018-09-13 11:59:41 +08:00
    总共就四个数值为什么要写 for 循环?直接列表推导式不就好了
    css3
        2
    css3  
    OP
       2018-09-13 12:02:13 +08:00
    @zhengyongtao 列表推导也不好直接生成我想要的结果啊,只能取到 values, 再循环?
    epicnoob
        3
    epicnoob  
       2018-09-13 12:27:22 +08:00
    >>> 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  
       2018-09-13 12:46:12 +08:00
    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  
       2018-09-13 12:48:34 +08:00
    就四个数还折腾什么。
    真够懒的,虽然是程序员三大美德之一吧,但是也得注意一下 kiss 原则
    cbiqih
        6
    cbiqih  
       2018-09-13 12:52:05 +08:00
    考虑到 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  
       2018-09-13 12:59:11 +08:00   ❤️ 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
       2018-09-13 13:04:02 +08:00
    @cbiqih 多谢,你这个办法真比我简单些,引入字典比,也是一绝啊,我把 int 换成 round 了,可以四舍五入
    css3
        9
    css3  
    OP
       2018-09-13 13:09:37 +08:00
    @cbiqih key:int(value * b[key])请教一下这个是什么意思啊,看不太懂
    cbiqih
        10
    cbiqih  
       2018-09-13 14:07:08 +08:00
    @css3 字典推导式
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5958 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 02:33 · PVG 10:33 · LAX 19:33 · JFK 22:33
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.