Python字典会抛出无效的语法

Python dictionary throws invalid syntax

为什么此代码引发无效语法错误?我很肯定这在三个月前就没问题了。关于字典,python语言有什么变化吗?错误如下:

1
2
3
File"code.py", line 4
'den'{'adm':2.4,'den':0,'ear':3.5,'edi':3.6,'eug':1.6,'fra':3.0,'thu':3.1,\
SyntaxError: invalid syntax

如果我把第4行和第5行注释掉,错误就指向第7行。错误总是引用第二个单引号字符。在第四行中,它指向"den"中n后面的'

代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
milevalue = {'adm':{'adm':0,'den':2.4,'ear':3,'edi':3.2,'eug':2.6,'fra':2.1,'thu':1.2,\
'hor':3.4,'lon':1.5,'rid':7.7,'hig':1.8,'tho':5.5,'was':1.8,'aca':0.8,'cen':.9}\
\
'den':{'adm':2.4,'den':0,'ear':3.5,'edi':3.6,'eug':1.6,'fra':3.0,'thu':3.1,\
'hor':1.6,'lon':1.4,'rid':7.4,'hig':2.3,'tho':5.8,'was':0.7,'aca':2.6,'cen':1.8},\
\
'ear':{'adm':3,'den':3.5,'ear':0,'edi':0.1,'eug':1.9,'fra':0.9,'thu':2.1,\
'hor':2.9,'lon':4.4,'rid':4.8,'hig':1.4,'tho':2.7,'was':3.2,'aca':2.4,'cen':2.5},\
\
'edi':{'adm':3.2,'den':2.4,'ear':0.1,'edi':0,'eug':2.0,'fra':1.1,'thu':2.3,\
'hor':2.7,'lon':4.5,'rid':4.7,'hig':1.5,'tho':2.5,'was':3.4,'aca':2.5,'cen':2.6},\
\
'eug':{'adm':2.6,'den':1.6,'ear':1.9,'edi':2.0,'eug':0,'fra':1.8,'thu':3.0,\
'hor':1,'lon':2.6,'rid':6.4,'hig':1.4,'tho':4.3,'was':1.4,'aca':2.5,'cen':1.7},\
\
'fra':{'adm':2.1,'den':3.0,'ear':0.9,'edi':1.1,'eug':1.8,'fra':0,'thu':1.2,\
'hor':2.8,'lon':3.4,'rid':5.7,'hig':0.4,'tho':3.6,'was':2.3,'aca':1.5,'cen':1.6},\
\
'thu':{'adm':1.2,'den':3.1,'ear':2.1,'edi':2.3,'eug':3.0,'fra':1.2,'thu':0,\
'hor':4,'lon':2.9,'rid':7.1,'hig':1.6,'tho':5.1,'was':2.4,'aca':0.6,'cen':1.6},\
\
'hor':{'adm':3.4,'den':1.6,'ear':2.9,'edi':2.7,'eug':1,'fra':2.8,'thu':4,\
'hor':0,'lon':2.7,'rid':6.1,'hig':2.4,'tho':4.4,'was':2,'aca':3.5,'cen':2.8},\
\
'lon':{'adm':1.5,'den':1.4,'ear':4.4,'edi':4.5,'eug':2.6,'fra':3.4,'thu':2.9,\
'hor':2.7,'lon':0,'rid':8.6,'hig':3.5,'tho':7.1,'was':1.6,'aca':2.5,'cen':2.5},\
\
'rid':{'adm':7.7,'den':7.4,'ear':4.8,'edi':4.7,'eug':6.4,'fra':5.7,'thu':7.1,\
'hor':6.1,'lon':8.6,'rid':0,'hig':5.9,'tho':2.4,'was':7.8,'aca':6.9,'cen':7},\
\
'hig':{'adm':1.8,'den':2.3,'ear':1.4,'edi':1.5,'eug':1.4,'fra':0.4,'thu':1.6,\
'hor':2.4,'lon':3.5,'rid':5.9,'hig':0,'tho':3.8,'was':1.9,'aca':1.3,'cen':1.1},\
\
'tho':{'adm':5.5,'den':5.8,'ear':2.7,'edi':2.5,'eug':4.3,'fra':3.6,'thu':5.1,\
'hor':4.4,'lon':7.1,'rid':2.4,'hig':3.8,'tho':0,'was':5.9,'aca':4.8,'cen':4.9},\
\
'was':{'adm':1.8,'den':0.7,'ear':3.2,'edi':3.4,'eug':1.4,'fra':2.3,'thu':2.4,\
'hor':2,'lon':1.6,'rid':7.8,'hig':1.9,'tho':5.9,'was':0,'aca':1.9,'cen':1.1}
\
'aca':{'adm':0.8,'den':2.6,'ear':2.4,'edi':2.5,'eug':2.5,'fra':1.5,'thu':0.6,\
'hor':3.5,'lon':2.5,'rid':6.9,'hig':1.3,'tho':4.8,'was':1.9,'aca':0,'cen':0.9}
\
'cen':{'adm':0.9,'den':1.8,'ear':2.5,'edi':2.6,'eug':1.7,'fra':1.6,'thu':1.6,\
'hor':2.8,'lon':2.5,'rid':7,'hig':1.1,'tho':4.9,'was':1.1,'aca':0.9,'cen':0}}


'cen':.9后面的右大括号后面似乎缺少逗号。尝试此修复:

1
2
3
... 'cen':.9} ,\
\
den:{...

我敢肯定,在这种情况下,这个文件三个月前不会正确运行。


这样的语法错误更容易被明智地使用空格发现。至少,错误消息会让您更好地了解到该在哪里查找。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
milevalue = {
  'adm': {
    'adm': 0,
    'den': 2.4,
    'ear': 3,
    'edi': 3.2,
    'hug': 2.6,
    'fra': 2.1,
    'the': 1.2,
    'hor': 3.4,
    'lon': 1.5,
    'rid': 7.7,
    'hig': 1.8,
    'tho': 5.5,
    'was': 1.8,
    'act': 0.8,
    'cen': .9
  }             # Oops, here's the missing comma
  'den': {
    'adm': 2.4,
    'den': 0,
    'ear': 3.5,
    'edi': 3.6,
    'hug': 1.6,
    'fra': 3.0,
  # etc


你在串'den'之前丢失了,


代码已修复。

缺少逗号",",请小心使用,否则应使用字典符号生成字典。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
milevalue = {'adm':{'adm':0,'den':2.4,'ear':3,'edi':3.2,'eug':2.6,'fra':2.1,'thu':1.2,\
'hor':3.4,'lon':1.5,'rid':7.7,'hig':1.8,'tho':5.5,'was':1.8,'aca':0.8,'cen':.9}\
\
'den':{'adm':2.4,'den':0,'ear':3.5,'edi':3.6,'eug':1.6,'fra':3.0,'thu':3.1,\
'hor':1.6,'lon':1.4,'rid':7.4,'hig':2.3,'tho':5.8,'was':0.7,'aca':2.6,'cen':1.8},\
\
'ear':{'adm':3,'den':3.5,'ear':0,'edi':0.1,'eug':1.9,'fra':0.9,'thu':2.1,\
'hor':2.9,'lon':4.4,'rid':4.8,'hig':1.4,'tho':2.7,'was':3.2,'aca':2.4,'cen':2.5},\
\
'edi':{'adm':3.2,'den':2.4,'ear':0.1,'edi':0,'eug':2.0,'fra':1.1,'thu':2.3,\
'hor':2.7,'lon':4.5,'rid':4.7,'hig':1.5,'tho':2.5,'was':3.4,'aca':2.5,'cen':2.6},\
\
'eug':{'adm':2.6,'den':1.6,'ear':1.9,'edi':2.0,'eug':0,'fra':1.8,'thu':3.0,\
'hor':1,'lon':2.6,'rid':6.4,'hig':1.4,'tho':4.3,'was':1.4,'aca':2.5,'cen':1.7},\
\
'fra':{'adm':2.1,'den':3.0,'ear':0.9,'edi':1.1,'eug':1.8,'fra':0,'thu':1.2,\
'hor':2.8,'lon':3.4,'rid':5.7,'hig':0.4,'tho':3.6,'was':2.3,'aca':1.5,'cen':1.6},\
\
'thu':{'adm':1.2,'den':3.1,'ear':2.1,'edi':2.3,'eug':3.0,'fra':1.2,'thu':0,\
'hor':4,'lon':2.9,'rid':7.1,'hig':1.6,'tho':5.1,'was':2.4,'aca':0.6,'cen':1.6},\
\
'hor':{'adm':3.4,'den':1.6,'ear':2.9,'edi':2.7,'eug':1,'fra':2.8,'thu':4,\
'hor':0,'lon':2.7,'rid':6.1,'hig':2.4,'tho':4.4,'was':2,'aca':3.5,'cen':2.8},\
\
'lon':{'adm':1.5,'den':1.4,'ear':4.4,'edi':4.5,'eug':2.6,'fra':3.4,'thu':2.9,\
'hor':2.7,'lon':0,'rid':8.6,'hig':3.5,'tho':7.1,'was':1.6,'aca':2.5,'cen':2.5},\
\
'rid':{'adm':7.7,'den':7.4,'ear':4.8,'edi':4.7,'eug':6.4,'fra':5.7,'thu':7.1,\
'hor':6.1,'lon':8.6,'rid':0,'hig':5.9,'tho':2.4,'was':7.8,'aca':6.9,'cen':7},\
\
'hig':{'adm':1.8,'den':2.3,'ear':1.4,'edi':1.5,'eug':1.4,'fra':0.4,'thu':1.6,\
'hor':2.4,'lon':3.5,'rid':5.9,'hig':0,'tho':3.8,'was':1.9,'aca':1.3,'cen':1.1},\
\
'tho':{'adm':5.5,'den':5.8,'ear':2.7,'edi':2.5,'eug':4.3,'fra':3.6,'thu':5.1,\
'hor':4.4,'lon':7.1,'rid':2.4,'hig':3.8,'tho':0,'was':5.9,'aca':4.8,'cen':4.9},\
\
'was':{'adm':1.8,'den':0.7,'ear':3.2,'edi':3.4,'eug':1.4,'fra':2.3,'thu':2.4,\
'hor':2,'lon':1.6,'rid':7.8,'hig':1.9,'tho':5.9,'was':0,'aca':1.9,'cen':1.1},
\
'aca':{'adm':0.8,'den':2.6,'ear':2.4,'edi':2.5,'eug':2.5,'fra':1.5,'thu':0.6,\
'hor':3.5,'lon':2.5,'rid':6.9,'hig':1.3,'tho':4.8,'was':1.9,'aca':0,'cen':0.9},
\
'cen':{'adm':0.9,'den':1.8,'ear':2.5,'edi':2.6,'eug':1.7,'fra':1.6,'thu':1.6,\
'hor':2.8,'lon':2.5,'rid':7,'hig':1.1,'tho':4.9,'was':1.1,'aca':0.9,'cen':0}}


您错过了一些逗号,修复了:

1
milevalue = {'adm':{'adm':0,'den':2.4,'ear':3,'edi':3.2,'eug':2.6,'fra':2.1,'thu':1.2,'hor':3.4,'lon':1.5,'rid':7.7,'hig':1.8,'tho':5.5,'was':1.8,'aca':0.8,'cen':.9},'den':{'adm':2.4,'den':0,'ear':3.5,'edi':3.6,'eug':1.6,'fra':3.0,'thu':3.1,'hor':1.6,'lon':1.4,'rid':7.4,'hig':2.3,'tho':5.8,'was':0.7,'aca':2.6,'cen':1.8},'ear':{'adm':3,'den':3.5,'ear':0,'edi':0.1,'eug':1.9,'fra':0.9,'thu':2.1,'hor':2.9,'lon':4.4,'rid':4.8,'hig':1.4,'tho':2.7,'was':3.2,'aca':2.4,'cen':2.5},'edi':{'adm':3.2,'den':2.4,'ear':0.1,'edi':0,'eug':2.0,'fra':1.1,'thu':2.3,'hor':2.7,'lon':4.5,'rid':4.7,'hig':1.5,'tho':2.5,'was':3.4,'aca':2.5,'cen':2.6},'eug':{'adm':2.6,'den':1.6,'ear':1.9,'edi':2.0,'eug':0,'fra':1.8,'thu':3.0,'hor':1,'lon':2.6,'rid':6.4,'hig':1.4,'tho':4.3,'was':1.4,'aca':2.5,'cen':1.7},'fra':{'adm':2.1,'den':3.0,'ear':0.9,'edi':1.1,'eug':1.8,'fra':0,'thu':1.2,'hor':2.8,'lon':3.4,'rid':5.7,'hig':0.4,'tho':3.6,'was':2.3,'aca':1.5,'cen':1.6},'thu':{'adm':1.2,'den':3.1,'ear':2.1,'edi':2.3,'eug':3.0,'fra':1.2,'thu':0,'hor':4,'lon':2.9,'rid':7.1,'hig':1.6,'tho':5.1,'was':2.4,'aca':0.6,'cen':1.6},'hor':{'adm':3.4,'den':1.6,'ear':2.9,'edi':2.7,'eug':1,'fra':2.8,'thu':4,'hor':0,'lon':2.7,'rid':6.1,'hig':2.4,'tho':4.4,'was':2,'aca':3.5,'cen':2.8},'lon':{'adm':1.5,'den':1.4,'ear':4.4,'edi':4.5,'eug':2.6,'fra':3.4,'thu':2.9,'hor':2.7,'lon':0,'rid':8.6,'hig':3.5,'tho':7.1,'was':1.6,'aca':2.5,'cen':2.5},'rid':{'adm':7.7,'den':7.4,'ear':4.8,'edi':4.7,'eug':6.4,'fra':5.7,'thu':7.1,'hor':6.1,'lon':8.6,'rid':0,'hig':5.9,'tho':2.4,'was':7.8,'aca':6.9,'cen':7},'hig':{'adm':1.8,'den':2.3,'ear':1.4,'edi':1.5,'eug':1.4,'fra':0.4,'thu':1.6,'hor':2.4,'lon':3.5,'rid':5.9,'hig':0,'tho':3.8,'was':1.9,'aca':1.3,'cen':1.1},'tho':{'adm':5.5,'den':5.8,'ear':2.7,'edi':2.5,'eug':4.3,'fra':3.6,'thu':5.1,'hor':4.4,'lon':7.1,'rid':2.4,'hig':3.8,'tho':0,'was':5.9,'aca':4.8,'cen':4.9},'was':{'adm':1.8,'den':0.7,'ear':3.2,'edi':3.4,'eug':1.4,'fra':2.3,'thu':2.4,'hor':2,'lon':1.6,'rid':7.8,'hig':1.9,'tho':5.9,'was':0,'aca':1.9,'cen':1.1},'aca':{'adm':0.8,'den':2.6,'ear':2.4,'edi':2.5,'eug':2.5,'fra':1.5,'thu':0.6,'hor':3.5,'lon':2.5,'rid':6.9,'hig':1.3,'tho':4.8,'was':1.9,'aca':0,'cen':0.9},'cen':{'adm':0.9,'den':1.8,'ear':2.5,'edi':2.6,'eug':1.7,'fra':1.6,'thu':1.6,'hor':2.8,'lon':2.5,'rid':7,'hig':1.1,'tho':4.9,'was':1.1,'aca':0.9,'cen':0}}