1

So, I'm trying to make a python program that needs to substitute a value in a python 2d array, but somehow it isn't working correctly, giving me results like:

[0, M, 0, M]
[0, M, 0, M]
[0, M, 0, M]
[0, M, 0, M]

when it's supposed to grant me results like:

[0, 0, 0, M]
[0, M, 0, 0]
[0, 0, 0, 0]
[0, 0, 0, 0]

Please, if someone could explain to me what causes the error, I would be really thankful.

import random
import sys
import os

def genwmap(minv, maxv, mode):
    wmap = []
    temp = []
    mx = random.randint(minv, maxv)
    my = random.randint(minv, maxv)
    for element in range(0, mx):
        temp.append("0")
    for element in range(0, my):
        wmap.append(temp)
    temp = random.randint(int(minv*maxv/15), int(minv*maxv/5)) #Use temp var to generate region's count
    if mode == 1:
        for element in range(0, temp):
            one = random.randint(0, len(wmap)-1)
            two = random.randint(0, len(wmap[0])-1)
            wmap[one][two] = "M"
            for element in wmap:
                print(element)
            print(wmap[1][1])
            print("one: " + str(one))
            print("two: " + str(two))
    return wmap

r = genwmap(10, 20, 1)
for element in r:
    print(element)
Rockintor
  • 13
  • 2
  • could you provide a little bit more about what you're trying to achieve ? – Wahalez Jan 28 '22 at 18:39
  • The problem is that `wmap.append(temp)` appends `temp` to `wmap` mutiple times in the loop. So you have a list which contains multiple references to the same list instead of a list of multiple lists like you want. – Code-Apprentice Jan 28 '22 at 18:47
  • Hi, Code-Apprentice, I've checked the post you've linked and shall say it was very helpful. I didn't even notice I was always linking to the same link. I'm really thankful for your answer, thank you! – Rockintor Jan 28 '22 at 21:27

1 Answers1

0

This code works as intended.

def genwmap(minv, maxv, mode):

    mx = random.randint(minv, maxv)
    my = random.randint(minv, maxv)

    wmap = [["0" for _ in range(mx)] for _ in range(my)]

    temp = random.randint(int(minv*maxv/15), int(minv*maxv/5))  # Use temp var to generate region's count

    if mode == 1:

        for element in range(temp):
            one = random.randint(0, len(wmap)-1)
            two = random.randint(0, len(wmap[0])-1)
            wmap[one][two] = "M"
            for element in wmap:
                print(element)
            print(wmap[1][1])
            print("one: " + str(one))
            print("two: " + str(two))
    return wmap


r = genwmap(10, 20, 1)
for element in r:
    print(element)

your problem was those lines where you create the 2D array:

for element in range(0, mx):
        temp.append("0")

for element in range(0, my):
        wmap.append(temp)

here you create a array that looks like this

[
 temp,
 temp,
 temp,
]

see they are all the same. thats do too how python handles memory and how it saves memory. Then instead of just making a new copy of temp when you add it to the wmap list is simply links always to the same list temp to save memory