这个问题让我很难过.如何在Python中对数字进行综合?
我尝试了一些(数字),但它将数字向下舍入.例:
round(2.3) = 2.0 and not 3, what I would like
Run Code Online (Sandbox Code Playgroud)
我尝试了int(数字+ .5),但它再次将数字向下舍入!例:
int(2.3 + .5) = 2
Run Code Online (Sandbox Code Playgroud)
然后我尝试了一下(数字+ .5)但它在边缘情况下不起作用.例:
WAIT! THIS WORKED!
Run Code Online (Sandbox Code Playgroud)
请指教.
Python 3.1 (r31:73574, Jun 26 2009, 20:21:35) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> 2/2
1.0
Run Code Online (Sandbox Code Playgroud)
这是有意的吗?我强烈记得早期版本的回归int/int=int?我应该怎么做,是否有新的分区运算符或者我必须总是演员?
我试图将一组数字从-100标准化为0到10-100的范围,并且只是注意到即使没有变量,也没有评估我期望它的方式:
>>> (20-10) / (100-10)
0
Run Code Online (Sandbox Code Playgroud)
浮动部门也不起作用:
>>> float((20-10) / (100-10))
0.0
Run Code Online (Sandbox Code Playgroud)
如果除法的任何一方被强制转换为浮点数,它将起作用:
>>> (20-10) / float((100-10))
0.1111111111111111
Run Code Online (Sandbox Code Playgroud)
第一个示例中的每一侧都评估为int,这意味着最终答案将被转换为int.因为0.111小于.5,所以它会变为0.在我看来,这不是透明的,但我想这就是它的方式.
解释是什么?
为什么Python会给出"错误"的答案?
x = 16
sqrt = x**(.5)
returns 4
sqrt = x**(1/2)
returns 1
Run Code Online (Sandbox Code Playgroud)
是的,我知道import math并使用sqrt.但我正在寻找上述答案.
我需要测试从1到1000的每个数字是3的倍数还是5的倍数.我认为我这样做的方法是将数字除以3,如果结果是整数那么它就会是3的倍数.与5相同.
如何测试数字是否为整数?
这是我目前的代码:
n = 0
s = 0
while (n < 1001):
x = n/3
if isinstance(x, (int, long)):
print 'Multiple of 3!'
s = s + n
if False:
y = n/5
if isinstance(y, (int, long)):
s = s + n
print 'Number: '
print n
print 'Sum:'
print s
n = n + 1
Run Code Online (Sandbox Code Playgroud) 我知道Python//向负无穷大舍入,而在C++中则/是截断,向0舍入。
到目前为止,这是我所知道的:
|remainder|
-12 / 10 = -1, - 2 // C++
-12 // 10 = -2, + 8 # Python
12 / -10 = -1, 2 // C++
12 // -10 = -2, - 8 # Python
12 / 10 = 1, 2 // Both
12 // 10 = 1, 2
-12 / -10 = 1, - 2 // Both
= 2, + 8
C++:
1. m%(-n) == m%n
2. -m%n == -(m%n)
3. (m/n)*n …Run Code Online (Sandbox Code Playgroud) 有谁知道如何转换int为float.
由于某种原因,它继续打印0.我希望它打印一个特定的小数.
sum = 144
women_onboard = 314
proportion_womenclass3_survived = sum / np.size(women_onboard)
print 'Proportion of women in class3 who survived is %s' % proportion_womenclass3_survived
Run Code Online (Sandbox Code Playgroud) 我似乎无法在这个网站上找到答案,尽管看起来它很常见.我试图输出一个双倍的两个文件中的行数比.
#Number of lines in each file
inputLines = sum(1 for line in open(current_file))
outputLines = sum(1 for line in open(output_file))
Run Code Online (Sandbox Code Playgroud)
然后得到比例:
ratio = inputLines/outputLines
Run Code Online (Sandbox Code Playgroud)
但即使我初始化它,这个比率似乎总是一个整数和圆形:
ratio = 1.0
Run Code Online (Sandbox Code Playgroud)
谢谢.
在一次真正超现实的体验中,我花了20分钟完成了一项我认为需要20秒的任务.
我想使用3个或更多位置的小数.我无法获得超过1个地方的任何东西,即使在那种情况下,这也是无稽之谈.
例如,我无法1/3显示除0or 之外的任何内容0.0.
谷歌搜索引导我到十进制,但十进制也没有帮助.
请结束这种折磨并告诉我要做什么1/3才能出现.333!
编辑谢谢大家清除它!显然,计算机科学家发明了一种称为整数除法的东西来混淆和激怒数学家.谢谢你能够消除我的困惑.
嘿这是一个演示,向我的一些同学展示python和编码的介绍.下面的代码应该能够采用类似的列表[0,1],如果使用该average函数运行将返回0.5.使用列表运行时,下面的函数返回错误'list' object has no attribute 'len'.如何在不删除该len()功能的情况下使该功能正常工作
def average(lst):
total = 0
for item in lst:
total += item
averageGrade= total / lst.len()
return averageGrade
Run Code Online (Sandbox Code Playgroud)
我怎么能让它返回一个浮点而不是一个整数
python ×10
integer ×3
math ×2
python-2.x ×2
python-3.x ×2
rounding ×2
c++ ×1
division ×1
modulus ×1
sqrt ×1