为什么这个字典定义引发语法错误? [英] Why does this dictionary definition raise a syntax error?

查看:174
本文介绍了为什么这个字典定义引发语法错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:


我正在定义一个字典,将日期数字映射到自己的单词。由于某些原因,以下代码引发了SyntaxError:invalid token并突出显示08

  days = {01:first ,02:第二,03:第三,04:第四,05:第五,06:第六,07:第七,08:第八,09:九 10:第十,
11:第十一,12:twelvth,13:第十三,14:第十四,15:第十五,16:十六,17:第十七,18:第十八,
19:十九,20:第二十,21:二十一,22:二十二,23:二十三 二十四,25:二十五,b $ b 26:二十六,27:二十七,28:二十八,29:二十九 30:第三十,31:第三十一}

修改代码,使08和09成为98和99停止任何错误,如下面的代码:

  days = {01:first,02: 第二,03:第三,04:第四,05:第五,06:第六,07:第七,98:第八,99:九 ,
11:第十一,12:twelvth,13:第十三 14:十四,15:十五,16:十六,17:十七,18:十八,
19:十九,20:第二十第二十一届,二十二日,二十二日,二十三日,二十四日,二十四日,二十五日,二十五号,二十六号,二十六号,二十六:第二十七,28:二十八,29:二十九,30:第三十,31:第三十一}
pre>

,输出成为:

  {1:'first ',2:'第二',3:'第三',第四,第四,第五,第六,第六,第七,第十,第十,第十一, 12:'twelvth',13:'ththteen',14:'第十四',15:'第十五',16:'十六',17:'十七',18:'十八',19:'十九' 二十,二十一,二十一,二十二,二十三,二十三,二十四,二十五,二十六,二十六',27:'二十七',28:'二十八',29:'二十九,30:'第30',31:'第三十一',98:'第八',99:'nineth'} 

以前错误的键已经移动到字典的末尾。



非常感谢那些发现这里发生的事情的人,



James

解决方案

号码 0 是一个整数文字的前缀,表示它是python中的八进制数。



...我意外地省略了,和@larsmans所以在他的评论中指出,将数字限制为仅包含数字 0 通过 7 ,不包括 8 9



虽然也值得注意的是,这是Python 2.x中的语法 - 它是根据Python 3.0进行更改的,表面上是因为您来到这里的确切原因。 PEP 3127 包含详细信息更改。



最相关的位:


几乎所有当前流行的电脑语言,包括C / C ++,Java,Perl和JavaScript,将前导零的数字序列视为八进制数。将这些数字视为十进制的支持者有一个非常有效的点 - [...]整个非计算机世界几乎完全使用十进制数。



Possible Duplicate:
Is there any reason why lVals = [1, 08, 2011] throws an exception?

I am defining a dictionary to map day numbers to their respective word. For some reason the following code raises a "SyntaxError: invalid token" and highlights "08"

days = {01:"first", 02:"second", 03:"third", 04:"fourth", 05:"fifth", 06:"sixth", 07:"seventh", 08:"eighth", 09:"nineth", 10:"tenth", 
    11:"eleventh", 12:"twelvth", 13:"thirteenth", 14:"fourteenth", 15:"fifteenth", 16:"sixteenth", 17:"seventeenth", 18:"eighteenth",
    19:"nineteenth", 20:"twentieth", 21:"twenty-first", 22:"twenty-second", 23:"twenty-third", 24:"twenty-fourth", 25:"twenty-fifth",
    26:"twenty-sixth", 27:"twenty-seventh", 28:"twenty-eighth", 29:"twenty-nineth", 30:"thirtieth", 31:"thirty-first"}

modifying the code so that 08 and 09 become 98 and 99 stops any errors, as in the code below:

days = {01:"first", 02:"second", 03:"third", 04:"fourth", 05:"fifth", 06:"sixth", 07:"seventh", 98:"eighth", 99:"nineth", 10:"tenth", 
    11:"eleventh", 12:"twelvth", 13:"thirteenth", 14:"fourteenth", 15:"fifteenth", 16:"sixteenth", 17:"seventeenth", 18:"eighteenth",
    19:"nineteenth", 20:"twentieth", 21:"twenty-first", 22:"twenty-second", 23:"twenty-third", 24:"twenty-fourth", 25:"twenty-fifth",
    26:"twenty-sixth", 27:"twenty-seventh", 28:"twenty-eighth", 29:"twenty-nineth", 30:"thirtieth", 31:"thirty-first"}

and the output becomes:

{1: 'first', 2: 'second', 3: 'third', 4: 'fourth', 5: 'fifth', 6: 'sixth', 7: 'seventh', 10: 'tenth', 11: 'eleventh', 12: 'twelvth', 13: 'thirteenth', 14: 'fourteenth', 15: 'fifteenth', 16: 'sixteenth', 17: 'seventeenth', 18: 'eighteenth', 19: 'nineteenth', 20: 'twentieth', 21: 'twenty-first', 22: 'twenty-second', 23: 'twenty-third', 24: 'twenty-fourth', 25: 'twenty-fifth', 26: 'twenty-sixth', 27: 'twenty-seventh', 28: 'twenty-eighth', 29: 'twenty-nineth', 30: 'thirtieth', 31: 'thirty-first', 98: 'eighth', 99: 'nineth'}

the previously erroneous keys have moved to the end of the dictionary.

Many thanks to the person who spots what is going on here,

James

解决方案

The number 0 is a prefix on an integer literal indicating that it is an octal number in python.

...which as I accidentally omitted, and as @larsmans so kindly pointed out in his comment, limits the number to containing only the numerals 0 through 7, excluding 8 and 9.

Though, also of note, this is the syntax in Python 2.x - it was changed as of Python 3.0, ostensibly for the exact reason that you came here. PEP 3127 contains the details of the change.

The most relevant bit:

Almost all currently popular computer languages, including C/C++, Java, Perl, and JavaScript, treat a sequence of digits with a leading zero as an octal number. Proponents of treating these numbers as decimal instead have a very valid point -- [...] the entire non-computer world uses decimal numbers almost exclusively.

这篇关于为什么这个字典定义引发语法错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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