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


Messages In This Thread
python nested list assignment weird behavior - by eyalk1 - Jan-16-2018, 07:06 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Strange behavior list of list mmhmjanssen 2 246 May-01-2024, 07:16 AM
Last Post: Gribouillis
  strange behavior of chess library in Python max22 1 359 Jan-18-2024, 06:35 PM
Last Post: deanhystad
  Weird ProcessPoolExecutor initializer behavior davetapley 2 1,707 Mar-13-2023, 06:49 PM
Last Post: davetapley
  List all possibilities of a nested-list by flattened lists sparkt 1 947 Feb-23-2023, 02:21 PM
Last Post: sparkt
  Weird behaviour using if statement in python 3.10.8 mikepy 23 3,731 Jan-18-2023, 04:51 PM
Last Post: mikepy
  Updating nested dict list keys tbaror 2 1,306 Feb-09-2022, 09:37 AM
Last Post: tbaror
  Python Program to Find the Total Sum of a Nested List vlearner 8 5,054 Jan-23-2022, 07:20 PM
Last Post: menator01
  Looping through nested elements and updating the original list Alex_James 3 2,175 Aug-19-2021, 12:05 PM
Last Post: Alex_James
  behavior list of lists roym 5 2,155 Jul-04-2021, 04:43 PM
Last Post: roym
  IWhat is the cause to get XPath in weird format using Python? MDRI 7 3,745 May-27-2021, 02:01 AM
Last Post: MDRI

Forum Jump:

User Panel Messages

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