Python: Changing values of a nested list

JayB

So I'm using a nested list to store some data and I'm having trouble with changing specific values of one of the sublists:

if attributes[3] == 'W':
    self.board[3][3] = 'W'

(numbers are placeholders i'm using to test)

board is a class variable that is created as follows (I'm trying to create a grid as specified by a user, with their input for column and row sizes making up the first two parts of attributes)

self.board = []
rows = []
self.score = [0, 0]
for x in range(attributes[0]):
    rows.append('')
for y in range(attributes[1]):
    self.board.append(rows)

However, whenever I try to change the value of a sublist, it changes the values for all sublists for that same index:

[['', '', '', 'W', '', '', '', ''], ['', '', '', 'W', '', '', '', ''], ['', '', '', 'W', '', '', '', ''], ['', '', '', 'W', '', '', '', ''], ['', '', '', 'W', '', '', '', ''], ['', '', '', 'W', '', '', '', ''], ['', '', '', 'W', '', '', '', ''], ['', '', '', 'W', '', '', '', '']]

I can't figure out what's wrong. Anyone have any ideas?

Marcin

I think the reason for this can be demonstrated on this very simple example:

a = [1,2,3]

b = [a,a,a] # <- this is what you are actually doing

print(list(map(id,b)))
#[140475353832712, 140475353832712, 140475353832712]

Please not that all ids are same above. This means that you are stroning in your list, references to the same object. thus changing one subsist, changes every sublist.

However, you should be doing this (or some equivalent):

b2 = [a[:], a[:], a[:]] #
print(list(map(id,b2)))
#[140475353832136, 140475353832072, 140475353832520]

Please note, different id for each sublist. Now b2 contains references to different sublists.

To sum up, you should have this:

for y in range(attributes[1]):
    self.board.append(rows[:])

Este artigo é coletado da Internet.

Se houver alguma infração, entre em [email protected] Delete.

editar em
0

deixe-me dizer algumas palavras

0comentários
loginDepois de participar da revisão

Artigos relacionados

Inner values in Python nested dictionary changing for no clear reason

Filtering a nested list by values in a second nested list

Python: create a nested dictionary from a list of parent child values

How to correctly update the values of list types in a Python nested dictionary?

Changing nested URL Values from API

Turn Python list from CSV with multi-value fields into a Python nested list, sort nested list values and export to CSV

Changing the values in a column using python

Pyspark: merging values in a nested list

Nested list to table python

Nested list comprehension on python

Python: Nested JSON List

Changing first item in nested list using list comprehension

A nested array is changing to a string of values going from JavaScript to Rails

Nested for loops python - duplicated values

Need help in printing dictionary & adding user prompt to obtain nested list values [Python]

Python nested list comprehension (accessing nested elements)

Extending python dictionary and changing key values

Changing long, lat values of Polygon coordinates in python

Can't update values in a nested list

Is there a better way to create a list of values of a nested dictionary?

Slicing nested list by index in python

python nested list comprehension in nltk

Detecting recursive nested list in Python

python nested list unexpected behaviour

Python - doing maths with a nested list

Combinations by list in nested lists in Python

Swap values in the list in Python

Integrating a list of values in Python

Changing an attribute for each object in a list for python