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

puyhon 文本价格相加 ex:¥23.00+¥20.00

  •  
  •   panda0 · 2015-12-22 21:22:34 +08:00 · 1964 次点击
    这是一个创建于 3050 天前的主题,其中的信息可能已经有所发展或是发生改变。

    如图

    如何实现价格相加,计算价格总和

    image

    shakespaces
        1
    shakespaces  
       2015-12-22 21:24:28 +08:00
    拼写不忍直视
    mengzhuo
        2
    mengzhuo  
       2015-12-22 21:32:30 +08:00
    天啊……楼主你还是回去看看书吧
    不……不是那本 Python 手册,我说的是英语书!
    Arthur2e5
        3
    Arthur2e5  
       2015-12-22 21:46:40 +08:00   ❤️ 1
    ……你想不想知道有個內置函式叫做 sum(*iterable)?
    bdbai
        4
    bdbai  
       2015-12-22 22:37:50 +08:00 via iPhone
    > 请在写下问题之后多多检查自己的拼写,这是对阅读你的问题的其他人起码的尊重。

    /t/10686
    stillwater
        5
    stillwater  
       2015-12-22 23:09:52 +08:00
    转换成 float 再相加吧
    ericls
        6
    ericls  
       2015-12-23 00:15:21 +08:00
    @shakespaces 估计是手滑 uy 在 yt 旁边
    latyas
        7
    latyas  
       2015-12-23 03:52:37 +08:00   ❤️ 2
    让 lz 难受一下
    sum(map(lambda item: float(re.findall('[\d\.]+', item)[0]), map(lambda x: x.text, i.select['.price_n'])))

    @Arthur2e5 是 sum(iterable)

    sum(iterable, start=0, /)
    Return the sum of a 'start' value (default: 0) plus an iterable of numbers

    When the iterable is empty, return the start value.
    This function is intended specifically for use with numeric values and may
    reject non-numeric types.
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2497 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 15:53 · PVG 23:53 · LAX 08:53 · JFK 11:53
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.