Python 'ValueError: 没有足够的值来解包(预期为 2,得到 0)'

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/40706847/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 23:52:56  来源:igfitidea点击:

'ValueError: not enough values to unpack (expected 2, got 0)'

python

提问by Link

I am attempting to create list of lists using for loops in python. My plan is to use two separate loops; one loop to create big lists to put the small lists in, and another loop to create the small lists and append them to the big lists.

我正在尝试在 python 中使用 for 循环创建列表列表。我的计划是使用两个独立的循环;一个循环创建大列表以放入小列表,另一个循环创建小列表并将它们附加到大列表。

This is being used to make a 'battleship' type game where there is a 10x10 grid.

这被用来制作一个“战舰”类型的游戏,其中有一个 10x10 的网格。

This is the chunk of code I am having difficult with:

这是我遇到困难的代码块:

for i in range(0,10):
    (newlist,(i))=[]

The only task this specific code is meant to complete is to create 10 new lists, each with a different name. For example, the first circulation of the loop would create list0, the second circulation list1and so on up to list9.

这个特定代码要完成的唯一任务是创建 10 个新列表,每个列表都有不同的名称。例如,循环的第一个循环将创建list0,第二个循环list1等等直到list9

Theoretically, I see nothing wrong with this code. It also does work when instead of creating a new list, you are putting a string into the new variable.

从理论上讲,我认为这段代码没有任何问题。当您将一个字符串放入新变量而不是创建新列表时,它也可以工作。

Whenever I run the program, I get the error

每当我运行程序时,我都会收到错误消息

ValueError: not enough values to unpack (expected 2, got 0)

I have no idea why this occurs, and would be very grateful if anyone here could help me out.

我不知道为什么会发生这种情况,如果有人能帮助我,我将不胜感激。

采纳答案by ForceBru

You seem to have a variable called list, which you want to populate with some lists. Your code doesn't work as this syntax is used to extract two pieces of data from the utterable on the right hand side. Clearly, []is empty, so you can extract nothingout of it.

您似乎有一个名为 的变量list,您想用一些列表填充它。您的代码不起作用,因为此语法用于从右侧的发音中提取两条数据。显然,[]是空的,所以你不能从中提取任何东西

You could do it like this:

你可以这样做:

your_list = [list() for x in range(9)] 

Note that you shouldn't call the variable listas there exists a built-in function with the same name that constructs an empty list. Right now the variable makes the built-in unaccessible.

请注意,您不应调用该变量list,因为存在构建空列表的同名内置函数。现在该变量使内置的无法访问。

Edit:If you need to have 10 lists of lists:

编辑:如果您需要有 10 个列表列表:

your_list = [[[] for x in range(9)] for y in range(10)]

Then, your_listwill be a list containing 10 lists of lists.

然后,your_list将是一个包含 10 个列表的列表。

回答by furas

Better create list with sublists because it is easier to use later,

最好用子列表创建列表,因为以后更容易使用,

all_lists = []
for i in range(10):
    all_lists.append([])

or shorter

或更短

all_lists = [ [] for i in range(10) ]

And now you can access lists using index, ie.

现在您可以使用索引访问列表,即。

all_lists[0]


Eventually create dictionary - then you can use number or string as index

最终创建字典 - 然后您可以使用数字或字符串作为索引

all_lists = {}
for i in range(10):
    all_lists[i] = []  # first version
    # or 
    all_lists[str(i)] = [] # second version

or shorter

或更短

all_lists = { i:[] for i in range(10) } # first version
all_lists = { str(i):[] for i in range(10) } # first version

And now you can access lists using number or string, ie.

现在您可以使用数字或字符串访问列表,即。

all_lists[0]   # first version
# or 
all_lists["0"] # second version


EDIT:to create 10x10 grid you can use

编辑:要创建 10x10 网格,您可以使用

grid = [ [None]*10 for _ in range(10) ]

You can use different value instead of Noneas default.

您可以使用不同的值而不是None默认值。

And to get position (x,y)you can use

并获得(x,y)您可以使用的位置

grid[y][x]

First is y(row), second is x(column)

第一个是y(行),第二个是x(列)