简体   繁体   English

错误:ValueError:解包的值太多(预期为 3)

[英]Error: ValueError: too many values to unpack (expected 3)

Getting the below error while iterating tuples.迭代元组时出现以下错误。 I am not sure what changes do I need to apply to iterate.我不确定我需要应用哪些更改来迭代。 Any help would be appreciated.任何帮助,将不胜感激。

ValueError: too many values to unpack (expected 3) ValueError:解包的值太多(预期为 3)

Program:-程序:-

 def convert_tuple_to_dict(self, tup):

        dt = defaultdict(list)
        temp_lst = []

        for i in range(len(tup)):

            if (len(tup[i]) == 2):
                for a, b in tup:
                    dt[a].append(b)

            if (len(tup[i]) == 3):
                print(tup[i])
                for (a, b, c) in tup[i]:
                    dt[a].append(b)
                    dt[a].append(c)

        return dict(dt)

    run = DataType()
    print(run.convert_tuple_to_dict(
        (('1328', '50434022', '53327'), (777, '5000435011607', '00720645'))))

Traceback details:-追溯细节:-

Traceback (most recent call last):
  File "foo/DataType.py", line 95, in <module>
    print(run.convert_tuple_to_dict(
  File "foo/DataType.py", line 86, in convert_tuple_to_dict
    for (a, b, c) in tup[i]:
ValueError: too many values to unpack (expected 3)
('1328', '50434022', '53327')

Expected Output:预期输出:

{'1328': ['50434022', '53327'], 777: ['5000435011607', '00720645']}
 if (len(tup[i]) == 3): print(tup[i]) for (a, b, c) in tup[i]:

Here you're checking the length of the tup[i] then iterating on it and trying to further unpack each item.在这里,您正在检查 tup[i] 的长度,然后对其进行迭代并尝试进一步解包每个项目。

So given tup[i] = ('1328', '50434022', '53327') you're going to do:所以给定tup[i] = ('1328', '50434022', '53327')你要做:

a, b, c = '1328'
a, b, c = '50434022'
a, b, c = '53327'

which is unlikely to be what you're trying to do.这不太可能是你想要做的。 The solution is to not iterate the tuple, just unpack-assign...解决方案是不迭代元组,只需解包分配...

a, b, c = tup[i]
# do the thing

You have the same mistake in the 2-tuple case incidentally.顺便说一句,您在 2 元组情况下也犯了同样的错误。

There's a few other debatable bits in your snippet:您的代码段中还有一些其他值得商榷的地方:

  • tup is not a tuple at all, it's a sequence of inputs, so the naming is misleading tup是不是在所有元组,它的输入序列,所以命名为误导
  • there's no point at which you need the index, so you've no reason to iterate on range(len(...)), just iterate the thing directly没有一点需要索引,所以你没有理由在 range(len(...)) 上迭代,直接迭代这个东西
  • you could use extended unpacking to not care about the length of the input tuples at all:您可以使用扩展解包完全不关心输入元组的长度:
def convert_tuple_to_dict(self, in_tuples):
    dt = defaultdict(list)
    for key, *values in in_tuples:
        dt[key].extend(values)
    return dict(dt)

The unpacking shouldn't be in a loop解包不应该是一个循环

if len(tup[i]) == 3:
    a, b, c = tup[i]
    dt[a].append(b)
    dt[a].append(c)

for x in tup[i] already unpack the tuple, which means you are trying to assign one value to 3 variables for x in tup[i]已经解包了元组,这意味着您正在尝试将一个值分配给 3 个变量

a, b, c = `1328`

You also don't need all the checks, use slice to append all the values您也不需要所有检查,使用 slice 附加所有值

def convert_tuple_to_dict(self, tup):

    dt = defaultdict(list)

    for i in range(len(tup)):
        dt[tup[i][0]].extend(tup[i][1:])

    return dict(dt)

If your tuple is of the format [(x1,y1,z1),(x2,y2,z2),(x3,y3,z3), ... ,(xn,yn,zn)] You can go something like this:如果您的元组的格式为 [(x1,y1,z1),(x2,y2,z2),(x3,y3,z3), ... ,(xn,yn,zn)] 您可以这样做:

for x,y,z in my_tuple:
        '''
        Rest of the code goes here -- it can loop over each element of the list of tuples
        And unpack each element of the tuple in the form of x,y,z
        '''

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ValueError:太多的值解包(预期2) - ValueError: too many values too unpack (expected 2) ValueError 通用 WSGI 请求错误太多值无法解包(预期 2) - ValueError generic WSGI request error too many values to unpack (expected 2) 如何传递此错误“ValueError:在 Python 中解压的值太多(预期为 2)? - How Pass This Error " ValueError: too many values to unpack (expected 2) in Python? 我想解决错误“ValueError: too many values to unpack (expected 2)” - I want to resolve the error “ValueError: too many values to unpack (expected 2)” OpenAIGPTModel PyTorch 错误 - ValueError: too many values to unpack (expected 2) - OpenAIGPTModel PyTorch Error- ValueError: too many values to unpack (expected 2) 收到错误:ValueError:要解包的值太多(预期为 2) - Getting an error: ValueError: too many values to unpack (expected 2) ValueError:更改值时解包的值太多(预期为 2)错误 - ValueError: too many values to unpack (expected 2) error when changing a value 出现错误:ValueError:要解包的值太多(预期为 5) - Getting error: ValueError: too many values to unpack (expected 5) pysnmp-ValueError:太多值无法解包(预期4) - pysnmp - ValueError: too many values to unpack (expected 4) python:ValueError:太多值无法解包(预期2) - python: ValueError: too many values to unpack (expected 2)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM