0

I somehow understand how enumeration works, and I know how for loops work. I just mostly don't understand the tile and tiles and how that works out

for row, tiles in enumerate(self.map_data):
    for col, tile in enumerate(tiles):
        if tile == "1":
        Wall(self. col, row)

So this is supposed to take a map.txt file with a bunch of 1's representing walls and 0's representing blank space. This code runs through all of those areas and places what is supposed to be there accordingly, following this video.

smci
  • 32,567
  • 20
  • 113
  • 146
Laskio
  • 95
  • 1
  • 8
  • 1
    `print( list( enumerate(self.map_data) ))` and you will see what you have. – furas Apr 21 '19 at 01:50
  • map has rows and columns. First `for` gets single row from map with its row's number, second `for` gets single cell from row with its `column's number. – furas Apr 21 '19 at 01:54
  • I understand it is printing out its index number with a whole row. How is it finding the 1's and the .'s and seperating them? – Laskio Apr 21 '19 at 01:55
  • So apparently my mind seems to think row and col means something code wise.. Until I replaced them with cow and dol and it still ran. This just confused me even more. – Laskio Apr 21 '19 at 02:03
  • you have second `for` loop to get single item from row – furas Apr 21 '19 at 02:03
  • 1
    `row`, and `col` are normal variables and enumerate will assign values 0, 1,2,3,etc to them. You can call them `cow` or `dol` but it is good to have names which explain what you have in variable. In `row` you will have row's number, in `col` you will have `column's number` - you can also name it `x, y` – furas Apr 21 '19 at 02:06
  • If you're simply asking *"How does this nested loop iterate over the contents of a 2D array?"*, then that's just equivalent to asking *"How is a 2D array laid out in Python? (as a list-of-lists)"*. As for the `enumerate` calls, they're not strictly necessary, they just give you the `row` and `col` coords as they iterate over the array. But anyway, you could have used `print()` statements to discover all that for yourself. – smci Apr 21 '19 at 09:08
  • To the people voting to close-as-dupe, this isn't an exact duplicate of [*What does enumerate() mean?*](https://stackoverflow.com/questions/22171558/what-does-enumerate-mean). This one seems to about how **nested `for`-loops with `enumerate`s can iterate over a 2D array**. Which is more about *"How is a 2D array laid out in Python? (as a list-of-lists)"*. Like the 2D version of "How does enumerate work?"*, but only when you know how 2D arrays are laid out. – smci Apr 21 '19 at 09:12

1 Answers1

0

enumerate returns 2 items (per iteration) - the index (0 through n) and the elements themselves (ordered).

So this the case you give:

for row, tiles in enumerate(self.map_data):
    for col, tile in enumerate(tiles):
        if tile == "1":
        Wall(self, col, row)

map_data is a 2D array of tiles that has rows and columns. In the first enumerate you get the row number (called row here) and the row itself (which is a 1D array of tiles called tiles here). Then the second enumerate goes over this 1D array of tiles (called tiles) and gets the index (which is the columns) and the element itself, which is the tile (called tile).

You could conceptually rewrite this like so which might be more clear:

for i, map_data[i] in enumerate(map_data):
    for j, map_data[i,j] in enumerate(map_data[i]):
        tile = map_data[i,j]
        if tile == "1":
        Wall(self, col, row)

I don't know the context, but do notice that the ==1 relates to the element itself, not the index, so in this case tile is probably coded such that 1 denotes the presence of a wall.

edit: I noticed in the original code you use self. but I assume it's a typo, and I wrote self, here.

edit 2: I hope this example will help, using a list of strings, which appears to be the data you have. let's assume you have

map_data = ['1111','1001','1101','1111']

the first loop will give 0,1,2,3 for row and then '1111', 1001','1101','1111' for tiles. Then the second loop will give 0,1,2,3 for col and '1','1','1','1' for tile on the first row (where row is 0 and tiles is '1111'), 0,1,2,3 and '1','0','0','1' for the second row ('1001') etc.

Tacratis
  • 1,035
  • 1
  • 6
  • 16
  • I understand the first for loop now, Just cant wrap my head around the second. – Laskio Apr 21 '19 at 02:12
  • The second one runs on the rows, since each row is a 1D array, you can again run a loop through it, and each element is the tile. Will an example with numbers help you? – Tacratis Apr 21 '19 at 02:16
  • So i have printed to the screen both the map data and tiles, Map data lists the rows with their index and every single symbol in that row. (0, "1111111111111111111"), tiles lists (1, "1") 30 times which seems to only be 1 row seperated into its elements. Is the second for loop just repeating that for every single row? – Laskio Apr 21 '19 at 02:22
  • I'm not sure what you are showing, but it looks like the first row (probably the edge of the screen?) is all walls. I wouldn't choose to store the map_data this way, but it seems that it is a list of strings, and when you iterate over a string (with enumerate for example) it will go over each character (so "11111" will be treated the same as [1,1,1,1,1]). Hope this clarifies things now. – Tacratis Apr 21 '19 at 02:25
  • This is just incredibly confusing to me. I'm not sure if I'm just stupid but I cannot seem to understand this. I thank you for your help. – Laskio Apr 21 '19 at 02:31
  • I'll try to add an example using strings, I hope it will clarify it further. I do agree that the apparent choice of storing the data of the tutorial you are following (or whichever source of this sample code) is not a very intuitive one... – Tacratis Apr 21 '19 at 02:37
  • He said he was going to go to a more advanced style of tilemaps later on, but I figured if I cant even imagine to understand this one then there is no reason to move on. – Laskio Apr 21 '19 at 02:40
  • Just imagine you have a grid (excel spreadsheet would work fine) where each row is a row, and then you put a single character (letter or number or symbol) in a separate cell. Then you'll have rows and columns where each tile is a single character (and a single excel cell). The row should read (if you ignore the cells) what is in map_data. That's your 2D array – Tacratis Apr 21 '19 at 02:44