Re: [Tutor] Tutor Digest, Vol 77, Issue 3
Instead of using "find" you could use a for loop. Explore what I mean in the attachment. If you have IDLE you can look at the programming in it. On Thu, Jul 1, 2010 at 11:58 PM, wrote: > Send Tutor mailing list submissions to >tutor@python.org > > To subscribe or unsubscribe via the World Wide Web, visit >http://mail.python.org/mailman/listinfo/tutor > or, via email, send a message with subject or body 'help' to >tutor-requ...@python.org > > You can reach the person managing the list at >tutor-ow...@python.org > > When replying, please edit your Subject line so it is more specific > than "Re: Contents of Tutor digest..." > > > Today's Topics: > > 1. S.find() (Corey Richardson) > 2. Re: S.find() (Mark Lawrence) > 3. Re: puzzled by Python 3's print() (Steven D'Aprano) > 4. Re: S.find() (Steven D'Aprano) > 5. Re: puzzled by Python 3's print() (Richard D. Moores) > 6. Re: (no subject) (Richard D. Moores) > > > -- > > Message: 1 > Date: Thu, 01 Jul 2010 18:05:35 -0400 > From: Corey Richardson > To: tutor@python.org > Subject: [Tutor] S.find() > Message-ID: <4c2d112f.7080...@aim.com> > Content-Type: text/plain; charset=ISO-8859-1; format=flowed > > Hello Tutors! >I'm having a problem with the find() method of string objects. I'm > currently making a hangman game, and I'm making the part that finds > if there are multiple copies of the guessed letter in the word, and then > if there are, finds them all. I can't for the life of me figure out the > syntax of the find() method. gameWord = "python", btw. > >The module documentation lists it as this: "S.find(sub[, start[, > end]]) -> int". >I'm assuming sub is the string you want to find, and that is how it > has worked out for me. (Bonus Points: What does sub mean? I'm guessing > subscriptable, as one of my errors says, but I'll get to that...) > When I try gameWord.find('p'[,1[,3]]), as the little help box > suggests, I get this: > > SyntaxError: invalid syntax > > Ok then, that is the exact syntax I was given. My next try is, and > gives, this: > > >>> gameWord.find('p', [1,[3]]) > > Traceback (most recent call last): > File "", line 1, in >gameWord.find('p', [1,[3]]) > TypeError: slice indices must be integers or None or have an __index__ > method > > > I assumed the comma after the 1 was messing it up, so I put this: > > >>> gameWord.find("p", [1[3]]) > Traceback (most recent call last): > File "", line 1, in >gameWord.find("p", [1[3]]) > TypeError: 'int' object is not subscriptable > > Is subscriptable what sup stands for in find()? What does mean? (5 Bonus > Points for answering that). > > I also tried passing a slice index right into it like gameWord.find('p', > [1:4]), but that returned a SyntaxError as well. > >I have the entirety of my code posted up at > http://pastebin.com/k9nMZNMy, I won't edit the code until I get this > worked out, except maybe a few housekeeping things, documentation, etc.* > > *I've tried everything I can, and I appreciate your time and help! > > ~Corey Richardson > > > > -- > > Message: 2 > Date: Thu, 01 Jul 2010 23:55:45 +0100 > From: Mark Lawrence > To: tutor@python.org > Subject: Re: [Tutor] S.find() > Message-ID: > Content-Type: text/plain; charset=ISO-8859-1; format=flowed > > On 01/07/2010 23:05, Corey Richardson wrote: > > Hello Tutors! > > I'm having a problem with the find() method of string objects. I'm > > currently making a hangman game, and I'm making the part that finds > > if there are multiple copies of the guessed letter in the word, and then > > if there are, finds them all. I can't for the life of me figure out the > > syntax of the find() method. gameWord = "python", btw. > > > > The module documentation lists it as this: "S.find(sub[, start[, end]]) > > -> int". > > What version of Python are you using? For Python 2.6.5 on Windows I > have from the compiled help file. > > " > str.find(sub[, start[, end]]) > Return the lowest index in the string where substring sub is found, such > that sub is contained in the range [start, end]. Optional arguments > start and end are interpreted as in slice notation. Return -1 if sub is > not found. > " > > > I'm assuming sub is the string you want to find, and that is how it has > > worked out for me. (Bonus Points: What does sub mean? I'm guessing > > See above. > > > subscriptable, as one of my errors says, but I'll get to that...) > > When I try gameWord.find('p'[,1[,3]]), as the little help box suggests, > > You don't need the square brackets, they're used in many forms of > documentation to indicate an optional argument. > > > I get this: > > > > SyntaxError: invalid syntax > > > > Ok then, that is the exact syntax I was given. My next try is, and > > gives, this: > > > > >>> gameWord.find('p', [1,[3]]) > > > > Traceback (most recent call last): > > File "", line 1, in > > gameWord.find('p', [1,
[Tutor] System Window
I was wondering if there was a way to program a new window using Tkinter so that a one-billion digit number could fit, instead of in the system window, which dumps numbers once they fill up the screen. I know that it would take me days to read all of the numbers, but that is not my intention. Thanks! ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] LCM
Hello all, I was wondering if you could please help me with an issue I'm having with my program. I'm not fond of LCM(Least Common Multiples), and I decided to make a program that deals with them. Here's the code: thing = raw_input("What is the first number?\n\n") thing2 = raw_input("What is the second number?\n\n") int(thing) int(thing2) x = 1 thing3 = x/thing thing4 = thing2/x def calc(): while True: if isinstance(thing3, int) == True: if isinstance(thing4, int)== True: print "The LCM is" print x raw_input() break else: x + 1 calc() else: x + 1 calc() calc() The only problem is that when I tell it that thing3 = x/thing it gives an error. Here it is: Traceback (most recent call last): File "C:/Documents and Settings/Family/My Documents/LCM.py", line 10, in thing3 = x/thing TypeError: unsupported operand type(s) for /: 'int' and 'str' Same with thing4... Can you please help! Thanks in advance... ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] (no subject)
Dear Tutors, I was wondering how I could make an AI for creatures that run around, and try to survive. Like polyworld. The real problem is making the code connection to make them learn. Can you please help? Thanks ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] (no subject)
Thanks, and I'm looking it over... On 10/30/10, Tino Dai wrote: > On Sat, Oct 30, 2010 at 6:44 PM, Jacob Bender > wrote: > >> Dear Tutors, >> >> I was wondering how I could make an AI for creatures that run >> around, and try to survive. Like polyworld. The real problem is making >> the code connection to make them learn. Can you please help? >> >> Thanks >> > > Hi Jacob, > > That is a really general question. Usually people have a specific > question and/or some code > they are having problems with. I don't know your level of Python knowledge, > but you should check > out http://wiki.hacdc.org/index.php/NARG for more information. They are > doing stuff along the > lines of what you are asking for. > > HTH, > Tino > ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] (no subject)
I have a few more ?'s. I was trying to make a program for ubuntu that does one function: if ctrl + Alt + "t" is pressed, to shut down the computer. There are only two problems. I want it to run in the background, so nobody knows it's there. I do know how to make it run at startup, so that isn't a problem. I found pygame, so I could say if ctrl +Alt + "t" is pressed to do something, but a black window would always have to be open, which leads to my first problem. I do know how a .pyw works, but it still rests in the Task Manager at the bottom of the screen. Thanks! ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] (no subject)
Dear tutors, I'm writing a program that can play Othello. Everything has been going fine, but there is a frequently occurring problem with printing the board with the squares filled in. Here's the code for the board: class board(object): def create_board(self): #Create a virtual board, that will be played on self.board = [] for squares in range(64): self.board.append(" ") self.board_table =\ """ A B C D E F G H 1 |%s|%s|%s|%s|%s|%s|%s|%s| 2 |%s|%s|%s|%s|%s|%s|%s|%s| 3 |%s|%s|%s|%s|%s|%s|%s|%s| 4 |%s|%s|%s|%s|%s|%s|%s|%s| 5 |%s|%s|%s|%s|%s|%s|%s|%s| 6 |%s|%s|%s|%s|%s|%s|%s|%s| 7 |%s|%s|%s|%s|%s|%s|%s|%s| 8 |%s|%s|%s|%s|%s|%s|%s|%s| """ def display_board(self): print self.board_table % self.board v = board() v.create_board() v.display board() I get this error: Traceback (most recent call last): File "C:\Documents and Settings\Owner\My Documents\Dropbox\Shared_With_Chris\Jake's Programs\AI\Othello_Unfinished.py", line 76, in v.display_board() File "C:\Documents and Settings\Owner\My Documents\Dropbox\Shared_With_Chris\Jake's Programs\AI\Othello_Unfinished.py", line 72, in display_board print self.board_table % self.board TypeError: not enough arguments for format string I tried doing: def display_board(self): print self.board_table % self.board[:] But that didn't help either, and I still got the same TypeError. If you could please figure out what's wrong, it would be greatly appreciated. Thanks! ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Tutor Digest, Vol 83, Issue 84
On 1/19/2011 4:53 PM, tutor-requ...@python.org wrote: Send Tutor mailing list submissions to tutor@python.org To subscribe or unsubscribe via the World Wide Web, visit http://mail.python.org/mailman/listinfo/tutor or, via email, send a message with subject or body 'help' to tutor-requ...@python.org You can reach the person managing the list at tutor-ow...@python.org When replying, please edit your Subject line so it is more specific than "Re: Contents of Tutor digest..." Today's Topics: 1. Re: Decoding from strange symbols (Alan Gauld) 2. Re: Why super does not work ! (Alan Gauld) 3. (no subject) (Jacob Bender) 4. Re: (no subject) (Emile van Sebille) 5. Correct Path for tkinter using python3.1 on Ubuntu Maverick!! (Nevins Duret) 6. Re: OOP question (Nick Stinemates) -- Message: 1 Date: Wed, 19 Jan 2011 18:04:23 - From: "Alan Gauld" To: tutor@python.org Subject: Re: [Tutor] Decoding from strange symbols Message-ID: Content-Type: text/plain; format=flowed; charset="iso-8859-1"; reply-type=original "Oleg Oltar" wrote I am trying to decode a string I took from file: file = open ("./Downloads/lamp-post.csv", 'r') I assume you know what its supposed to represent? What the columns etc are intended to be? Otherwise it will be a challenge! There is a section here that looks like the months of the year (English) in reverse: \x00S\x00e\x00a\x00r\x00c\x00h\x00e\x00s\x00\t\x00D\x00e\x00c\x00 \x002\x000\x001\x000\x00\t\x00N\x00o\x00v\x00 \x002\x000\x001\x000\x00\t\x00O\x00c\x00t\x00 \x002\x000\x001\x000\x00\t\x00S\x00e\x00p\x00 \x002\x000\x001\x000\x00\t\x00A\x00u\x00g\x00 \x002\x000\x001\x000\x00\t\x00J\x00u\x00l\x00 \x002\x000\x001\x000\x00\t\x00J\x00u\x00n\x00 \x002\x000\x001\x000\x00\t\x00M\x00a\x00y\x00 \x002\x000\x001\x000\x00\t\x00A\x00p\x00r\x00 \x002\x000\x001\x000\x00\t\x00M\x00a\x00r\x00 \x002\x000\x001\x000\x00\t\x00F\x00e\x00b\x00 \x002\x000\x001\x000\x00\t\x00J\x00a\x00n\x00 Otherwise its all a bit arbitrary... HTH, Thanks! That did the trick! ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] Socket and Changing IP's
Tutors, I was looking into network programming, and I came across a problem. Socket programs need an IP address to function correctly, right? Well, IP addresses are always changing, so how should I go about making sockets that will work for an extended period of time? Thanks in advance! ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Socket and Changing IP's
On 2/28/2011 6:57 PM, Wayne Werner wrote: On Mon, Feb 28, 2011 at 5:49 PM, Jacob Bender <mailto:benderjaco...@gmail.com>> wrote: Tutors, I was looking into network programming, and I came across a problem. Socket programs need an IP address to function correctly, right? Well, IP addresses are always changing, so how should I go about making sockets that will work for an extended period of time? Thanks in advance! If your IP address is changing that frequently, something is probably wrong! Your IP should basically stay the same from the time you connect to the internet until the time you disconnect - longer if you use static IPs. What are you attempting to program? -Wayne I'm trying to be a host without having to keep the computer on ALL of the time. Acting as a "Dropbox" would be an example. My IP doesn't change very often. It's stayed the same for days. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] Communicating Between Programs Using Raw Inputs
Dear Python Tutors, I was wondering how to break into my one program I made using brute force methods. Here's the code: password = "Helloworld" try= raw_input("What's the password?") while try != password: try = raw_input("Incorrect, what's the password?") I know how to do it in the command line, but not through another program. Generating the random tries for the password isn't the issue, but entering the password(s) in between the two programs is an issue because I don't know how to make programs communicate through raw inputs. Thanks! ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Tutor Digest, Vol 88, Issue 53
Thanks, and I'll try those. On Wed, Jun 15, 2011 at 12:47 AM, wrote: > Send Tutor mailing list submissions to >tutor@python.org > > To subscribe or unsubscribe via the World Wide Web, visit >http://mail.python.org/mailman/listinfo/tutor > or, via email, send a message with subject or body 'help' to >tutor-requ...@python.org > > You can reach the person managing the list at >tutor-ow...@python.org > > When replying, please edit your Subject line so it is more specific > than "Re: Contents of Tutor digest..." > > > Today's Topics: > > 1. Communicating Between Programs Using Raw Inputs (Jacob Bender) > 2. Re: Break stament issue (Steven D'Aprano) > 3. Re: Communicating Between Programs Using Raw Inputs > (Steven D'Aprano) > 4. Re: Communicating Between Programs Using Raw Inputs > (Steve Willoughby) > 5. Re: Communicating Between Programs Using Raw Inputs (Alan Gauld) > 6. Re: Communicating Between Programs Using Raw Inputs > (Steve Willoughby) > 7. Already Initialized Object Inheritance? (WolfRage) > > > ------ > > Message: 1 > Date: Tue, 14 Jun 2011 17:39:03 -0400 > From: Jacob Bender > To: Python Tutor > Subject: [Tutor] Communicating Between Programs Using Raw Inputs > Message-ID: > Content-Type: text/plain; charset="iso-8859-1" > > Dear Python Tutors, > > I was wondering how to break into my one program I made using brute force > methods. Here's the code: > > password = "Helloworld" > try= raw_input("What's the password?") > while try != password: > try = raw_input("Incorrect, what's the password?") > > I know how to do it in the command line, but not through another program. > Generating the random tries for the password isn't the issue, but entering > the password(s) in between the two programs is an issue because I don't > know > how to make programs communicate through raw inputs. > > Thanks! > -- next part -- > An HTML attachment was scrubbed... > URL: < > http://mail.python.org/pipermail/tutor/attachments/20110614/0c5ecf31/attachment-0001.html > > > > -- > > Message: 2 > Date: Wed, 15 Jun 2011 08:40:26 +1000 > From: Steven D'Aprano > To: tutor@python.org > Subject: Re: [Tutor] Break stament issue > Message-ID: <4df7e35a.1080...@pearwood.info> > Content-Type: text/plain; charset=ISO-8859-1; format=flowed > > Susana Iraiis Delgado Rodriguez wrote: > > Hello members! > > > > I'm doing a script that needs to loop to get some information, in order > to > > do that I'm using modules from OGR and Mapnik. These to get data from > > shapefiles, but some of the files have 0 elements, I wrote a line to > > validate it, but it hasn't worked, so I added a break to keep working. > When > > I run the scipt I got the next error: > > Traceback (most recent call last): > > File "", line 1, in > > import mapnik_punto_sin_duda > > File "C:\Python26\mapnik_punto_sin_duda.py", line 23 > > break > >^ > > IndentationError: unexpected indent > > This error has nothing to do with break. Look at the error message, and > the position of the ^ mark: the error occurs *before* the break > statement, in the indentation. > > I can duplicate the error in Python 2.6 with this: > > > >>> for t in (1, 2, 3): > ... print t # four spaces > ... break # one tab > File "", line 3 > break # one tab > ^ > IndentationError: unexpected indent > > > Newer versions of Python give more helpful error messages: Python 3 > reports "TabError: inconsistent use of tabs and spaces in indentation". > > > You can fix this by using the tabnanny script supplied with Python. From > the shell (not Python's interactive interpreter!) or the DOS prompt, run: > > python -m tabnanny -v > > replacing with the actual filename, and see what it > says. > > > -- > Steven > > > -- > > Message: 3 > Date: Wed, 15 Jun 2011 08:48:49 +1000 > From: Steven D'Aprano > To: Python Tutor > Subject: Re: [Tutor] Communicating Between Programs Using Raw Inputs > Message-ID: <4DF7E <4df7e551.6080...@pearwood.info>551.6080204@ > pearwood.info> > Content-Type: text/plain; charset=ISO-8859-1; format=flowed > > Jacob Bender wrote: > > Dear Python Tutors, > > > > I was wonder
[Tutor] Communicating Between Programs Using A Raw Input (Cont'd)
Dear Tutors, Alright, I read up on stdin and stdout and I did find how useful they are. I did have one question however, and that is will I need to modify the code of my password program? Here's the code again: password = "Helloworld" try= raw_input("What's the password?") while try != password: try = raw_input("Incorrect, what's the password?") And if so, then should I use sys.stdin.read() and sys.stdout.write() for the program that generates the random passwords or are there other commands that I should use? Thanks! ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] Communicating Between Programs Using Raw Inputs (con'd)
Dear Tutors, Alright, I'm using linux (ubuntu) and I took all of your advice and I got something that works and doesn't work at the same time. Here's the source code for my two programs called Lock and Key: *Lock.py: * password = "a" psswd_try = raw_input("What's the password? ") if psswd_try == password: print "correct!!!" raw_input() else: print "wrong" raw_input() * Key.py*: import sys sys.stdout.write("a") In the linux terminal I ran this command(they were both in my home folder, so no directories were needed): python Key.py | python Lock.py And all went well except for an EOF error caused by the raw_input inside the "if" statement in my Lock program. However I did get it to print "correct", so I know sys.stdout.write() works for what I want it to, but I don't want the EOF error. Here's the output: Traceback (most recent call last): File "Lock.py", line 7, in raw_input() EOFError: EOF when reading a line Please help me get rid of it because I couldn't find a sys.stdout.close() command or any other std command that would help. Thanks! ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] Socket and Ports
Dear Tutors, I've been having an issue with socket. I wanted to use it for transmitting strings over the Internet. The problem is that my friend insists that allowing python to transmit and receive information via an Internet port is a bad idea. He claimed that I could(and probably would) receive information that wouldn't necessarily do my computer any good(in a nutshell). First of all, I thought that you could specify where the message was supposed to come from via an IP address, while you were in the process of creating your clients(or server). That way, I shouldn't be receiving all kinds of random stuff from people on the same port that don't have that IP address. My friend also claims that hackers could use my python distribution to hack other programs on my system(all without my permission, of course). And ultimately steal information or bring down my system. All via socket, because I'd allow only that program to access the port. In conclusion, basically my friend says that whatever I do, hackers can and probably will somehow take control of my computer in some way, shape, or form. I, however think this to be untrue because I'm specifying where the information is supposed to come from and I'm not going to open my entire computer to the port, I'm ONLY allowing python to receive and transmit information. Please help. Thanks, Jacob Bender ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Socket and Ports
Thank you, and I'm not planning on executing any data I receive from anybody. So I should be pretty safe... On Sun, Oct 16, 2011 at 10:48 AM, Hugo Arts wrote: > On Sun, Oct 16, 2011 at 4:20 PM, bob gailer wrote: > > On 10/16/2011 8:28 AM, Jacob Bender wrote: > >> > >> Dear Tutors, > >> > >> I've been having an issue with socket. I wanted to use it for > >> transmitting strings over the Internet. > > > > That's good, because strings is all you can transmit. > > > >> The problem is that my friend insists that allowing python to transmit > and > >> receive information via an Internet port is a bad idea. He claimed that > I > >> could(and probably would) receive information that wouldn't necessarily > do > >> my computer any good(in a nutshell). > > > > I am not the expert on this issue. My view: > > > > once you establish a socket connection then you wait to receive data. All > > the socket software (Python or other) does is receive a string. What you > do > > with it is up to you. If you apply eval or exec to it than anything could > > happen. No one can IMHO cause any action via socket. > > > > vulnerabilities in the lower level stack notwithstanding, of course. > But in essence, using sockets in python is not any more dangerous than > using sockets in any other language. You have to watch what you're > doing and be careful with the data you receive, but as long as you do > that you shouldn't be in any danger. > > Hugo > ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] Socket not connecting
Dear Tutors, I'm the same person who asked about intruders getting into my computer via socket. I tried copying a socket program from online, that worked on my computer, and executed perfectly. However, I did not open a port for python, but I do have an exception in my firewall. Anyhow, I called the same friend and had him run a client program when I was running the host program. The host program did absolutely nothing and my IP was programmed into the client as the "Host IP". I took all necessary steps and even turned off my antivirus, which didn't help at all. I also told my friend and now he has an exception on his antivirus, but still nothing is working. I've been using port 80 the whole time. Is there anything I did wrong, or a step I've missed? Here is the source code for both the server and the client I was talking about: *Client:* * * # Echo client program import socket HOST = '# my IP was once here'# The remote host PORT = 80 # The same port as used by the server s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.connect((HOST, PORT)) s.send('Hello, world') data = s.recv(1024) s.close() print 'Received', repr(data) * * *Server:* * * # Echo server program import socket HOST = '' # Symbolic name meaning all available interfaces PORT = 80 # Arbitrary non-privileged port s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.bind((HOST, PORT)) s.listen(1) conn, addr = s.accept() print 'Connected by', addr while 1: data = conn.recv(1024) if not data: break conn.send(data) conn.close() Thanks! ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] Sorting the parts of a dictionary into a list
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. Thank you and please help! ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] Sorting the Parts of a Dictionary into a List
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 ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] Sorting a dictionary into a list cont'd
Oh, and here's the error my program returns when it runs: Traceback (most recent call last): File "/home/jacob/Dropbox/Shared_With_Chris_And_Logan/Jake's Programs/Yet_To_Work_On/Synaptic/synaptic.py", line 57, in neuron.smartest() File "/home/jacob/Dropbox/Shared_With_Chris_And_Logan/Jake's Programs/Yet_To_Work_On/Synaptic/synaptic.py", line 50, in smartest sorted(neuron, key=self.total(neuron)) TypeError: 'int' object is not iterable -- Thank you, Jacob ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor