平时很少用到,一直记成整除,刷 leetcode 才重新记得 //运算符是向下取整,-123 // 10 结果是-13 。
python 里面要像 C++那样整除是不是只能 int(-123 / 10)?但经过了浮点数转换总觉得效率不够高,有没有更好的方法?
python 里面要像 C++那样整除是不是只能 int(-123 / 10)?但经过了浮点数转换总觉得效率不够高,有没有更好的方法?
4 条回复 • 2020-03-29 21:45:16 +08:00
|
1
metamask 2020 年 3 月 28 日
math.ceil(-123/10)
|
|
2
123444a 2020 年 3 月 28 日 via Android
像 0 取整,向负无穷取整
|
|
3
aloxaf 2020 年 3 月 28 日
-(123 // 10) (
|
|
4
asche910 2020 年 3 月 29 日
明确正数的话直接 // ,负数取整我看别人基本上都是 float 转换 int 。不过都用 python 了,感觉更注重的是简洁方便吧,速度还是算了。
|
推荐学习书目
› 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