【问题标题】:Python Syntax Error - following the example given and cannot work out where I have gone wrong [closed]Python语法错误-按照给出的示例,无法找出我出错的地方[关闭]
【发布时间】:2021-08-27 08:48:28
【问题描述】:

我目前正在学习使用 python 3。

我正在按照提供的说明/演练进行一些练习。

我在尝试更改列名时开始收到以下错误:

File "<ipython-input-25-591a166ee92a>", line 5
    'human development index (male)':"hdi_m"
                                    ^
SyntaxError: invalid syntax

这是我使用的代码:

gender_development.rename(columns={'gdi rank':"gdi_rank",
                                  'gender development index (gdi)':"gdi"
                                  'human development index (male)':"hdi_m"
                                  'life expectancy at birth (female)':"life_expectancy_f"
                                  'life expectancy at birth (male)':"life_expectancy_m"
                                  'expected years of educaton (female)':"expected_education_years_f"
                                  'expected years of education (male)':"expected_education_years_m"
                                  'mean years of education (female)':"mean_education_years_f"
                                  'mean years of education (male)':"mean_education_years_m"
                                  'estimated gross national income per capita (female)':"gross_income_pc_f"
                                  'estimated gross national income per capita (male)':"gross_income_pc_m"
                                  'human development index (female)':"hdi_f"},
                         inplace=True)

有什么想法吗?

【问题讨论】:

  • 字典中每一行的末尾都需要逗号。

标签: python python-3.x dictionary syntax-error


【解决方案1】:

您似乎缺少一些逗号,它们在 python 字典中用于分隔键值对。注意以下代码中的附加逗号:

gender_development.rename(columns={'gdi rank': "gdi_rank",
                               'gender development index (gdi)': "gdi",
                               'human development index (male)': "hdi_m",
                               'life expectancy at birth (female)': "life_expectancy_f",
                               'life expectancy at birth (male)': "life_expectancy_m",
                               'expected years of educaton (female)':"expected_education_years_f",
                               'expected years of education (male)': "expected_education_years_m",
                               'mean years of education (female)':"mean_education_years_f",
                               'mean years of education (male)': "mean_education_years_m",
                               'estimated gross national income per capita (female)':"gross_income_pc_f",
                               'estimated gross national income per capita (male)': "gross_income_pc_m",
                               'human development index (female)':"hdi_f"}, inplace = True)

所有编程语言都对此类语法错误非常敏感。尽量注意使用正确的结构——随着时间的推移,这对你来说会变得很自然。

【讨论】:

  • 谢谢!我一直在努力寻找大错误并忘记小错误。我最终会到达那里。
【解决方案2】:

另外,试试 linter。 linter (可能)会抱怨,行长超过 80 个字符,缩进不足,视觉上缺少缩进,: 之后缺少空间等。

清除所有需要的错误(除了缺少的gender_development):

gender_development.rename(columns={
    'gdi rank': "gdi_rank",
    'gender development index (gdi)': "gdi",
    'human development index (male)': "hdi_m",
    'life expectancy at birth (female)': "life_expectancy_f",
    'life expectancy at birth (male)': "life_expectancy_m",
    'expected years of educaton (female)': "expected_education_years_f",
    'expected years of education (male)': "expected_education_years_m",
    'mean years of education (female)': "mean_education_years_f",
    'mean years of education (male)': "mean_education_years_m",
    'estimated gross national income per capita (female)': "gross_income_pc_f",
    'estimated gross national income per capita (male)': "gross_income_pc_m",
    'human development index (female)': "hdi_f"
}, inplace=True)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-02-21
    • 1970-01-01
    • 2015-02-05
    • 2013-05-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多