Python Forum
python nested list assignment weird behavior
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
python nested list assignment weird behavior
#1
mat was initialized like this:
mat = [[0]*26]*5
The problem is that after an inner loop iteration all the rows in mat get changed the same but i clearly stated that only the ith row should change.
the problematic lines are the ones after the # comment
please help me :(

def fill_letters_matrix(mat, word_list):
        for i in range(len(mat)):
            for j, item in enumerate(word_list):
                if j != len(word_list)-1:
                    if len(item)-i-1 < 0:
                        continue
                    #the jibrish in the second brackets works,so no need to dive in...
                    mat[i][ord(item[len(item)-i-1])-ord("A")] += 1
                else:
                    mat[i][ord(item[len(item) - i-1]) - ord("A")] -= 1
Reply
#2
Please, read this https://nedbatchelder.com/text/names1.html
the way you initialize it is a problematic, because actually every row is a reference to the same 26-element list (row).
>>> mat = [[0]*6]*5
>>> print mat
[[0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0]]
>>> mat[1][1]=1
>>> mat
[[0, 1, 0, 0, 0, 0], [0, 1, 0, 0, 0, 0], [0, 1, 0, 0, 0, 0], [0, 1, 0, 0, 0, 0], [0, 1, 0, 0, 0, 0]]
>>> mat = [[0]*6 for _ in range(5)]
>>> mat
[[0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0]]
>>> mat[1][1] = 1
>>> mat
[[0, 0, 0, 0, 0, 0], [0, 1, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0]]
>>> 
Reply
#3
In [1]: mat = [[0]*26]*5

In [2]: id(mat[0])
Out[2]: 140082606853896

In [3]: id(mat[1])
Out[3]: 140082606853896
As you can see the list elements are one object.
Use list comprehension instead.

mat = [0 for _ in range(26)] for range(5)]]
See this video: https://www.youtube.com/watch?v=F6u5rhUQ6dU
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  strange behavior of chess library in Python max22 1 261 Jan-18-2024, 06:35 PM
Last Post: deanhystad
  Weird ProcessPoolExecutor initializer behavior davetapley 2 1,577 Mar-13-2023, 06:49 PM
Last Post: davetapley
  List all possibilities of a nested-list by flattened lists sparkt 1 878 Feb-23-2023, 02:21 PM
Last Post: sparkt
  Weird behaviour using if statement in python 3.10.8 mikepy 23 3,430 Jan-18-2023, 04:51 PM
Last Post: mikepy
  Updating nested dict list keys tbaror 2 1,243 Feb-09-2022, 09:37 AM
Last Post: tbaror
  Python Program to Find the Total Sum of a Nested List vlearner 8 4,787 Jan-23-2022, 07:20 PM
Last Post: menator01
  Looping through nested elements and updating the original list Alex_James 3 2,070 Aug-19-2021, 12:05 PM
Last Post: Alex_James
  behavior list of lists roym 5 2,036 Jul-04-2021, 04:43 PM
Last Post: roym
  IWhat is the cause to get XPath in weird format using Python? MDRI 7 3,605 May-27-2021, 02:01 AM
Last Post: MDRI
  pls help with this assignment. basic into to python class valerie_thomas 2 2,008 Nov-21-2020, 12:37 PM
Last Post: ibreeden

Forum Jump:

User Panel Messages

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