开发者

Help With Printing a single item from list inside list in python

I first create my grid:

grid = []
for x in range(1,collength + 1):
    for y in range(1,collength + 1):
        grid.append([x,y,'e'])

Then I make que for my grid and I want to manipulate the que based on the 0, 1, and 2 position of the lists inside the lists:

floodfillque = []

grid = floodfillque

for each in f开发者_开发技巧loodfillque:
    floodfilllist = []
    currentfloodfill = []
    print '::'
    print each[1]

But when I try to print each[1] I get the whole list, not just the nth element of the list inside the list

What am I doing wrong?


As you have written it, your code iterates over an empty list. I think you mean:

for each in grid:

or perhaps:

floodfillque = grid

This code works fine:

collength = 3

grid = []
for x in range(1,collength + 1):
    for y in range(1,collength + 1):
        grid.append([x,y,'e'])

for each in grid:
    floodfilllist = []
    currentfloodfill = []
    print '::'
    print each[1]

Result:

::
1
::
2
::
3
::
1
::
2
::
3
::
1
::
2
::
3
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜