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

求问,有没有比较快的、准确的计算方式?谢谢。

  •  1
     
  •   kingmo888 · Nov 25, 2022 · 2966 views
    This topic created in 1402 days ago, the information mentioned may be changed or developed.
    target_num = 15
    weights = {
        'A':0.6,
        'B':0.3,
        'C':0.1
        }
    single_num = {k:int(round(v*target_num, 0)) for k,v in weights.items()}
    

    其中 target_num(0~1000), weights 内部元素数量处于 1~N ( N<10 )且 values 之和为 1,

    要求最终 single_num 的 values 之和等于 target_num 。

    上述代码总会在某些权重下多一点或少一点。

    同时,基于上述要求的话,列表推导式之外还需要加判断代码,速度就慢了。

    请问有什么比较好的解决办法吗?

    谢谢。

    3 replies  •  2022-11-25 16:13:56 +08:00
    paopjian
        1
    paopjian  
       Nov 25, 2022
    背包问题?
    zbatman
        2
    zbatman  
       Nov 25, 2022
    return null

    你就说快不快吧
    Huelse
        3
    Huelse  
       Nov 25, 2022
    参考 /t/895464

    建议确定精度后转整数计算
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   961 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 18:13 · PVG 02:13 · LAX 11:13 · JFK 14:13
    ♥ Do have faith in what you're doing.