Numpy Array of Sets

Peter Otten __peter__ at web.de
Sun May 25 09:26:42 EDT 2014


LJ wrote:

> Wolfgang, thank you very much for your reply.
> 
> Following the example in the link, the problem appears:
> 
>>>> A = [[0]*2]*3

You can see this as a shortcut for

value = 0
inner = [value, value]
A = [inner, inner, inner]

When the value is mutable (like your original set) a modification of the 
value shows in all six entries. Likewise if you change the `inner` list the 
modification shows in all three rows.

>>>> A
> [[0, 0], [0, 0], [0, 0]]
>>>> A[0][0] = 5
>>>> A
> [[5, 0], [5, 0], [5, 0]]
> 
> Now, if I use a numpy array:
> 
>>>> d=array([[0]*2]*3)
>>>> d
> array([[0, 0],
>        [0, 0],
>        [0, 0]])
>>>> d[0][0]=5
>>>> d
> array([[5, 0],
>        [0, 0],
>        [0, 0]])
> 
> 
> What is the difference here?

Basically a numpy array doesn't reference the lists, it uses them to 
determine the required shape of the array. A simplified implementation might 
be

class Array:
    def __init__(self, data):
        self.shape = (len(data), len(data[0]))
        self._data = []
        for row in data: self._data.extend(row)
    def __getitem__(self, index):
        y, x = index
        return self._data[y * self.shape[1] + x]

With that approach you may only see simultaneous changes of multiple entries 
when using mutable values.




More information about the Python-list mailing list