0

Trying to make a simple map for my player and the map uses multiple rows but is there a way i can store all of these rows to 1 variable? so i can print them all at once at also i can call upon them like print(row[2][4])

row1 = [" -", " -", " -", " -", " -", " -", " -"]
row2 = [" -", " -", " -", " -", " -", " -", " -"]
row3 = [" -", " -", " -", " -", " -", " -", " -"]
row4 = [" -", " -", " -", " -", " -", " -", " -"]
row5 = [" -", " -", " -", " -", " -", " -", " -"]
row6 = [" -", " -", " -", " -", " -", " -", " -"]
row7 = [" -", " -", " -", " -", " -", " -", " -"]

i understand i could condense this to a function and print it at once but i want to call upon it as stated above

Jake
  • 3
  • 2
  • [How to define a two-dimensional array in Python](//stackoverflow.com/q/6667201) – 001 Jun 11 '18 at 13:33
  • Possible duplicate of [How to define a two-dimensional array in Python](https://stackoverflow.com/questions/6667201/how-to-define-a-two-dimensional-array-in-python) – Sirmyself Jun 11 '18 at 13:54

3 Answers3

1

If you want to keep your existing variables you can do the following:

row = [row1, row2, row3, row4, row5, row6, row7]
print(row[2][4])

Or you can define row as a list of lists:

row = [
    [" -", " -", " -", " -", " -", " -", " -"],
    [" -", " -", " -", " -", " -", " -", " -"],
    [" -", " -", " -", " -", " -", " -", " -"],
    [" -", " -", " -", " -", " -", " -", " -"],
    [" -", " -", " -", " -", " -", " -", " -"],
    [" -", " -", " -", " -", " -", " -", " -"],
    [" -", " -", " -", " -", " -", " -", " -"],
]
print(row[2][4])
Mike Scotty
  • 10,530
  • 5
  • 38
  • 50
1

Yes; in Python one can make a list of lists, which would accomplish what you wish. You could either use your existing variables and set rows thus:

rows = [row1, row2, row3, row4, row5, row6, row7]

or you could simply dispense with the individual rows and set the 2D list in one go, thus:

row1 = [[" -", " -", " -", " -", " -", " -", " -"],
        [" -", " -", " -", " -", " -", " -", " -"],
        [" -", " -", " -", " -", " -", " -", " -"],
        [" -", " -", " -", " -", " -", " -", " -"],
        [" -", " -", " -", " -", " -", " -", " -"],
        [" -", " -", " -", " -", " -", " -", " -"],
        [" -", " -", " -", " -", " -", " -", " -"]]

Then you can access individual elements as you wished, using rows[2][4], and the like.

0

You can use dictionary:

row =   {1 : [" -", " -", " -", " -", " -", " -", " -"],
         2 : [" -", " -", " -", " -", " -", " -", " -"],
         3 : [" -", " -", " -", " -", " -", " -", " -"],
         4 : [" -", " -", " -", " -", " -", " -", " -"],
         5 : [" -", " -", " -", " -", " -", " -", " -"],
         6 : [" -", " -", " -", " -", " -", " -", " -"],
         7 : [" -", " -", " -", " -", " -", " -", " -"]}

This will help you maintain the counting as you had with your variables.

If you already have those variables, same solution could be achieved with enumerate:

row = dict(enumerate([row1, row2, row3, row4, row5, row6, row7], 1))

You get the same, only in one line.

zipa
  • 27,316
  • 6
  • 40
  • 58
  • Why not a list of list? Do you really that starting at 1 instead of 0 is worth the overhead of a dict against a list? – Serge Ballesta Jun 11 '18 at 13:36
  • @SergeBallesta OP seems to wan't to call it with `1` indexed and this does the job. – zipa Jun 11 '18 at 13:37
  • I commented without reading the other answers. This can be an acceptable alternative even if I do not really like it (opinions...). But IMHO you should explain the reason for using a dict. Only one text line is often short for a good answer... – Serge Ballesta Jun 11 '18 at 13:44