python中的数字 [英] Numbers in python
问题描述
基本上,我的代码已经差不多完成了,但是我在这个过程的最后阶段难以获得
。我有一个程序,
通过在字典中查找不同的值来分配不同的单词
在字典中:
例如如果THE在写作中,它会分配0.965
并且一旦读取整个段落,它将返回
格式中的所有数字,如下所示:
[''0.965'',''1.000'',''0.291'',''1.000'',''0.503'']
但是,我似乎无法将程序视为
数字。如果我把它们放在字典中作为''''= int(0.965)
程序返回1.0,如果我把''THE'= = float(0.965)它返回
0.96555555549或类似的东西。这些都不对!我基本上需要将字符串中的每个项目作为一个数字来访问,因为
我的上一个函数我希望将它们全部乘以每个
其他。
我为最后一位尝试了两位代码,但两者都没有工作(我不确定第一位)一个,但第二个应该
工作我想如果我能弄清楚如何返回值为
数字):
第一个代码
value =密码子[x] *密码子[x + 1]
x =(int)
x = 0
打印价值
x + = 2
if(x
new_value = value *密码子[x]
value = new_value
x + = 1
else:
print new_value
这会给出错误消息
Traceback(最近一次调用最后一次):
文件" C:\ Python24 \ code2",第88行,在-toplevel-
值=密码子[x] *密码子[x + 1]
NameError :名称''x''未定义
代码2 - 最可能的代码
prod = 1
for item in(密码子):prod * = item
prod
print prod
给出此错误消息:
回溯(最近一次调用最后一次):
文件C:\Python24 \ code2,第90行,在-toplevel-
for item in (密码子):prod * = item
TypeError:不能将序列乘以非int
任何人都有任何想法?
Dave 。
>但是,我似乎无法让程序将数字视为数字。如果我把它们放在字典中作为''''= int(0.965)
程序返回1.0
它确定_not_返回1.0 - 它返回1.并且这就是所有它可以
返回是一个整数,根据定义没有小数部分。
如果我把''THE'= = float(0.965)它返回
0.96555555549或类似的东西。这些都不对!我基本上需要将字符串中的每个项目作为一个数字来访问,因为对于我的上一个函数,我想将它们全部加在一起,每个
其他。
如果您使用花车,那么_is_正确的数字 - 您只需要考虑
帐户,基于10的分数不能总是由
基于2的IEEE浮点数,导致舍入错误。你看到的是什么
。
http://wiki.python.org/moin/Represen...ht=%28float%29
你要不要不关心细微差别,使用浮动。或者你这样做,
然后使用python 2.4中引入的十进制类
Diez
写道:
如果我把它们作为''THE'= int(0.965)放在字典中,
程序返回1.0,如果我把''THE'=浮动(0.965)它返回
0.96555555549或类似的东西。这些都不对!
您的系统似乎真的搞砸了。 int(0.965)应为0,并且
float(0.965)应为0.96499999999999997或类似的东西。
阅读
http://www.python。 org / doc / faq / genera ...-如此不准确
br *********** @ hotmail.com 写道:
基本上,我有一个代码几乎已经完成但我在这个过程的最后阶段很困难。我有一个程序,通过在字典中查找
来分配不同的单词
例如,如果THE在写作中,它指定0.965
格式中的所有数字,如下所示:
[''0.965'',''1.000'',' '0.291'',''1.000'',''0.503'']
然而,我似乎无法让程序将数字视为数字。如果我将它们放在字典中作为''''= int(0.965),
程序返回1.0,如果我把''THE'= = float(0.965)它返回
0.96555555549或类似的东西。这些都不对!
int(0.965)是0而int(0.965)是一个错误,所以我不确定你是什么
确实得到1.0。
但0.965和0.96499999999999997确实是一回事,当
存储为二进制浮点数时:
0.965
0.96499999999999997 0.965 == 0.96499999999999997
真实
的解释,请参阅此页:
http://docs.python.org/tut/node16.html
在你的情况下,在途中明确格式化数字可能是好的
足够,例如
print"%。3f" %价值
了解详情,请参阅:
http://docs.python.org/lib/typesseq-strings
如果你真的需要支持十进制浮点数算术,请参阅:
http:// docs .python.org / lib / module-decimal.html
< / F>
Basically, I have a code with is almost finished but I''ve having
difficultly with the last stage of the process. I have a program that
gets assigns different words with a different value via looking them up
in a dictionary:
eg if THE is in the writing, it assigns 0.965
and once the whole passage is read it returns all the numbers in the
format as follows:
[''0.965'', ''1.000'', ''0.291'', ''1.000'', ''0.503'']
However, I can''t seem to get the program to treat the numbers as
numbers. If I put them in the dictionary as ''THE'' = int(0.965) the
program returns 1.0 and if I put ''THE'' = float(0.965) it returns
0.96555555549 or something similar. Neither of these are right! I
basically need to access each item in the string as a number, because
for my last function I want to multiply them all together by each
other.
I have tried two bits of code for this last bit, but neither are
working (I''m not sure about the first one but the second one should
work I think if I could figure out how to return the values as
numbers):
1st code
value = codons[x] * codons[x+1]
x = (int)
x = 0
print value
x +=2
if (x<r):
new_value = value * codons[x]
value = new_value
x +=1
else:
print new_value
This gives the error message
Traceback (most recent call last):
File "C:\Python24\code2", line 88, in -toplevel-
value = codons[x] * codons[x+1]
NameError: name ''x'' is not defined
Code 2 - the most likely code
prod = 1
for item in (codons): prod *= item
prod
print prod
Gives this error message:
Traceback (most recent call last):
File "C:\Python24\code2", line 90, in -toplevel-
for item in (codons): prod *= item
TypeError: can''t multiply sequence by non-int
Anyone got any ideas?
Dave.
> However, I can''t seem to get the program to treat the numbers asnumbers. If I put them in the dictionary as ''THE'' = int(0.965) the
program returns 1.0
It certainoly does _not_ return 1.0 - it returns 1. And that is all it can
return for being an integer that has by definition no fractional part.
and if I put ''THE'' = float(0.965) it returns
0.96555555549 or something similar. Neither of these are right! I
basically need to access each item in the string as a number, because
for my last function I want to multiply them all together by each
other.
It _is_ the right number if you use floats - you just have to take into
account that 10-based fractions can''t always be represented properly by
2-based IEEE floating points, resulting in rounding errors. Which is what
you see.
http://wiki.python.org/moin/Represen...ht=%28float%29
Either you don''t care about the minor differences, the use float. Or you do,
then use the decimal-class introduced in python 2.4
Diez
wrote:
If I put them in the dictionary as ''THE'' = int(0.965) the
program returns 1.0 and if I put ''THE'' = float(0.965) it returns
0.96555555549 or something similar. Neither of these are right!
Your system seems to be really screwed. int(0.965) should be 0, and
float(0.965) should be 0.96499999999999997 or something similar.
Read
http://www.python.org/doc/faq/genera...-so-inaccurate
br***********@hotmail.com wrote:
Basically, I have a code with is almost finished but I''ve having
difficultly with the last stage of the process. I have a program that
gets assigns different words with a different value via looking them up
in a dictionary:
eg if THE is in the writing, it assigns 0.965
and once the whole passage is read it returns all the numbers in the
format as follows:
[''0.965'', ''1.000'', ''0.291'', ''1.000'', ''0.503'']
However, I can''t seem to get the program to treat the numbers as
numbers. If I put them in the dictionary as ''THE'' = int(0.965) the
program returns 1.0 and if I put ''THE'' = float(0.965) it returns
0.96555555549 or something similar. Neither of these are right!
int(0.965) is 0 and int("0.965") is an error, so I''m not sure what you
did to get 1.0.
but 0.965 and 0.96499999999999997 is indeed the same thing, when
stored as binary floating point numbers:
0.965 0.96499999999999997 0.965 == 0.96499999999999997
True
for an explanation, see this page:
http://docs.python.org/tut/node16.html
in your case, explicitly formatting the numbers on the way might be good
enough, e.g.
print "%.3f" % value
for details, see:
http://docs.python.org/lib/typesseq-strings
if you really need support for decimal floating point arithmetics, see:
http://docs.python.org/lib/module-decimal.html
</F>
这篇关于python中的数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!