Strange behaviour while inserting into spliced lists? [duplicate] - python

This question already has answers here:
Changing a particular element in a 2-D list in python
(3 answers)
Closed 9 years ago.
What I am trying to achieve is to insert a small 2D-list into a big one (I'm using Python 2.7.3).
Why am I getting different results for var1 and var2?
def modify(foo):
small = [[1]*2]*2
for y, line in enumerate(small):
foo[y+1][1:3] = line
return foo
var1 = [[0]*4]*4
var2 = [
[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0]]
print modify(var1)
print modify(var2)
# Result:
# var1 = [
# [0, 1, 1, 0],
# [0, 1, 1, 0],
# [0, 1, 1, 0],
# [0, 1, 1, 0]]
#
# var2 = [
# [0, 0, 0, 0],
# [0, 1, 1, 0],
# [0, 1, 1, 0],
# [0, 0, 0, 0]]

By using [[0]*4]*4 you are in fact creating a single list ([0]*4) and referencing it four times.
>>> var1 = [[0]*4]*4
>>> var1
[[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]
>>> var1[0][0] = 2
>>> var1
[[2, 0, 0, 0], [2, 0, 0, 0], [2, 0, 0, 0], [2, 0, 0, 0]]
>>> var1[0] is var1[1]
True
It's a little clearer if you replace the inner content with a variable
>> inner = [0]*4
>> var1 = [inner, inner, inner, inner]
>>> var1
[[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]
>>> inner[0] = 1
>>> var1
[[1, 0, 0, 0], [1, 0, 0, 0], [1, 0, 0, 0], [1, 0, 0, 0]]

[0]*4 creates [0, 0, 0, 0], however, [[0,0,0,0]]*4 creates a list containing shallow copies of [0,0,0,0]. That means, all inner lists are the same.

Related

why doesn't append in python work properly?

def get_next_state_for_x(list, next_state):
final = []
state = list
for g in range (len(next_state)):
temp = state[g]
state[g] = next_state[g]
print(state)
final.append(state)
state[g] = int(temp)
print(final)
get_next_state_for_x([0, 0, 0, 0], [1, 1, 1, 1])
so while i compile this code i get the output:
[1, 0, 0, 0]
[0, 1, 0, 0]
[0, 0, 1, 0]
[0, 0, 0, 1]
[[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]
instead of (for the last line)
[[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 1, 0], [0, 0, 0, 1]]
why does final.append(state) add wrong list to the result ?
You're linking the list, so it changes everytime. You have to copy it
Correct to:
final.append(state.copy())
So:
def get_next_state_for_x(list, next_state):
final = []
state = list
for g in range (len(next_state)):
temp = state[g]
state[g] = next_state[g]
print(state)
final.append(state.copy())
state[g] = int(temp)
print(final)
get_next_state_for_x([0, 0, 0, 0], [1, 1, 1, 1])
Output:
[1, 0, 0, 0]
[0, 1, 0, 0]
[0, 0, 1, 0]
[0, 0, 0, 1]
[[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 1, 0], [0, 0, 0, 1]]

Multiplying a list by a number creates items have relation with the original one? [duplicate]

This question already has answers here:
List of lists changes reflected across sublists unexpectedly
(17 answers)
Closed 9 years ago.
I have a list of List say mysolution:
>>>mySolution
[[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]
>>> mySolution[0][0] = 1
>>> mySolution
[[1, 0, 0, 0], [1, 0, 0, 0], [1, 0, 0, 0], [1, 0, 0, 0]]
Intended output:
[[1, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]
why is it that all the 1st elements in my list of list's is being changed to 1?
I would only like to change the first element of the first list to 1.
What matters is how you created your original mysolution list. As it seems, it contains four times the same list which is why changing it once will make it change in all four locations.
To initialize independent zero-filled lists like that, you can do the following:
mysolution = [[0] * 4 for i in range(4)]
It's quite possible that you created the list like this:
mySolution = [0]*4
mySolution = [mySolution]*4
Or equivalently:
mySolution = [[0]*4]*4
Either of the above snippets will create a list with four sublists which are copies of the exact, same sublist, so any modification on one sublist will be reflected on the others - they're one and the same. The solution is to create four different sublists:
mySolution = [[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]
Or a bit shorter:
mySolution = [[0]*4 for _ in xrange(4)]
Because all the contained lists are actually the same list. When you do:
l = [0, 0, 0, 0]
my_solution = [l, l, l]
Then, my_solution[0], my_solution[1], and my_solution[2] are references to the same object (l).
If you modify the list in one location, it changes everywhere. That is because lists are mutable objects.
Instead, use multiple lists:
l1 = [0, 0, 0, 0]
l2 = [0, 0, 0, 0]
l3 = [0, 0, 0, 0]
my_solution = [l1, l2, l3]
Which will work as intended.
please note that this is doing fine:
mySolution = [[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]
mySolution[0][0] = 1
print mySolution
>>>
[[1, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]
it all depends on how you initialized your solution. this
mySolution = [[0, 0, 0, 0]]*4
mySolution[0][0] = 1
print mySolution
gives
>>>
[[1, 0, 0, 0], [1, 0, 0, 0], [1, 0, 0, 0], [1, 0, 0, 0]]
>>>
because here each array [0, 0, 0, 0] in mySolution is a copy of initialization array [0, 0, 0, 0] in [[0, 0, 0, 0]]*4. if you change first element of first array, copy of it also change.
with this initialization mySolution = [[0, 0, 0, 0] for x in range(4)] you are not copying the array but appending [0,0,0,0] four times, giving the result that you are expecting.

Python: Iterating over sub-lists

I have a project where I am trying to edit portions of nested lists.
Say I started with this list:
[[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]]
I want to iterate over a portion of this list so that I get an output that is a square of ones in the center like so.
[[0, 0, 0, 0, 0],
[0, 1, 1, 1, 0],
[0, 1, 1, 1, 0],
[0, 1, 1, 1, 0],
[0, 0, 0, 0, 0]]
I tried using a for-loop to iterate through the list and a nested for loop to iterate through the sub-lists. However, that did not work. What I got instead was this list:
[[0, 1, 1, 1, 0],
[0, 1, 1, 1, 0],
[0, 1, 1, 1, 0],
[0, 1, 1, 1, 0],
[0, 1, 1, 1, 0]]
Here is my code:
list = [[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]]
for i in range(1,4):
for j in range(1,4):
list[i][j] = 1
Why won't this code work? I have searched for a day or two and have not found an answer. Thank you in advance to whoever takes the time to answer or comment.
The code you posted is working fine:
>>> list = [[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]]
>>>
>>> for i in range(1,4):
... for j in range(1,4):
... list[i][j] = 1
...
>>> pprint(list)
[[0, 0, 0, 0, 0],
[0, 1, 1, 1, 0],
[0, 1, 1, 1, 0],
[0, 1, 1, 1, 0],
[0, 0, 0, 0, 0]]
Check that your code actually looks like what you posted here.

Changing an element in one list changes multiple lists [duplicate]

This question already has answers here:
List of lists changes reflected across sublists unexpectedly
(17 answers)
Closed 9 years ago.
I have a list of List say mysolution:
>>>mySolution
[[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]
>>> mySolution[0][0] = 1
>>> mySolution
[[1, 0, 0, 0], [1, 0, 0, 0], [1, 0, 0, 0], [1, 0, 0, 0]]
Intended output:
[[1, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]
why is it that all the 1st elements in my list of list's is being changed to 1?
I would only like to change the first element of the first list to 1.
What matters is how you created your original mysolution list. As it seems, it contains four times the same list which is why changing it once will make it change in all four locations.
To initialize independent zero-filled lists like that, you can do the following:
mysolution = [[0] * 4 for i in range(4)]
It's quite possible that you created the list like this:
mySolution = [0]*4
mySolution = [mySolution]*4
Or equivalently:
mySolution = [[0]*4]*4
Either of the above snippets will create a list with four sublists which are copies of the exact, same sublist, so any modification on one sublist will be reflected on the others - they're one and the same. The solution is to create four different sublists:
mySolution = [[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]
Or a bit shorter:
mySolution = [[0]*4 for _ in xrange(4)]
Because all the contained lists are actually the same list. When you do:
l = [0, 0, 0, 0]
my_solution = [l, l, l]
Then, my_solution[0], my_solution[1], and my_solution[2] are references to the same object (l).
If you modify the list in one location, it changes everywhere. That is because lists are mutable objects.
Instead, use multiple lists:
l1 = [0, 0, 0, 0]
l2 = [0, 0, 0, 0]
l3 = [0, 0, 0, 0]
my_solution = [l1, l2, l3]
Which will work as intended.
please note that this is doing fine:
mySolution = [[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]
mySolution[0][0] = 1
print mySolution
>>>
[[1, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]
it all depends on how you initialized your solution. this
mySolution = [[0, 0, 0, 0]]*4
mySolution[0][0] = 1
print mySolution
gives
>>>
[[1, 0, 0, 0], [1, 0, 0, 0], [1, 0, 0, 0], [1, 0, 0, 0]]
>>>
because here each array [0, 0, 0, 0] in mySolution is a copy of initialization array [0, 0, 0, 0] in [[0, 0, 0, 0]]*4. if you change first element of first array, copy of it also change.
with this initialization mySolution = [[0, 0, 0, 0] for x in range(4)] you are not copying the array but appending [0,0,0,0] four times, giving the result that you are expecting.

python: problems with a list of class objects: All items are the same [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
“Least Astonishment” in Python: The Mutable Default Argument
I'm trying to create a list of objects from the class "fooclass", with different attributes, but always end up with all elements of the list containing the same values.
Here is the code I run:
#!/usr/bin/env python
class fooclass():
def __init__(self,vertices = [[0,0,0],[0,0,0],[0,0,0],[0,0,0],[0,0,0],[0,0,0],[0,0,0],[0,0,0]]):
self.vertices = vertices
l=[]
print(l)
a=fooclass(); a.vertices[0]=[7,9,9]; l.append(a)
print 'a=', a.vertices
a=fooclass(); a.vertices[0]=[789,9,9]; l.append(a)
print 'a=', a.vertices
print(l[0].vertices)
print(l[1].vertices)
print(l)
l=[]
print(l)
a=fooclass(); a.vertices[0]=[7,9,9]; l.append(a)
print 'a=', a.vertices
b=fooclass(); b.vertices[0]=[789,9,9]; l.append(b)
print 'b=', b.vertices
print(l[0].vertices)
print(l[1].vertices)
print(l[0])
print(l[1])
And the output I get:
$ python ./class_test2.py
[]
a= [[7, 9, 9], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0]]
a= [[789, 9, 9], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0]]
[[789, 9, 9], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0]]
[[789, 9, 9], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0]]
[<__main__.fooclass instance at 0x7f945eafecf8>, <__main__.fooclass instance at 0x7f945eafed88>]
[]
a= [[7, 9, 9], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0]]
b= [[789, 9, 9], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0]]
[[789, 9, 9], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0]]
[[789, 9, 9], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0]]
<__main__.fooclass instance at 0x7f945eafecf8>
<__main__.fooclass instance at 0x7f945eafed88>
Why are l[0].vertices and l[1].vertices exactly the same despite inputting different values?
System info:
Ubuntu 10.04.4 LTS
$ python --version
Python 2.6.5
$ uname -a
Linux *** 2.6.32-39-generic #86-Ubuntu SMP Mon Feb 13 21:50:08 UTC 2012 x86_64 GNU/Linux
Note: Tried with Python 3.1.2 (just changing the print statements), same problem. :(
The default value of vertices can be modified in Python. You can rewrite __init__ like so to avoid it:
def __init__(self, vertices=None):
if vertices is None:
vertices = [[0,0,0],[0,0,0],[0,0,0],[0,0,0],[0,0,0],[0,0,0],[0,0,0],[0,0,0]]
self.vertices = vertices
When you set the default argument vertices = [...], you're actually instantiating a single list object to be used every time the argument is unspecified. That means both of your fooclass instances are sharing--and modifying--a single list.
Instead, instantiate it inside your __init__ method if no value is given for that argument. That ensures that a new one is created each time the method is run.
Edit: phihag's answer gives good example code for correcting __init__.

Categories