[Tutor] Sorting the Parts of a Dictionary into a List

Jacob Bender benderjacob44 at gmail.com
Tue May 8 23:23:03 CEST 2012


Dear Tutors,

My original email was this:

"Dear tutors,

I'm trying to create a neural network program. Each neuron is in a
dictionary and each of its connections and their strengths are in a nested
dictionary. So {0:{1:4, 2:5}}, 1:{0:6}, 2:{1:2}} would mean that neuron 0
is connected to neuron 1 with a strength of 4. And it also means that
neuron 1 is connected to neuron 0 with a strength of 6.

The problem is that I'm working on a function that is supposed to add the
total strengths of each neuron. So, for example, neuron 0's connections
have a total strength of 9 (4+5). The other problem is getting all of the
total strengths and ordering the neurons into a list. So, from the example,
the list would be from [0,1,2] because zero has the greatest total strength
of 9, then 1 with a total strength of 6 and so on. I've been working on
this problem for at least 2 hours now and still haven't found anything
close to a solution."

And here's my source code:

class neuron(object):
    def __init__(self):
        self.neurons = {}
        self.neuron_total = 0

    def create_neurons(self, number):
        for i in range(number):
            self.neuron_total += 1
            self.neurons[self.neuron_total] = {}

    def connect_neurons(self,connecter,connectee):
        try:
            self.neurons[connecter][connectee] += 1
        except(KeyError):
            try:
                self.neurons[connecter][connectee] = 1
            except(KeyError):
                self.neurons[connecter] = {connectee:1}

    def ping(self,neuron,choice):
        if choice == True: #If the neuron pinged needs to choose only one
neuron
            most_connected = 0
            for connections in self.neurons[neuron]:
                if max(str(self.neurons[neuron][connections])) >
most_connected:
                    most_connected = connections
                else:
                    pass
            return most_connected
        else:
            for neuron in self.neurons:
                for connections in self.neurons[choice]:
                    return connections
    def total(self, neuron):
        total = 0
        for connection in self.neurons[neuron]:
            total = total+self.neurons[neuron][connection]



    def smartest(self): #Return the neurons in order from smartest to
dumbest in list form.
        for neuron in self.neurons:
            sorted(neuron, key=self.total(neuron))

The total function works when it returns the strength of a neuron, but I
don't think the "sorted" function is the best because, with its current
configuration, it returns a type error. I have been doing python for
several years now. I don't know EVERYTHING there is to know, but I am able
to do most tasks without error. Please help me get the neurons into an
order in a list as described in my original email. Also, I do thank you for
your original replies!

-- 
Thank you,
Jacob
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20120508/a293651a/attachment.html>


More information about the Tutor mailing list