我收到以下错误,需要超过1个值才能解包

阿波娃·索马尼(Apoorva Somani)

我在python中具有以下结构。

data_set = {'1':[('Worktype', 'Consultancy'), ('Age', 30), ('Qualification', 'Ph.D'), ('Age', 9)], \
            '2':[('Worktype', 'Service'), ('Age', 21), ('Qualification', 'M.Tech'), ('Age', 1)], \
            '3':[('Worktype', 'Research'), ('Age', 26), ('Qualification', 'M.Tech'), ('Age', 2)], \
           }

我正在使用以下循环访问元素。我的目的是打印与“工作类型”相关的值。

for d,c in data_set:
    print(c['Worktype']) 

但是,我收到以下错误-

for d,c in data_set:
ValueError: need more than 1 value to unpack

正确的方法是什么?

阿南德·库玛(Anand S Kumar)

当您直接遍历字典时,它仅遍历其keys。要同时遍历值,您也应该遍历.items(),即使内部元素不是列表,也是如此dict,因此您不能直接访问- c['Worktype']

似乎内部元素是要用作字典的,如果是这样,则应首先使用dict()内置函数将其转换为字典例子 -

data_set = {k:dict(v) for k,v in data_set.items()}
for d,c in data_set.items():
    print(c['Worktype'])

演示-

>>> data_set = {'1':[('Worktype', 'Consultancy'), ('Age', 30), ('Qualification', 'Ph.D'), ('Age', 9)], \
...             '2':[('Worktype', 'Service'), ('Age', 21), ('Qualification', 'M.Tech'), ('Age', 1)], \
...             '3':[('Worktype', 'Research'), ('Age', 26), ('Qualification', 'M.Tech'), ('Age', 2)], \
...            }
>>>
>>> data_set = {k:dict(v) for k,v in data_set.items()}
>>>
>>> for d,c in data_set.items():
...     print(c['Worktype'])
...
Service
Consultancy
Research

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

Python错误:“ ValueError:需要多个值才能解压缩”

Django测试异常:“需要多个值才能解压”

Python pip安装错误:ValueError:需要两个以上的值才能解压

我不断收到“ docker build”,需要确切的1个参数错误

“ TypeError:需要一个类似字节的对象,而不是'str'”。我怎样才能解决这个问题?

Django模板:需要2个值才能解开for循环;拿到8

错误返回“需要超过1个值才能解压缩”

ValueError:需要超过0个值才能解包-手套

遇到“ gladLoadGL”问题。我收到一个错误消息,说它不需要1个参数

PYTHON:需要超过0个值才能解压缩(比较2个文件)

需要超过0个值才能解压缩(使用tkinter的python会在变量错误时进行协调)

MATLAB程序需要花费超过1个小时的时间才能执行

如何访问python字典中的键,值?(错误:“需要超过1个值才能解包”)

ValueError:需要超过1个值才能解包

“需要多个0值才能解压缩”错误消息

ValueError:需要超过0个值才能解压(python列表)

我不断收到DNS_PROBE_FINISHED_BAD_CONFIG错误。我怎样才能解决这个问题?

ValueError:需要超过2个值才能解压-阅读GREYSCALE时

ValueError:需要两个以上的值才能解压

split-ValueError:需要多个值才能解压

Pulp (Python) 值错误:需要 1 个以上的值才能解包

django 值错误需要多个值才能解压

如何解决 ValueError:需要 1 个以上的值才能解包?

自定义管理表单错误“需要 1 个以上的值才能解压缩”

ValueError:需要 1 个以上的值才能使用 matplotlib 解包

在 Snowsql 中,当我尝试“放置”一个文件时,我收到以下错误消息:

Google App Engine fetch() ValueError:需要 1 个以上的值才能解包

Python 给我一个 Value 错误太多的值无法解包(预期 2)

尽管使用了不同元组的联合,但 Mypy 错误“需要超过 2 个值来解包(预期 3 个)”