You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python字典定义触发Syntax Error,请求技术排查与修复

Python字典定义语法错误的解决方法

你的代码里有几个明显的语法错误,还有一些需要注意的问题,逐个给你说明:

1. 字典构造的核心语法错误

你用dict()函数创建字典时,错误地使用了==(比较运算符)来表示键值对,这完全不符合Python的语法规则。Python中创建字典的正确方式主要有两种:

  • 直接用大括号{}包裹,键和值用冒号:分隔,比如{"key": value}
  • 使用dict()函数时,要么传入键=值形式的参数(仅适用于键是合法标识符的情况),要么传入包含(键, 值)元组的可迭代对象

你的场景里有大量带空格、特殊字符的字符串键,显然用大括号直接定义是最直观且不易出错的方式。

2. 未用引号包裹的字符串

代码里的I don没有加引号,Python会把它当成变量名处理,而don前面的空格直接触发了语法错误,这也是报错箭头指向的位置。根据上下文推测,这里应该是你输入时的笔误,正确写法应该是带引号的字符串,比如"I don't know"。

修正后的完整代码

d = {
    0: 0,
    "I don't know": 1,  # 修正未加引号的问题,补全合理字符串
    "Unknown": 2,
    "Unsure": 3,
    "Hacking": 4,
    "Anonymity": 5,
    "Privacy": 6,
    "Addiction": 7,
    "Catfishing": 8,
    "Loss of interest in the real world": 9,
    "Headaches": 10,
    "Mental health": 11,
    "Heart attack": 12,
    "Financial collapse": 13,
    "Anonymity & online harassment": 14,
    "Dissociation": 15,
    "Personal data and privacy terms": 6,
    "Addiction and neglecting the real world": 16,
    "Scamming": 17,
    "Reduce real-life interaction": 18,
    "Legal issues": 19,
    "Digital property can be damaged more easily": 20,
    "I don't use metaverse": 21,
    "Program error": 22,
    "Scams and sextings": 23,
    "Escapicism": 24,
    "Mental risks": 11,
    "Identity fraud": 25,
    "Scammers and sexual predators": 23,
    "Scams and catfishing": 26,
    "Meeting someone when in reality it's someone else behind a screen": 8,
    "Private manners": 27,
    "Fraud": 17,
    "Cyber bullying": 28,
    "Bad advertisement content": 29,
    "Wrong knowledgement": 30,
    "Reality confusion": 31,
    "Security risks & financial risks": 32,  # 修正拼写错误:finanical→financial
    "Sexual harassment": 33,
    "lag issues": 34,
    "Data collection without consent, Addiction": 35,
    "Identity theft": 36,
    "Wrong investment": 13,
    "GDPR": 37,
    "Addiction and health problems": 38,  # 修正拼写错误:probelems→problems
    "Infringement of personal information": 39,
    "Crime and anonymity": 5,
    "Invasion of privacy": 6,
    "Privacy and hacking": 40,
    "Crime": 41,
    "Unable to distinguish between reality and virtuality": 31,
    "Leak of private informations": 39,
    "Privacy and security risks that come with technology": 42,
    "Losing touch with reality": 15,
    "People using it to bully/harrass others": 43,
    "Sex crimes and theft": 41,
    "Deterioration of face to face communication": 66
}

额外注意事项

  • 顺便修正了几个拼写错误,你可以核对是否符合原意
  • 字典里存在多个重复键(比如"Privacy"多次出现、"Mental health"和"Mental risks"都对应11),Python字典会自动保留最后一次出现的键值对。如果需要一个键对应多个值,建议用列表或其他数据结构存储。

内容的提问来源于stack exchange,提问作者Yoohyuk Kim

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.21 10:48:15