-3

All right, so I have a code where I need to print a game board with 5x5 squares. All the squares is in squareList, which is a list (oh you don't say). A square is based on an object with a variable number that is the value I want to print. How can I do this?

EDIT: The reason to why I want to do this is because I want to start on a new line every five squares so that I get a board, not a line, with values.

PhP
  • 82
  • 7
  • 1
    `for fifth in squareList[4::5]: .. do something with fifth ..` – falsetru Aug 27 '14 at 15:37
  • or `for fifth in itertools.islice(squareList, 4, None, 5): ...` – falsetru Aug 27 '14 at 15:38
  • 4
    I don't see the relation between the question title and its body. – interjay Aug 27 '14 at 15:38
  • @falsetru Comments are not for answers. Use an answer for, well, answering. – MatsLindh Aug 27 '14 at 15:40
  • 1
    @MatsLindh, we need to know the actual question before we can answer, the title and the body have very little in common – Padraic Cunningham Aug 27 '14 at 15:42
  • I voted to put this on hold because the title and body are very different, and because the body of the question is not at all clear. I don't think it's fair to expect people to guess at possible meanings. – Weeble Aug 27 '14 at 15:43
  • Better now? Sorry for not explaining my reasons, thus not explaining the relation between title and question. – PhP Aug 27 '14 at 15:44
  • Okay, it's not a great question, but I think it's clear what you mean now, so I retracted my close vote. – Weeble Aug 27 '14 at 15:45
  • @PadraicCunningham Still, my response was to answers to the question as comments, not to the comment asking to clarify the question. Regarding understanding the question, while the language might be clumsy and require a bit of extra parsing, there is no direct contradiction between the title and the text (_game board_ vs _data structure_). – MatsLindh Aug 27 '14 at 15:51

3 Answers3

2

The python slice / array operator supports an optional step as the third value. squareList[start:end:step]:

for o in squareList[::5]:
    print(o.number)

Use 5 as the step value to get every fifth entry in the list.

MatsLindh
  • 49,529
  • 4
  • 53
  • 84
0

Why not make list for each row in the square, and then put those rows into a list?

squareList = [rowList1, rowList2, rowList3, rowlist4, rowList5]

This way you can manipulate a column as you loop through the rows.

for row in SquareList:
  doSomething(row[4])

You can also extract a column using a list comprehension.

colList1 = [row[0] for row in squareList]
CCKx
  • 1,303
  • 10
  • 22
0

I would agree that you might want to consider other more convenient structures for your data, as suggested by CCKx.

Here are two approaches

Assuming:

squareList = [0,0,1,0,0,
              1,2,0,0,1,
              0,0,0,1,2,
              2,2,0,0,0,
              1,0,0,0,1]

Then you can do this:

for index, value in enumerate(squareList):
    print(value, end='')
    if (index % 5) == 4:
        print()

(I'm assuming Python 3.)

That will literally do what you asked for in the way that you asked for it. index will count up through each element of your list, and the % or "modulo" operator will get you the remainder when you divide by 5, allowing you to take some action every 5 times round the loop.

Or you can use slicing:

for row in range(5):
    for cell in squareList[row*5:row*5+5]:
        print(cell, end='')
    print()

See also:

What is the most "pythonic" way to iterate over a list in chunks?

Community
  • 1
  • 1
Weeble
  • 17,058
  • 3
  • 60
  • 75