[Tutor] appending lists of objects
Lloyd Kvam
pythontutor@venix.com
Wed, 15 May 2002 08:44:22 -0400
This is a lot of code to wade through. Can you make a smaller
example that illustrates the problem? I do not understand
what kind of response you are looking for.
Jason Barbour wrote:
> Hello all,
>
> I am having trouble appending a list of class objects.
> I am using deepcopy on a list of objects which were successfully
> created, but when I print out the list, I just get pointers
> to the instantiation. Is there a way to get my append strategy
> to work?
>
> Thanks
>
> Jason
>
> class:
>
> class ANN:
>
> def __init__(self):
> self.numero = None
> self.inputs = []
> self.weights = []
> # or make bias as the last input, or first [0]
> self.bias = None
> # make a variable to store the 'true' answer
> self.verdad = None
>
> code:
>
> def InitializeTopology(datalist, num_nodes):
>
> # datalist is 'rawlist'
> # num_nodes is 'hidden_nodes'
>
> # take the length of the [0] element
> # of datalist, where input data is kept
> # to determine input unit number
> input_units = len(datalist[0])
> output_units = len(datalist[1])
>
> # List which will hold the entire network
> NN = [[]]
> # Temp lists to append to NN
> ent = []
> out = []
> hid = []
> # NN example counter
> j = 0
> print datalist
> # loop through each datalist element
> for i in range(len(datalist)):
> NN.append([])
> # loop through input list within datalist element
> ent = []
> for e in (range(input_units)):
> ent.append(datalist[i][e])
> print "Input Nodes"
> print ent
> entx = copy.deepcopy(ent)
> NN[j].append(entx)
>
> # loop through desired hid node list and
> # instantiate and append that number
> hid = []
> for h in (range(num_nodes)):
> # hnode is a temp instantiation
> hnode = ANN()
> # set numero
> hnode.numero = h
> # add one extra for the bias node
> for d in range(len(datalist[i][0])):
> # set random weights
> hnode.weights.append(GetRandom())
> hid.append(hnode)
> # this works fine
> print "Hidden Node Weights"
> print hid[h].weights
> hidx = copy.deepcopy(hid)
> NN[j].append(hidx)
>
> # loop through output list within datalist element
> out = []
> for s in (range(output_units)):
> # temp instantiation of node class
> onode = ANN()
> # set numero
> onode.numero = s
> # write 'true' value for this node in from datalist
> onode.verdad = datalist[i][1]
> # add one extra for the bias node
> for hi in range(len(hid) + 1):
> # set random weights
> onode.weights.append(GetRandom())
> ond = copy.deepcopy(onode)
> out.append(ond)
> print "Output Node Weights"
> print out[s].weights
> outx = copy.deepcopy(out)
> NN[j].append(outx)
>
> j = j + 1
>
> # when I view the final list,
> # i only see the pointers shown at the
> # end of the output below
> print NN
> print NN[0][1]
> return NN
>
> output:
>
> Input Nodes
> [['0', '1', '0', '1', '0', '1'], ['1']]
> Hidden Node Weights
> [-0.018296596687270905, -0.081403175088918187, -0.053187146906907665,
> 0.056229649302163988, -0.075566267128166456, -0.022527181800035524]
> Hidden Node Weights
> [-0.0065363445994707451, 0.069861139620503818, 0.0024552409969152309,
> 0.076356456899948924, -0.069533002904092575, -0.014238828091323708]
> Hidden Node Weights
> [-0.075400889950218292, -0.050053122099344274, -0.0048485866459656354,
> 0.08662597642000594, -0.017859878096892778, 0.055191369394788102]
> Output Node Weights
> [0.037663575883956704, 0.039522195201120526, 0.052848206171747923, -
> 0.01037766943868983]
> Output Node Weights
> [-0.078275023370294267, 0.069305743921806412, 0.025268759536601591,
> 0.056177439285214084]
> Input Nodes
> [['1', '0', '1', '0', '1', '0'], ['0']]
> Hidden Node Weights
> [0.081712459684275235, -0.073062555492460518, -0.089030582658396876, -
> 0.080425085755549922, -0.05533052873035009, 0.0087849646658016093]
> Hidden Node Weights
> [-0.00023837933585462155, 0.097517550486271948, 0.090236482283893277,
> 0.089746470744624232, -0.014322076190345668, -0.050886396966472083]
> Hidden Node Weights
> [0.00011939214512719509, 0.040501166817123305, -0.095668774128323864,
> 0.078139479446892604, 0.08745296237997438, -0.018099444839937705]
> Output Node Weights
> [-0.027795386749378048, -0.015904567714063299, 0.069970274891191647, -
> 0.0051844350890069887]
> Output Node Weights
> [0.062263405185009335, -0.070228462857787652, -0.058050767060430421,
> 0.092388392928725957]
> Input Nodes
> [['0', '1', '1', '0', '1', '1'], ['1']]
> Hidden Node Weights
> [-0.014520789653509381, 0.050637826018312279, -0.096331315026787184, -
> 0.015789978617662914, -0.080072518921819588, 0.057655140839156793]
> Hidden Node Weights
> [0.077143949905593145, -0.052093418319049123, 0.021041221782613872, -
> 0.073784377429996467, 0.081250167691744937, -0.073069394523676903]
> Hidden Node Weights
> [0.079183132057173283, -0.064352582341993106, -0.047743468583672025,
> 0.056374150998905657, -0.089680772685834367, 0.093224700033546687]
> Output Node Weights
> [0.031468967429602215, 0.037860013704278585, 0.026289431822902287,
> 0.047595309078331482]
> Output Node Weights
> [0.022103436047705573, 0.080429621649877797, -0.032776026302476426, -
> 0.044834187905566168]
> Input Nodes
> [['1', '1', '0', '0', '1', '0'], ['0']]
> Hidden Node Weights
> [-0.09723932133933047, 0.082793639556330317, -0.014779765311136072,
> 0.05815203111935676, 0.093341660960959721, -0.041596612150172387]
> Hidden Node Weights
> [-0.033319581113907759, 0.035324831017861325, -0.093066597933479517,
> 0.055496245555570048, 0.067858476165719578, -0.066920463380311906]
> Hidden Node Weights
> [-0.0266917262783797, 0.051820006475687741, -0.0093643674736724417,
> 0.084802963987182942, 0.029095629325276783, -0.057989083104446948]
> Output Node Weights
> [-0.09894360261541886, -0.096914462031604504, 0.059415174380555588, -
> 0.0036597219717522389]
> Output Node Weights
> [-0.015256824879165975, 0.010845872845301941, -0.029854732686460394, -
> 0.070282855919804182]
> [[[['0', '1', '0', '1', '0', '1'], ['1']], [<__main__.ANN instance at 0x02E02340
>
>>, <__main__.ANN instance at 0x02E67500>, <__main__.ANN instance at 0x02E67150
>>], [<__main__.ANN instance at 0x02E62B20>, <__main__.ANN instance at 0x02E641A0
>>]], [[['1', '0', '1', '0', '1', '0'], ['0']], [<__main__.ANN instance at
>>
> 0x02E12120>, <__main__.ANN instance at 0x02E13C40>, <__main__.ANN instance at
> 0x02E11360>], [<__main__.ANN instance at 0x02E654D0>, <__main__.ANN instance at
> 0x02E90EF0>]], [[['0', '1', '1', '0', '1', '1'], ['1']], [<__main__.ANN instance
> at 0x02EA51E0>, <__main__.ANN instance at 0x02EA6EA0>, <__main__.ANN instance at
> 0x02EA6C10>], [<__main__.ANN instance at 0x02DF75A0>, <__main__.ANN instance at
> 0x02DF7390>]], [[['1', '1', '0', '0', '1', '0'], ['0']], [<__main__.ANN instance
> at 0x02EB6F80>, <__main__.ANN instance at 0x02EB6D10>, <__main__.ANN instance at
> 0x02EB6A50>], [<__main__.ANN instance at 0x02DF53A0>, <__main__.ANN instance at
> 0x02E138F0>]], []]
> [<__main__.ANN instance at 0x02E02340>, <__main__.ANN instance at 0x02E67500>,
> <__main__.ANN instance at 0x02E67150>]
>
>
>
>
> _______________________________________________
> Tutor maillist - Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
>
>
--
Lloyd Kvam
Venix Corp.
1 Court Street, Suite 378
Lebanon, NH 03766-1358
voice:
603-443-6155
fax:
801-459-9582