Python Forum
Multiple items of a matrix has been updated.
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Multiple items of a matrix has been updated.
#1
Hey everyone, I an new in Python and I want to update an item of a matrix, but 3 items are updated. I am not sure why this happens.
Please check the following code:

	Board = []
	rows = ["_" for i in range(3)]
	for index in range(3):
		Board.append(rows)

	Board[0][0] = "1"
	print(Board)
The current Output is:
[['1', '_', '_'], ['1', '_', '_'], ['1', '_', '_']]
The expected Output should be:
[['1', '_', '_'], ['_', '_', '_'], ['_', '_', '_']]
Reply
#2
Only one row was updated. The problem is that your matrix only has one rows.
Reply
#3
(May-19-2020, 12:25 PM)deanhystad Wrote: Only one row was updated. The problem is that your matrix only has one rows.

Hey thanks for the hint! I have updated the creation of the Board as the following:
Board = [ ["_" for i in range(3)] for j in range(3) ]
However, I still don't know why my original code will create 1 row matrix:
Board = []
rows = ["_" for i in range(3)]
for index in range(3):
    Board.append(rows)
If we do print(Board) both code will generate the same output:
[['_', '_', '_'], ['_', '_', '_'], ['_', '_', '_']]
Which is a list of 3 lists
Reply
#4
What would you expect to see as output?
a = [1, 3, 3]
b = a
b.append(4)
print(a)
If still confused, try adding this line and run again
print(id(a), id(b))
b is not a copy of a, it is a. a and b are the same list. When you build your Board you are using the same rows for each row in the Bostf. If you did print(id(Board[0]), id(Board[1])) you would see they are the same and not two different lists.
Reply
#5
Thanks for the clear explanation!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Check if two matrix are equal and of not add the matrix to the list quest 3 819 Jul-10-2023, 02:41 AM
Last Post: deanhystad
  Mysql Workbench table not updated CatBall 2 1,092 Feb-13-2023, 05:37 PM
Last Post: CatBall
  How to multiply a matrix with herself, until the zero matrix results peanutbutterandjelly 3 3,352 May-03-2021, 06:30 AM
Last Post: Gribouillis
  live updated json file Nyanpasu 2 2,227 Aug-22-2020, 04:41 PM
Last Post: Nyanpasu
  Verify if .docx table of contents is updated as per document sections or not vintysaw 0 3,873 Oct-24-2019, 12:01 PM
Last Post: vintysaw
  Help with pyGenealogical-Tools (Updated) Matan_ran 5 120,442 Oct-16-2019, 08:48 PM
Last Post: Larz60+
  dictionary of dictionaries, to be updated Skaperen 2 2,362 Mar-03-2019, 07:15 AM
Last Post: Skaperen
  Updated security logger jameseroni 0 2,030 Oct-29-2018, 04:23 AM
Last Post: jameseroni
  Mouse click movements? [UPDATED] minnaadel 3 3,545 Mar-31-2018, 05:15 PM
Last Post: woooee
  Modifying / extracting multiple list items simultaneously using variable from range ehammarlund 4 3,686 Dec-06-2017, 08:15 PM
Last Post: ehammarlund

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020