Python 将奇数除以最接近的整数
Python divide odd number to closest integers
我希望有一个 oneligner(如果不可能;好的)将奇数除以 2,并给出 2 个最接近的整数 Python 2.7:
9/2 results in 4,5
7/2 results in 3,4
等等
我试过了,但我想不出任何简单的解决方案。
from math import ceil, floor
closest_ints = (int(floor(9/2)), int(ceil(9/2)))
我想你可以将它包装在一个函数中。
编辑:我假设 Python 3 整数除法,在 Python 2 中添加
from __future__ import division
div_odd = lambda n: (n//2, n//2 + 1)
我希望有一个 oneligner(如果不可能;好的)将奇数除以 2,并给出 2 个最接近的整数 Python 2.7:
9/2 results in 4,5
7/2 results in 3,4
等等
我试过了,但我想不出任何简单的解决方案。
from math import ceil, floor
closest_ints = (int(floor(9/2)), int(ceil(9/2)))
我想你可以将它包装在一个函数中。
编辑:我假设 Python 3 整数除法,在 Python 2 中添加
from __future__ import division
div_odd = lambda n: (n//2, n//2 + 1)