Apologies as I'm sure this problem has come up before but none of the examples seem to apply.
I'm trying to create a 2D list (4 by 100) which assigns random values in different ranges in each row.
import random
size_of_meadow = 100
no_of_flowers = 100
no_of_flower_types = 3
flower = [[0] * 5] * no_of_flowers
for row in range(no_of_flowers):
flower[row][0] = random.randint(0, size_of_meadow - 1) # x coord
flower[row][1] = random.randint(0, size_of_meadow - 1) # y coord
flower[row][2] = random.randint(1, no_of_flower_types)
if random.randint(0, 100) < 5:
flower[row][3] = 1
else:
flower[row][3] = 0
print(flower[0][0])
print(flower[0][1])
print(flower[0][2])
print(flower[0][3])
print(" ")
print(flower[1][0])
print(flower[1][1])
print(flower[1][2])
print(flower[1][3])
# Sorry the above isn't in a for loop
The outputs just show the same set of numbers for each row of the 2D list...
Output:
29
21
2
0
29
21
2
0
I'm new to python and I know I must be missing something fundamental, but after a lot of umming and ahhing I haven't figured it out, any help is appreciated.
Thanks