1

这是一段有点乱码的代码摘录。

上下文是它试图遍历一个列表,构建,它应该看起来像 [100, 92, 87, etc]。它希望遍历建筑物的每一层,然后将该人(通过减少当前楼层)移动到下一个可用楼梯。

我的问题是函数内的嵌套列表和 if 语句。这是一个三重打击,我无法理解正确的语法:

  • 调用列表列表的特定部分 - 即 list[i][j]

  • 在循环中使用 if 语句

  • 嵌套循环

  • 在其他循环中使用循环变量

这是我的代码:

def Evacuate(building):
    while sum(building) > 0:
        for i in building:
            if building[i] > 0:
                for j in range(STAIRCASE):
                    if staircasenetwork[j[i-1]] < CAPACITY:
                        building[i] -= 1
                        staircase[i-1] += [TRAVELTIME]

编辑:

我弄清楚了这个问题。Building 的输入是一个列表。Staircasenetwork 也是一个列表。列表看起来像这样

building = [100,90,101]
staircasenetwork = [[0,0,0],[0,0,0]]

这代表了一座 3 层楼的建筑,里面挤满了人和两个空楼梯。

我做错的是我试图写一些类似的东西:

for i in building:
    #I'm skipping or simplifying some specific conditionals for the problem here
    for j in staircasenetwork:
        building[i] -= 1
        staircasenetwork[j][i] += 1

应该采取 (i) 在建筑中(我预计是 [0, 1, 2])和 (j) 在楼梯网络中(我预计是 [0, 1])并使用这两个坐标遍历我写的列表。

我忘记的是python通过直接将这些值分配给(i)来遍历列表。

所以,如果我有一个清单:

[100, 90, 101]

我写道:

for i in list:
    print i

它将打印:

100
90
101

不是:

0
1
2

所以解决我的问题是使用 range(len(building) 而不是 building.

该语句需要构建,并首先使用 len() 将其转换为等于构建列表长度的整数。然后它获取该整数并使用 range() 将其转换为从 0 到 X 的数字列表。

本质上: [100, 90, 101] >> 3 >> [0, 1, 2]

building = [100, 90, 101]
for i in range(len(building)):
    print str(range(len(building))) + " " str(building)

将打印:

0 100
1 90
2 101

所以在我的第一次尝试中,当我使用:

for i in building:
    #to call on
    building[i]

它做的第一件事是查看建筑物列表中的第一个值,即 100。然后它使用该数字查找建筑物列表中的第 100 个数字,该数字不存在,因为该列表只有 3 个值长。

它试图这样做:

building = [100, 90, 101]
for i in building
>>> first val of building = 100
    building[100]
    >>> IndexError: list index out of range
    >>> Is looking for the 100th number in a 3-number long list

有多种使用语句的方法,例如:

for i, e in enumerate(building):

要创建可能如下所示的元组列表:

[(0, 100), (1, 90), (2, 101)]

但我更喜欢使用 range(len(building)) 方法。这种方法可能无法达到很好的速度基准,但它成功地教会了我一些关于代码如何工作的知识,并且解决了我的小问题。

固定代码将显示为:

for i in range(len(building)):
    for j in range(len(staircasenetwork)):
        building[i] -= 1
        staircasenetwork[j][i] += 1

现在 (i) 和 (j) 将是 (via range(len())) 的坐标,而不是变量的直接反映。

Lego Stormtrooper 还对我的代码的其他问题做出了很好的回应。

4

1 回答 1

1

Calling on specific parts of lists of lists - i.e. list[i][j]

You've got this correct in you question, but not in your code. Consider:

x = [ 1 , [2,3] ]

When we call x[0] we get an object, in this case the integer 1. Not much else to do. But when we call x[1] we get a list ([2,3]) and then we can operate on that, like so x[1][1] which would give us 3.

In your code, break it down like this:

staircasenetwork[j[i-1]]

Is equivilent to:

x = j[i-1]
staircasenetwork[x]

So unless x is a valid index, it will fail.

Using if statements in loops

Use these like you would anywhere else:

for i in range(10):
    if i%2 == 1:
        print i

Will only print odd numbers below 10. Not much to it.

Nesting loops

Again, these operate like anywhere else. But you aren't accessing your items correct. In Python for x in some_list iterates through the items in the list, not the indexes.

So you want something like:

    for floor in building:
        if floor > 0:

Using loop variables in other loops

Variables have scope in their block, so:

for i in range(3):
    x = i*2
    # i and x in scope
    for j in range(x):
        # i, j, x and y are all in scope
        y = i+j+x
        print y
    # The next line will fail because ONLY y isn't in scope.
    # j is in scope, but is set to the final value of j for the above loop.
    print i+j+x+y

Lastly, miscellaneous issue.

sum isn't magic. It won't do what you think there, as sum takes an iterable (list) of integers. You are passing an iterable of iterables.

Some of your variables are unset - specifically STAIRCASE, CAPACITY and TRAVELTIME

于 2014-07-04T03:15:13.740 回答