简体   繁体   English

在嵌套 enumerate() 的 for 循环中添加第二个参数有什么作用?

[英]What does adding a second parameter in a for loop with nested enumerate() do?

I'm trying myself on Python again and I want to create a text-based Ticktacktoe.我再次尝试使用 Python,我想创建一个基于文本的 Ticktacktoe。

Currently, I am working on the layout with 0 representing the empty spaces in the games and numbers up top and on the left for coordinates.This is what I have so far:目前,我正在处理布局,其中 0 代表游戏中的空白空间,顶部和左侧的数字代表坐标。这就是我目前所拥有的:

game = [[0, 0, 0],
        [0, 0, 0],
        [0, 0, 0],]

print('   0  1  2')

for row in enumerate(game):
    print(row)

which outputs this :输出这个:

     0  1  2
(0, [0, 0, 0])
(1, [0, 0, 0])
(2, [0, 0, 0])

The problem is that I want it to look like this:问题是我希望它看起来像这样:

   0  1  2
0 [0, 0, 0]
1 [0, 0, 0]
2 [0, 0, 0]

Now I found a way by adding 'count' into the for loop.现在我找到了一种方法,将“计数”添加到 for 循环中。 But I don't understand why at all.但我完全不明白为什么。 Looking up the Python documentation did not help.查找 Python 文档没有帮助。 Why is this happening?为什么会这样? Why does that get rid of the brackets and commas?为什么去掉括号和逗号?

game = [[0, 0, 0],
        [0, 0, 0],
        [0, 0, 0],]

print('   0  1  2')

for count, row in enumerate(game):
    print(count, row)

Edit: I think I understand now.Since enumarate() returns the Index and the value all that's basically happening is that I assigned count to the Index and row to basically the value?编辑:我想我现在明白了。自从 enumerate enumarate()返回索引和值后,基本上发生的所有事情就是我将count分配给索引并将行分配给基本上值? And that is why there are now brackets since print prints more than 1 variable using a space?这就是为什么现在有括号的原因,因为 print 使用空格打印了 1 个以上的变量? Could you confirm this?你能确认一下吗?

for row in enumerate(game):

This will print row , which is a tuple.这将打印row ,它是一个元组。 Tuples are printed in the format (item1, item2, ...)元组以 (item1, item2, ...) 格式打印

Example:例子:

>>> row = (5, [0,0,0])
>>> print(row)
(5, [0,0,0]) 

for count, row in enumerate(game): Here, the result is unpacked. for count, row in enumerate(game):这里,结果被解包。 What was a tuple is now split into count and row .元组现在被拆分为countrow print can take a variable number of arguments, and by default it separates them with a space. print可以接受可变数量的参数,默认情况下用空格分隔它们。

Example:例子:

>>> count = 5
>>> row = [0,0,0]
>>> print(count, row)
5 [0,0,0]

Nested Lists嵌套列表

A nested list is a list that contains other lists.嵌套列表是包含其他列表的列表。 A simple example is一个简单的例子是

nested = [[0,1,2], [3,4,5]]

You can unpack a nested list (or any list at all)您可以解压缩嵌套列表(或任何列表)

list012, list345 = nested

That's what for count, row in enumerate(game): is doing.这就是for count, row in enumerate(game):正在做的事情。

enumerate(game) returns a tuple: a counter and the normal object. enumerate(game)返回一个元组:一个计数器和普通对象。 We can compare it like this:我们可以这样比较:

my_list = [10,20,30]
for i in my_list:
    print i    # prints 10 20 30
for i in enumerate(my_list):
    print i    # prints (0,10) (1,20) (2,30)

In the first example a tuple will be passed to print() , which is printed enclosed in parenthesis.在第一个示例中,一个元组将传递给print() ,它被打印在括号中。 In the second answer the row count and the row itself are passed as separate arguments to print() - print() will then print the arguments space separated.在第二个答案中,行数和行本身作为单独的参数传递给print() - 然后 print print()将打印分隔空间的参数。

enumerate(items) returns a list of tuples of a number and your item, where the number increases by one for each new item in the list (counting). enumerate(items)返回一个数字和您的项目的元组列表,其中列表中的每个新项目的数字增加一(计数)。 In this case enumerate returns the following tuples:在这种情况下,枚举返回以下元组:

{0, [0, 0, 0]}, {1, [0, 0, 0]}, {2, [0, 0, 0]}

By using a second variable name in the for loop you are destructuring the tuple into count and row .通过在 for 循环中使用第二个变量名称,您将元组解构为countrow Whereas before you were only giving 1 variable name, so the variable was assigned the entire tuple.而在此之前,您只给出了 1 个变量名,因此该变量被分配了整个元组。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM