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

python3 中 -123 // 10 = ?

  •  
  •   chenstack · 2020-03-28 19:54:05 +08:00 · 2922 次点击
    这是一个创建于 1461 天前的主题,其中的信息可能已经有所发展或是发生改变。
    平时很少用到,一直记成整除,刷 leetcode 才重新记得 //运算符是向下取整,-123 // 10 结果是-13 。
    python 里面要像 C++那样整除是不是只能 int(-123 / 10)?但经过了浮点数转换总觉得效率不够高,有没有更好的方法?
    4 条回复    2020-03-29 21:45:16 +08:00
    freakxx
        1
    freakxx  
       2020-03-28 19:59:57 +08:00
    math.ceil(-123/10)
    123444a
        2
    123444a  
       2020-03-28 20:04:13 +08:00 via Android
    像 0 取整,向负无穷取整
    aloxaf
        3
    aloxaf  
       2020-03-28 21:08:34 +08:00
    -(123 // 10) (
    asche910
        4
    asche910  
       2020-03-29 21:45:16 +08:00
    明确正数的话直接 // ,负数取整我看别人基本上都是 float 转换 int 。不过都用 python 了,感觉更注重的是简洁方便吧,速度还是算了。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1132 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 18:45 · PVG 02:45 · LAX 11:45 · JFK 14:45
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.