string.atoi和string.atol坏了吗? [英] string.atoi and string.atol broken?

查看:75
本文介绍了string.atoi和string.atol坏了吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我认为string.atoi和string.atol中可能存在错误。这是空闲时输出的一些


Python 2.3.4(#2,2005年1月5日,08:24:51)关于linux2的[GCC 3.3.5(Debian 1:3.3.5-5)]
类型版权,信用和或许可证()了解更多信息。

************************************** ************ **************
个人防火墙软件可能会警告使用此计算机IDLE对其子进程的连接''内部环回
界面。此连接在任何外部
接口上都不可见,并且没有数据发送到Internet或从Internet接收。
********************* ***************************** **************
IDLE 1.0.4

将字符串导入为s
s.atoi('''',3)2 s.atoi(''4' ',3)
Traceback(最近一次调用最后一次):
文件"< pyshell#2>",第1行,in -ople /
s.atoi(''4 '',3)
文件" /usr/lib/python2.3/string.py" ;,第220行,atoi
return _int(s,base)
ValueError:invalid literal for int():4 s.atoi(''''',11)13 s.atoi(''''',4)7 s.atoi('''',4)6 s.atoi(' '8'',4)
Traceback(最近一次调用最后一次):
文件"< pyshell#6>",第1行,在-toplevel-
s.atoi( ''8'',4)
文件" /usr/lib/python2.3/string.py" ;,第220行,atoi
return _int(s,ba se)
ValueError:int()的文字无效:8




s.atoi(''''', 3)应该导致11


s.atoi(''''','4)应该导致31


s.atoi( ''12'',4)应该导致30

s.atoi(''''',4)是合法的,但它会产生错误。


这是一个错误,还是我错过了一些明显的东西?


TIA,

Mike

解决方案

Mike Moum写道:

我认为string.atoi和string.atol可能存在错误。这是空闲时的一些输出。

Python 2.3.4(#2,2005年1月5日,08:24:51)
关于linux2的[GCC 3.3.5(Debian 1:3.3.5-5)]
类型版权,信用和或许可证()了解更多信息。

************************************** ************ **************
个人防火墙软件可能会警告使用此计算机IDLE对其子进程的连接''内部环回
界面。此连接在任何外部
接口上都不可见,并且没有数据发送到Internet或从Internet接收。
********************* ***************************** **************
IDLE 1.0.4

>导入字符串为s
> s.atoi('''',3)



2

> s.atoi(''4'',3)



Traceback(最近一次调用最后一次):
文件"< pyshell#2>",第1行,在-toplevel-
s.atoi(''4'',3)
文件" / usr / lib /python2.3/string.py" ;,第220行,atoi
return _int(s,base)
ValueError:int()的无效文字:4

> s.atoi(''12'',11)



13

> s.atoi(''13 '',4)



7

> s.atoi(''12'',4)



6
> s.atoi(''8'',4)



Traceback(最近一次调用最后一次):
文件"< pyshell#6>",第1行,在-toplevel-
s.atoi(''8'',4)
文件" /usr/lib/python2.3/string.py" ,第220行,atoi
return _int(s,base)
ValueError:int()的无效文字:8



s.atoi(''4 '',3)应该导致11

s.atoi(''''','4)应该导致31

s.atoi('''12'' ,4)应该导致30

s.atoi(''''',4)是合法的,但它会产生错误。

这是一个错误,还是我错过了一些明显的东西吗?
[...]




这不是一个错误,你错过了一些明显的东西。


string.atoi的第二个参数(或int builtin)

是你想要的数字基数(或基数)

转换为。


例如string.atoi(" 777,8结果为511,

因为7 * 8 ** 2 + 7 * 8 ** 1 + 7 * 8 ** 0 = 511.


只是出于好奇:

你觉得atoi有什么作用?

我不明白你是怎么想到atoi(''4' ',3)

应该产生11.

再见,

Dennis


Mike Moum写道:

> s.atoi(''4'',3)
Traceback(最近一次调用最后一次):
文件"< pyshell#2>",第1行,in -ople / - br / > s.atoi(''4'',3)
文件" /usr/lib/python2.3/string.py" ;,第220行,atoi
return _int(s,base)
ValueError:int()的文字无效:4


你期望基数3中4的值是多少?基数3中没有'4''

...只有''0'','1''和'''''。

> s.atoi(''8'',4)



回溯(最近一次调用最后一次):
文件"< pyshell#6>",第1行,in -ople /
s.atoi(''8'',4)
文件" /usr/lib/python2.3/string.py" ;,第220行,atoi return _int(s,base)
ValueError:int()的无效文字:8




并且没有''8''基地3要么。


这是一个错误,还是我错过了一些明显的东西?




嗯,首先,除非你在2.0之前使用Python,否则你会错过string.atoi的
。你应该使用int(),

,它可以采用相同的参数。


我认为其次,你缺少那个int(字符串,基数) )将给定字符串的

转换为int,假设字符串在给定的基数中。

如果您提供的数字超出了<的数字范围br />
对该基数有效,你会收到错误。


Steve


Mike Moum写道:

s.atoi(''4'',3)应该导致11

s.atoi(''''','4)应该得到在31

s.atoi(''12'',4)应该导致30
s.atoi(''''','4)是合法的,但是它会产生错误。

这是一个错误,还是我错过了一些明显的东西?




你和atoi()似乎不同意转换的方向,和
atoi()获胜:-)。它将数字的字符串表示转换为

对应的整数。第二个参数指定了这个

字符串的基数。

你似乎想要类似的东西

import string


def itoa(n,base):

断言2< = base< = 16

如果n< 0:

digits = [" - "]

n = -n

else:

digits = []

而n:

n,m = divmod(n,base)

digits.append(string.hexdigits [m])

digits.reverse()

返回"" .join(数字)
如果__name__ ==" __ main __"


断言itoa(4,3)==" 11"

断言itoa(13,4)==" 31"

断言itoa(12,4)==" 30"

断言itoa(8,4)==" 20"

彼得


I think there may be a bug in string.atoi and string.atol. Here''s some
output from idle.

Python 2.3.4 (#2, Jan 5 2005, 08:24:51)
[GCC 3.3.5 (Debian 1:3.3.5-5)] on linux2
Type "copyright", "credits" or "license()" for more information.

************************************************** **************
Personal firewall software may warn about the connection IDLE
makes to its subprocess using this computer''s internal loopback
interface. This connection is not visible on any external
interface and no data is sent to or received from the Internet.
************************************************** **************

IDLE 1.0.4

import string as s
s.atoi(''2'',3) 2 s.atoi(''4'',3)
Traceback (most recent call last):
File "<pyshell#2>", line 1, in -toplevel-
s.atoi(''4'',3)
File "/usr/lib/python2.3/string.py", line 220, in atoi
return _int(s, base)
ValueError: invalid literal for int(): 4 s.atoi(''12'',11) 13 s.atoi(''13'',4) 7 s.atoi(''12'',4) 6 s.atoi(''8'',4)
Traceback (most recent call last):
File "<pyshell#6>", line 1, in -toplevel-
s.atoi(''8'',4)
File "/usr/lib/python2.3/string.py", line 220, in atoi
return _int(s, base)
ValueError: invalid literal for int(): 8



s.atoi(''4'',3) should result in 11

s.atoi(''13'',4) should result in 31

s.atoi(''12'',4) should result in 30

s.atoi(''8'',4) is legitimate, but it generates an error.

Is this a bug, or am I missing something obvious?

TIA,
Mike

解决方案

Mike Moum wrote:

I think there may be a bug in string.atoi and string.atol. Here''s some
output from idle.

Python 2.3.4 (#2, Jan 5 2005, 08:24:51)
[GCC 3.3.5 (Debian 1:3.3.5-5)] on linux2
Type "copyright", "credits" or "license()" for more information.

************************************************** **************
Personal firewall software may warn about the connection IDLE
makes to its subprocess using this computer''s internal loopback
interface. This connection is not visible on any external
interface and no data is sent to or received from the Internet.
************************************************** **************

IDLE 1.0.4

>import string as s
>s.atoi(''2'',3)



2

>s.atoi(''4'',3)



Traceback (most recent call last):
File "<pyshell#2>", line 1, in -toplevel-
s.atoi(''4'',3)
File "/usr/lib/python2.3/string.py", line 220, in atoi
return _int(s, base)
ValueError: invalid literal for int(): 4

>s.atoi(''12'',11)



13

>s.atoi(''13'',4)



7

>s.atoi(''12'',4)



6

>s.atoi(''8'',4)



Traceback (most recent call last):
File "<pyshell#6>", line 1, in -toplevel-
s.atoi(''8'',4)
File "/usr/lib/python2.3/string.py", line 220, in atoi
return _int(s, base)
ValueError: invalid literal for int(): 8



s.atoi(''4'',3) should result in 11

s.atoi(''13'',4) should result in 31

s.atoi(''12'',4) should result in 30

s.atoi(''8'',4) is legitimate, but it generates an error.

Is this a bug, or am I missing something obvious?
[...]



That''s not a bug, you''r missing something obvious.

The second parameter of string.atoi (or the int builtin)
is the base (or radix) in which the number you want to
convert is given.

For example string.atoi("777", 8) results in 511,
because 7 * 8**2 + 7 * 8**1 + 7 * 8**0 = 511.

Just out of curiosty:
What did you think what atoi does?
I don''t understand how you came to expect that atoi(''4'',3)
should result in 11.
Bye,
Dennis


Mike Moum wrote:

> s.atoi(''4'',3)
Traceback (most recent call last):
File "<pyshell#2>", line 1, in -toplevel-
s.atoi(''4'',3)
File "/usr/lib/python2.3/string.py", line 220, in atoi
return _int(s, base)
ValueError: invalid literal for int(): 4
What did you expect the value of ''4'' in base 3 to be? There is no ''4''
in base 3... only ''0'', ''1'' and ''2''.

> s.atoi(''8'',4)



Traceback (most recent call last):
File "<pyshell#6>", line 1, in -toplevel-
s.atoi(''8'',4)
File "/usr/lib/python2.3/string.py", line 220, in atoi
return _int(s, base)
ValueError: invalid literal for int(): 8



And no ''8'' in base 3 either.

Is this a bug, or am I missing something obvious?



Well, first of all, unless you''re using a Python before 2.0, you''re
missing that string.atoi is deprecated. You should be using int(),
which can take the same parameters.

I think secondly, you''re missing that int(string, base) converts the
given string to an int assuming that the string is in the given base.
If the numbers you provide are out of the range of the digits that are
valid for that base, you will get an error.

Steve


Mike Moum wrote:

s.atoi(''4'',3) should result in 11

s.atoi(''13'',4) should result in 31

s.atoi(''12'',4) should result in 30

s.atoi(''8'',4) is legitimate, but it generates an error.

Is this a bug, or am I missing something obvious?



You and atoi() seem to disagree about the direction of the conversion, and
atoi() wins :-). It converts a string representation of a number into the
corresponding integer. The second parameter specifies in what base this
string is given.
You seem to want something like

import string

def itoa(n, base):
assert 2 <= base <= 16
if n < 0:
digits = ["-"]
n = -n
else:
digits = []
while n:
n, m = divmod(n, base)
digits.append(string.hexdigits[m])
digits.reverse()
return "".join(digits)

if __name__ == "__main__":
assert itoa(4, 3) == "11"
assert itoa(13, 4) == "31"
assert itoa(12, 4) == "30"
assert itoa(8, 4) == "20"
Peter


这篇关于string.atoi和string.atol坏了吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆