【发布时间】:2021-04-01 14:03:14
【问题描述】:
我目前遇到了这个错误,我找不到问题。一开始我以为是因为transformed_data[2]里面只有1个,结果好像不是这样的。
ValueError: 字典更新序列元素#0 的长度为 1; 2 是必需的
我的代码:
print(transformed_data)
country =transformed_data[0]
continent = transformed_data[1]
transformed_data[2] = transformed_data[2].replace("''",'test')
print(dict(transformed_data[2])) # when I add dict it does the error.
print((transformed_data[2])) # without dict, no error.
没有字典的shell:
['Qatar', 'ASIA', '{2001: 41.215}', '{2001: 615000}']
{2001: 41.215}
['Cameroon', 'AFRICA', '{2001: 3.324}', '{2001: 16358000}']
{2001: 3.324}
['Democratic Republic of Congo', 'AFRICA', '{2006: 1.553}', '{2006: 56578000}']
{2006: 1.553}
['Bulgaria', 'EUROPE', '{2001: 48.923}', '{2001: 7931000}']
{2001: 48.923}
['Poland', 'EUROPE', '{2001: 306.696}', '{2001: 38489000}']
{2001: 306.696}
['Lesotho', 'AFRICA', "{1975: ''}", "{1975: '1161000'}"]
{1975: test}
['Albania', 'EUROPE', '{2002: 3.748}', '{2002: 3126000}']
{2002: 3.748}
['Colombia', 'SOUTH AMERICA', '{2001: 56.259}', '{2001: 39630000}']
{2001: 56.259}
['Pakistan', 'ASIA', '{2012: 166.33}', '{2012: 187280000}']
{2012: 166.33}
带字典的外壳:
Exception raised:
Traceback (most recent call last):
File "/Applications/Thonny.app/Contents/Frameworks/Python.framework/Versions/3.7/lib/python3.7/doctest.py", line 1330, in __run
compileflags, 1), test.globs)
File "<doctest __main__.get_bar_co2_pc_by_continent[1]>", line 1, in <module>
get_bar_co2_pc_by_continent(d1, 2001)
File "/Users/bob/Desktop/python class/assignment/final/plot_data.py", line 33, in get_bar_co2_pc_by_continent
print(dict(transformed_data[2]))
ValueError: dictionary update sequence element #0 has length 1; 2 is required
【问题讨论】:
-
嗨@mkrieger1,谢谢你的建议,但我不允许使用任何模块,所以这不适用于我的情况
-
也许解决方案是
transformed_data[2]首先不应该是一个字符串。它来自哪里? -
@mkrieger1 是教授要求的。这是代码中的另一个函数,它将所有内容都放入字符串中,然后在这段代码中,我必须从该字符串中获取值
-
@mkrieger1 我已经浏览了您链接的整个线程,他们只是拆分并替换它,这就是我最终要做的。我想从心底里感谢你。最后,我可以继续努力完成这个项目。圣诞快乐!
标签: python dictionary