gpt4 book ai didi

Python 字典抛出无效语法

转载 作者:太空宇宙 更新时间:2023-11-04 07:06:55 24 4
gpt4 key购买 nike

为什么这段代码会抛出无效语法错误?我很确定这在三个月前运行良好。 Python 语言在字典方面有什么变化吗?错误如下:

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 之后的 '

代码如下:

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 后的右大括号后缺少一个逗号。试试这个修复:

... 'cen':.9} ,\
\
den:{...

我敢肯定,在这种情况下,这个文件在三个月前是无法正常运行的。

关于Python 字典抛出无效语法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38733475/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com