change every value of a 2d array in python [duplicate] - python

This question already has answers here:
List of lists changes reflected across sublists unexpectedly
(17 answers)
Closed 7 months ago.
I have a matrix like:
matrix = [
[0, 0, 0],
[0, 0, 0],
[0, 0, 0]
]
and I have to assign a value to every element based on the formula (2 ∗ i + 3 ∗ j) mod 6, where i and j are the indexes.
I'm using the following code to iterate and assign value:
for i in range(len(matrix)):
for j in range(len(matrix[i])):
matrix[i][j] = (((2 * i) + (3 * j)) % 6)
but I have this output:
matrix = [
[4, 1, 4],
[4, 1, 4],
[4, 1, 4]
]
instead of the expected:
matrix = [
[0, 3, 0],
[2, 5, 2],
[4, 1, 4]
]
how can I solve this issue? Also, I can't use NumPy to solve this problem.

That is NOT how you created your array! If you did, it would work. What you did instead is
matrix = [[0]*3]*3
That gives you three references to a SINGLE list object, not three separate list objects. Initialize it as
matrix = [[0]*3 for _ in range(3)]
or better yet, use numpy.

Related

Could not get the nested array initialization work? [duplicate]

This question already has answers here:
List of lists changes reflected across sublists unexpectedly
(17 answers)
Closed 2 years ago.
i am puzzled by this behavior
r,c = (5,2)
slist = [[0]*c]*r
print(slist)
for i in range(r):
slist[i][0] = i
print(slist)
Output is
[[0, 0], [0, 0], [0, 0], [0, 0], [0, 0]]
[[4, 0], [4, 0], [4, 0], [4, 0], [4, 0]]
When you do [[0] * c] * r, you create a list where every element is a reference to the same list. So, when you change one, they all change. Use a list comprehension with a range instead to create unique lists:
slist = [[0] * c for _ in range(r)]
See here for more info.

Select some rows in python 2d list [duplicate]

This question already has an answer here:
How to return a subset of a list that matches a condition [duplicate]
(1 answer)
Closed 2 years ago.
I have a 2d List in python like below:
[[0,2,3,4],[0,1,3,4],[1,2,3,4]]
And I want to get only items that the first item is zero. In this Example the first and second item.
How could I do this?
In [46]: a = [[0,2,3,4],[0,1,3,4],[1,2,3,4]]
In [47]: [i for i in a if i[0] == 0]
Out[47]: [[0, 2, 3, 4], [0, 1, 3, 4]]
OR
In [49]: list(filter(lambda x: x[0] == 0, a))
Out[49]: [[0, 2, 3, 4], [0, 1, 3, 4]]

why the last vector is copied to all elements of the list in python [duplicate]

This question already has answers here:
List of lists changes reflected across sublists unexpectedly
(17 answers)
Closed 2 years ago.
I want to generate random numbers to vectors, but when the external loop ends, the list contains duplicate values from the last vector
class Cops_planner:
def __init__(self):
self.move_plan_vector = []
self.move_plan_vector.append(0)
self.move_plan = []
for i in range(number_of_cops):
self.move_plan.append(self.move_plan_vector)
def algorithm(self, worlds_list_copy):
vector = []
for j in range(number_of_cops):
vector.clear()
for i in range(k):
vector.append(randint(0, 4))
self.move_plan[j] = vector
print(self.move_plan[j]) #1.
print(self.move_plan) #2.
So, for example, the first print looks like this:
[1, 0, 4, 3, 3]
[4, 0, 1, 3, 1]
[0, 0, 3, 2, 2]
[2, 3, 0, 4, 2]
and the second one is like this:
[[2, 3, 0, 0, 2], [2, 3, 0, 0, 2], [2, 3, 0, 0, 2], [2, 3, 0, 0, 2], [2, 3, 0, 0, 2]]
You're clearing the vector list but not its reference. So you need to create a new list or use a list with different reference. Otherwise when you're populating the vector list then self.move_plan hoding the last populated values of vector :
class Cops_planner:
def __init__(self):
self.move_plan_vector = []
self.move_plan_vector.append(0)
self.move_plan = []
for i in range(number_of_cops):
self.move_plan.append(self.move_plan_vector)
def algorithm(self, worlds_list_copy):
for j in range(number_of_cops):
vector = []
for i in range(k):
vector.append(randint(0, 4))
self.move_plan[j] = vector
print(self.move_plan[j]) #1.
print(self.move_plan) #2.

Python: I need to put pair of point from a list into an array [duplicate]

This question already has answers here:
How to zip two 1d numpy array to 2d numpy array [duplicate]
(3 answers)
Closed 2 years ago.
I need to put pair of points into an array
for i in range(len(lon)):
for j in range(len(lat)):
tab = np.array([lon[i],lat[j]])
output:
array([1, 2])
But what I expect to have this: array([1, 2],[1, 2],[1, 3],[1,2])
The original values are:
lon = [1, 1, 1, 1]
lat = [2, 2, 3, 2]
Thanks a lot
This is because the tab variable is being overwritten everytime in the loop. You can do it also simpler with a list comprehension:
lon = [1,1,1,1]
lat = [2,2,3,2]
output = [[lon[i],lat[i]] for i in range(len(lon))]
print(output)
This shows:
[[1, 2], [1, 2], [1, 3], [1, 2]]

Update value in Multidimensional list in Python [duplicate]

This question already has answers here:
List of lists changes reflected across sublists unexpectedly
(17 answers)
Closed 5 years ago.
I have an list like the following
line_37_data = [[0, 0, 0], [0, 0, 0], [0, 0, 0]]
When I print line_37_data[0][0] , the value 0 is printed.
When I update the list as line_37_data[0][0] = 5, the list gets modified like below
[[5, 0, 0], [5, 0, 0], [5, 0, 0]]
How can I can update the value in the list based on the index ?
Note :- I don't use NumPy. This is pure plain Python without any libraries. I am using 2.7 and not Python 3
If you pass in the same list as each element of your outer list, manipulating it will show in each place it appears. If you're just looking to fill a 2d list with zeros, list comprehension would be easy:
def generate_2d(h, w):
return [[0 for x in range(w)] for y in range(h)]
array = generate_2d(3, 3)
# Format is array[y][x] based on names in function
array[0][0] = 5
array[1][2] = 7
assert array == [
[5, 0, 0],
[0, 0, 7],
[0, 0, 0]]

Categories