gpt4 book ai didi

python - 为什么循环嵌套的顺序很重要python?

转载 作者:太空宇宙 更新时间:2023-11-03 13:37:58 27 4
gpt4 key购买 nike

我正在努力读完这本书:用 Python 自动化无聊的事情。我在第 6 章的练习项目中:桌面打印机。( https://automatetheboringstuff.com/chapter6/ ) - cmmd + F "table printer"

我设法找到了答案,但我发现我对嵌套循环的理解非常薄弱。为什么嵌套循环的顺序很重要?它如何影响它打印数据的方式?

这是数据:

tableData = [['apples', 'oranges', 'cherries', 'banana'],
['Alice', 'Bob', 'Carol', 'David'],
['dogs', 'cats', 'moose', 'goose']]

这是第一个代码:

for a in range(len(tableData[0])):
for b in range(len(tableData)):
print(tableData[b][a],end = "")
print()

它打印:

  apples   Alice    dogs
oranges Bob cats
cherries Carol moose
banana David goose

这是第二个代码(我改变了 a 和 b 的顺序嵌套,但没有改变我想要打印的内容):

for b in range(len(tableData)):
for a in range(len(tableData[0])):
print(tableData[b][a],end = "")
print()

它打印出这个:

apples orangescherries  banana
Alice Bob Carol David
dogs cats moose goose

这是第三段代码(我没有改变嵌套的顺序,只是改变了我要打印的内容):

for a in range(len(tableData[0])):
for b in range(len(tableData)):
print(tableData[a][b],end = "")
print()

它打印这个:

  apples orangescherries
Alice Bob Carol
dogs cats moose
Traceback (most recent call last):
File "<pyshell#12>", line 1, in <module>
printTable()
File "<pyshell#11>", line 10, in printTable
print(tableData[x][j].rjust(colWidths[0]),end = "")
IndexError: list index out of range

我难以理解的是:

(1) 对于第一个和第二个代码 - 为什么嵌套顺序会影响打印的内容?

(2) 对于第一个和第三个代码 - 为什么 print[a][b] 的顺序很重要?

很抱歉,这是一个非常长的问题,但我遇到了一些麻烦,请帮助我。

最佳答案

您在 Python 中将表创建为包含 listlist

table = [ ["row 0, column 0", "row 0, column 1", "row 0, column 2"],
["row 1, column 0", "row 1, column 1", "row 1, column 2"],
["row 2, column 0", "row 2, column 1", "row 2, column 2"],
["row 3, column 0", "row 3, column 1", "row 3, column 2"] ]

这就是您从这样的“多维”列表中访问项目的方式:

row = table[1]  # get row 1 (2nd item of the outer list)
item = row[3] # from row 1, get column 3 (4th item of the inner list)

或者简而言之:

item = table[1][3]  # get item 1 of the outer list, and from that inner list get item 3

因此这解释了为什么从“多维”列表访问项目时参数的顺序很重要,第一个索引选择行,第二个索引选择列(如果我们将列表的列表想象成如上所示的表格)。


现在是嵌套循环:

for row_number in range(len(table)):  # iterate over the row indices

for column_number in range(len(table[row_number])): # iterate over the column indices
# of the current row

print("Row:", row_number, "- Column:", column_number)
print(" --> Item:", table[row_number][column_number])

# <-- jump back to the column loop head here, if elements remaining

# <-- jump back to the row loop head here, if elements remaining

因此外循环(行)计数较慢,而内循环(列)计数较快,对于外循环的每个迭代步骤,内循环对其所有元素执行完整的迭代。


但是,您不应该在 Python 中迭代列表索引,但您可以直接迭代列表项。这样也变得更加清晰:

for row in table:  # iterate over the rows (items of the outer list)

for column in row: # iterate over the columns of the current row (items of inner list)

print("Row:", row_number, "- Column:", column_number)
print(" --> Item:", table[row_number][column_number])

# <-- jump back to the column loop head here, if elements remaining

# <-- jump back to the row loop head here, if elements remaining

关于python - 为什么循环嵌套的顺序很重要python?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36760498/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com