1

I try to create the list of object with some attributes like this. info.py

import numpy as np
class MapInfo(object):
    """docstring forInfomation."""
    def __init__(self, arg):
        super(MapInfo,self).__init__()
        self.arg = arg
    cost = np.array([[0,12,11,7,10,10,9,8,6,12],
            [12,0,8,5,9,12,14,16,17,22],
            [11,8,0,9,15,17,8,18,14,22],
            [7,5,9,0,7,9,11,12,12,17],
            [10,9,15,7,0,3,17,7,15,18],
            [10,12,17,9,3,0,18,6,15,15],
            [9,14,8,11,17,18,0,16,8,16],
            [8,16,18,12,7,6,16,0,11,11],
            [6,17,14,12,15,15,8,11,0,10],
            [12,22,22,17,18,15,16,11,10,0]])
    luggage = np.array([0, 10, 15, 18, 17, 3, 5, 9, 4, 6])
    capacitor = 40
class depot(object):
    """docstring for depot."""
    def __init__(self):
        super(depot, self).__init__()
        next_pt = np.zeros((1,10))

I am trying to add data for next_pt of 10 "depot" objects like this. main.py

from info import MapInfo as inf
from info import depot as dp
import numpy as np

def init():
    for i in range(0,10):
        depot.append(dp)

"""-------__init__method----------------"""
def ArrangeNextDes():
    temp = np.zeros((1,10))
    pos = np.zeros((1,10))
    for i in range(0,10):#for depot object
        for j in range(0,10):
                temp[0,j] = inf.cost[i,j]
                pos[0,j] = j
        for pt in range(0,9):# for next_pt object
            for jpt in range(pt+1,10):
                if (temp[0,jpt] < temp[0,pt]):
                    t = temp[0,pt]
                    temp[0,pt] = temp[0,jpt]
                    temp[0,jpt] = t
                    p = pos[0,pt]
                    pos[0,pt] = pos[0,jpt]
                    pos[0,jpt] = p
        depot[i].next_pt = pos
        print(depot[i].next_pt)
"""-------------------------------------""" 
"""------main program-----------"""
start_pt = 0
map_status = np.zeros((10,10))
map_status[0,0]=1
depot = []
init()
ArrangeNextDes()
for i in range(0,10):
    print("depot[%d] = "%i,depot[i].next_pt)

And the result like this:

[[0. 8. 3. 7. 6. 5. 4. 2. 1. 9.]]
[[1. 3. 2. 4. 0. 5. 6. 7. 8. 9.]]
[[2. 1. 6. 3. 0. 8. 4. 5. 7. 9.]]
[[3. 1. 0. 4. 2. 5. 6. 7. 8. 9.]]
[[4. 5. 3. 7. 1. 0. 2. 8. 6. 9.]]
[[5. 4. 7. 3. 0. 1. 8. 9. 2. 6.]]
[[6. 2. 8. 0. 3. 1. 7. 9. 4. 5.]]
[[7. 5. 4. 0. 8. 9. 3. 6. 1. 2.]]
[[8. 0. 6. 9. 7. 3. 2. 4. 5. 1.]]
[[9. 8. 7. 0. 5. 6. 3. 4. 2. 1.]]
depot[0] =  [[9. 8. 7. 0. 5. 6. 3. 4. 2. 1.]]
depot[1] =  [[9. 8. 7. 0. 5. 6. 3. 4. 2. 1.]]
depot[2] =  [[9. 8. 7. 0. 5. 6. 3. 4. 2. 1.]]
depot[3] =  [[9. 8. 7. 0. 5. 6. 3. 4. 2. 1.]]
depot[4] =  [[9. 8. 7. 0. 5. 6. 3. 4. 2. 1.]]
depot[5] =  [[9. 8. 7. 0. 5. 6. 3. 4. 2. 1.]]
depot[6] =  [[9. 8. 7. 0. 5. 6. 3. 4. 2. 1.]]
depot[7] =  [[9. 8. 7. 0. 5. 6. 3. 4. 2. 1.]]
depot[8] =  [[9. 8. 7. 0. 5. 6. 3. 4. 2. 1.]]
depot[9] =  [[9. 8. 7. 0. 5. 6. 3. 4. 2. 1.]]

The problem is that after the loop finishes, all the object of the list is the last result of the loop [[9. 8. 7. 0. 5. 6. 3. 4. 2. 1.]] when they must be equal to each results in the loop orderly (the first ten lines).

The question is what is going wrong here. Thanks

NUK
  • 11
  • 3
  • Perhaps `depot.append(dp)` should be `depot.append(list(dp))`, so that you don't copy the reference to one and the same list, but create a new list every time. – Gassa May 19 '18 at 18:50
  • 2
    You only have **one** array. `depot.append(dp)` doesn't append a copy, that's a single object you append 10 times. – Martijn Pieters May 19 '18 at 18:50
  • Also, numpy uses *views*; slicing produces a view, not a copy. `inf.cost[i,j]` gives you a view on the array, not a copy, so changing the view will change the underlying array. – Martijn Pieters May 19 '18 at 18:52
  • And `depot[i].next_pt = pos` just assigns a reference to a single object, not a copy. So each object in `depot[i]`, which is already *each the same object, a class object*, is now being assigned the same `pos` object to the `next_pt` attribute, a single numpy array, in a loop. Not a copy. – Martijn Pieters May 19 '18 at 18:54
  • 3
    You really want to read https://nedbatchelder.com/text/names.html to understand how Python names (and list indices and dictionary keys and values, etc) work. – Martijn Pieters May 19 '18 at 18:54
  • @MartijnPieters that really should become an alias. Maybe [ned]? – juanpa.arrivillaga May 19 '18 at 19:31

0 Answers0