[英]Get next n whole numbers for a decimal python
Say I have a decimal no说我有一个小数没有
a = 49.5
I am looking to get 5+ and 5- values(whole no.s) for the given decimal no.我正在寻找给定十进制数的 5+ 和 5- 值(整数)。
Final intended output is a list:最终预期的 output 是一个列表:
[45,46,47,48,49,49.5,50,51,52,53,54]
I'm trying to run a loop using math.ceil and math.floor but there are issues.我正在尝试使用 math.ceil 和 math.floor 运行循环,但存在问题。
Is there a pythonic way to do this?有没有一种pythonic方法可以做到这一点?
You could do:你可以这样做:
from math import ceil
a = 49.5
diff = 5
ceil_a = int(ceil(a))
output_list = list(range(ceil_a - diff, ceil_a)) + [a] + list(range(ceil_a, ceil_a + diff))
print(output_list)
import math
temp=[]
for i in range(40,55):
if i % 5 == 0:
j = f'{i-1}.5, {i}'
temp.append(j)
else:
temp.append(i)
print(temp)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.