Itertools product ValueError: too many values to unpack (expected 2)
Itertools product ValueError: too many values to unpack (expected 2)
我知道这个问题被重复了很多次,但我还没有看到处理这个特定问题的问题。我得到了这个函数,它取一个 numpy 数组的长度,然后取叉积:
def solve(tab, vacios):
if vacios == 0:
return is_valid( tab ) #Not important here
large = len(tab)
for fila, col in product(range(large), repeat=(large-1)):
但是我得到这个错误:
ValueError: too many values to unpack (expected 2)
我真的不知道该怎么做,所以如果你能帮助我,那就太好了,谢谢!
您每次迭代都会生成 large-1
个值的组合,因为您将 repeat
参数设置为:
product(range(large), repeat=(large-1):
但将值解包为两个变量,fila
和 col
:
for fila, col in product(range(large), repeat=(large-1)):
如果您传入一个长度不是 3 的 tab
值,那么要解包的值的数量总是错误的。
例如,如果 len(tab)
为 4,则生成长度为 3 的元组:
>>> from itertools import product
>>> large = 4
>>> list(product(range(large), repeat=large - 1))
[(0, 0, 0), (0, 0, 1), (0, 0, 2), (0, 0, 3), (0, 1, 0), (0, 1, 1), (0, 1, 2), (0, 1, 3), (0, 2, 0), (0, 2, 1), (0, 2, 2), (0, 2, 3), (0, 3, 0), (0, 3, 1), (0, 3, 2), (0, 3, 3), (1, 0, 0), (1, 0, 1), (1, 0, 2), (1, 0, 3), (1, 1, 0), (1, 1, 1), (1, 1, 2), (1, 1, 3), (1, 2, 0), (1, 2, 1), (1, 2, 2), (1, 2, 3), (1, 3, 0), (1, 3, 1), (1, 3, 2), (1, 3, 3), (2, 0, 0), (2, 0, 1), (2, 0, 2), (2, 0, 3), (2, 1, 0), (2, 1, 1), (2, 1, 2), (2, 1, 3), (2, 2, 0), (2, 2, 1), (2, 2, 2), (2, 2, 3), (2, 3, 0), (2, 3, 1), (2, 3, 2), (2, 3, 3), (3, 0, 0), (3, 0, 1), (3, 0, 2), (3, 0, 3), (3, 1, 0), (3, 1, 1), (3, 1, 2), (3, 1, 3), (3, 2, 0), (3, 2, 1), (3, 2, 2), (3, 2, 3), (3, 3, 0), (3, 3, 1), (3, 3, 2), (3, 3, 3)]
要么将 repeat
硬编码为 2
,要么不要尝试将可变数量的值解压缩为固定数量的变量。
我知道这个问题被重复了很多次,但我还没有看到处理这个特定问题的问题。我得到了这个函数,它取一个 numpy 数组的长度,然后取叉积:
def solve(tab, vacios):
if vacios == 0:
return is_valid( tab ) #Not important here
large = len(tab)
for fila, col in product(range(large), repeat=(large-1)):
但是我得到这个错误:
ValueError: too many values to unpack (expected 2)
我真的不知道该怎么做,所以如果你能帮助我,那就太好了,谢谢!
您每次迭代都会生成 large-1
个值的组合,因为您将 repeat
参数设置为:
product(range(large), repeat=(large-1):
但将值解包为两个变量,fila
和 col
:
for fila, col in product(range(large), repeat=(large-1)):
如果您传入一个长度不是 3 的 tab
值,那么要解包的值的数量总是错误的。
例如,如果 len(tab)
为 4,则生成长度为 3 的元组:
>>> from itertools import product
>>> large = 4
>>> list(product(range(large), repeat=large - 1))
[(0, 0, 0), (0, 0, 1), (0, 0, 2), (0, 0, 3), (0, 1, 0), (0, 1, 1), (0, 1, 2), (0, 1, 3), (0, 2, 0), (0, 2, 1), (0, 2, 2), (0, 2, 3), (0, 3, 0), (0, 3, 1), (0, 3, 2), (0, 3, 3), (1, 0, 0), (1, 0, 1), (1, 0, 2), (1, 0, 3), (1, 1, 0), (1, 1, 1), (1, 1, 2), (1, 1, 3), (1, 2, 0), (1, 2, 1), (1, 2, 2), (1, 2, 3), (1, 3, 0), (1, 3, 1), (1, 3, 2), (1, 3, 3), (2, 0, 0), (2, 0, 1), (2, 0, 2), (2, 0, 3), (2, 1, 0), (2, 1, 1), (2, 1, 2), (2, 1, 3), (2, 2, 0), (2, 2, 1), (2, 2, 2), (2, 2, 3), (2, 3, 0), (2, 3, 1), (2, 3, 2), (2, 3, 3), (3, 0, 0), (3, 0, 1), (3, 0, 2), (3, 0, 3), (3, 1, 0), (3, 1, 1), (3, 1, 2), (3, 1, 3), (3, 2, 0), (3, 2, 1), (3, 2, 2), (3, 2, 3), (3, 3, 0), (3, 3, 1), (3, 3, 2), (3, 3, 3)]
要么将 repeat
硬编码为 2
,要么不要尝试将可变数量的值解压缩为固定数量的变量。