Re: [Tutor] dealing with user input whose value I don't know
On Fri, Oct 3, 2008 at 9:45 AM, David [EMAIL PROTECTED] wrote: Here is the code: def main(): import string Hey, lagging a bit behind the list, import string is unnecessary, mate. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] More Pythonesque or a more efficient method
Hi, The below script which prints anagrams available for any word available within a given database. It does work, but it is not very fast. I am relatively certain there are more Python friendly coding techniques but I am more concerned with a faster algorithm. The database item consists of the key; the actual word, and the value, the size as a string. For example, the word 'myth' is represented as key= 'myth', value = '4'. I think the slow part of the algorithm is the script retrieves a list of all database words of length (myth)=4. That's a lot of words. Each word is sorted and then compared to the alpha sorted word. When there is a match, that word becomes a part of the anagram list. Even a very basic look at the word 'myth' shows there are no anagrams. So, to return an empty list, we have scanned all words in the database of size 4. I will be happy to send the first program and the original word file to anyone who would like to implement the database. I could not attach the database since that made this post way too large. Any and all suggestions are most welcome. #!/usr/bin/env python ##The second program is in a perpetual loop waiting on input. A 'Q ' will terminate the program. ##All input is by string; for example the word 'zygote' without the quotes is a request for all possible anagrams ##created from all the letters in 'zygote'. def get_key(d1, value): return [item[0] for item in d1.items() if item[1] == value] def Get_Anagrams(db,word): Alist=list() tulip1=tuple(word) list1=list(tulip1) list1.sort() wordlist=get_key(db,str(len(word))) for items in wordlist: if items != word: tulip2=tuple(items) list2=list(tulip2) list2.sort() if list2==list1: Alist.append(items) if len(Alist)0: return Alist else: return None def main(): import anydbm db=anydbm.open('anagram.db','r') Repeat=True while Repeat != False: word_in=str(raw_input('Input a word to build anagrams\nA Q will quit the program: ')) if word_in=='Q': Repeat = False else: if db.has_key(word_in) 1: print word_in,' is not in the dictionary\n' else: Anagrams=Get_Anagrams(db,word_in) if Anagrams != None: print 'Anagrams for ',word_in,' are ',Anagrams else: print word_in,' has no anagrams.' db.close return 0 if __name__ == '__main__': main() Thanks, Robert ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] PYTHON 26 DOESN'T MATCH TUTORIAL
On Sun, Oct 5, 2008 at 3:44 AM, WM [EMAIL PROTECTED] wrote: I am trying to learn PYTHON from scratch using two screens; the tutor and IDLE. The first two lessons went OK but the third (IF and ELSE IF) just do not work. When I try ELSE I get without any further indent. When I did COPY and PASTE I got showered with incomprehensible error messages. I did this because I wondered if I were screwing up with my keyboarding. Can you copy / paste the IDLE session to an email? My guess is that you entered an extra line before the else. You should just backspace to remove the indent. Is this what you do? Answer about beginners' problems? I just subscribed to the list; this is my first query. Yes, pretty much. You should send your question to tutor@python.org, though. tutor-owner just goes to the list maintainers, not to the entire list. When you reply, use Reply All so your reply also goes to the list. Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] More Pythonesque or a more efficient method
Kent, Thank you for two excellent suggestions. I will implement your suggestion of indexing by the sorted letters in the word. Robert Kent Johnson wrote: On Sun, Oct 5, 2008 at 1:52 PM, Robert Berman [EMAIL PROTECTED] wrote: The database item consists of the key; the actual word, and the value, the size as a string. For example, the word 'myth' is represented as key= 'myth', value = '4'. I think the slow part of the algorithm is the script retrieves a list of all database words of length (myth)=4. That's a lot of words. Each word is sorted and then compared to the alpha sorted word. When there is a match, that word becomes a part of the anagram list. Even a very basic look at the word 'myth' shows there are no anagrams. So, to return an empty list, we have scanned all words in the database of size 4. This is pretty inefficient, yes. You actually scan every word in the database because you have to find the ones of length 4. You are really using the database as just a list of words. A better data structure would be to index by word length with the value being a list of words of that length. Even better would be to index by the sorted letters in the word, with the value being a list of all words with that sorting. Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] dealing with user input whose value I don't know
Omer [EMAIL PROTECTED] wrote Here is the code: def main(): import string import string is unnecessary, mate. Not entirely true since the code uses string.split() However since the split method of the string could be used instead then that would indeed render the import unnecessary. But you need both changes. Alan G ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] IF statements
I used to do Basic and enjoyed it. Someone said Python was a vastly better language than Visual Basic, which I considered playing with. So I sought to give it a go but struck a sticking point very early. I am now going through the Python tutorial. All went well until I came to IF. The code below was not written by me. It is a copy/paste job from the tutor. I do not have any idea what is going wrong. IDLE 2.6 x = int(raw_input(Please enter an integer: )) Please enter an integer: 42 if x 0: ... x = 0 ... print 'Negative changed to zero' ... elif x == 0: ... print 'Zero' ... elif x == 1: ... print 'Single' ... else: ... print 'More' ... More 12 SyntaxError: invalid syntax ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] first call - newcomer
This is my first post - I will be brief... One: I have programmed before - but it has been DECADES...so just a few simple queries: 1. A brief (but complete) description regarding the use of script editor (I will be using command prompt in Windows), as: a. details about loading and saving programs (not in that order) and little specs about pathnames or other requirements (I will probably store all my little goodies in one folder or space). That should get me going ... a book and manual by my side should suffice for the rest - - - except for one thing: 2. I have been unable to locate the gizmo in the literature to get ascii codes in python. In the old days, it was a list of 256 (or so) characters that represented all keyboard symbols (A equalled 36; B equalled 37; et cetera). To assign a value, you used Let A$ = ASC (36) where A$ was a variable and 36 was the ASCII value for 'A'. I believe the reverse of this process was PRINT VAL(A$) or something. I want to play with a program that will assign a number to a word (using a simple algorhythm that will give a specific number to every word). Other stuff is pretty easy to find with the book and on-line literature. I will need to get an ascii code out of a string (whose content is not known to the programmer, as raw_input). Then to assign, I will need the actual list with assigned numbers. You will be giving me probably the only boost I will need! I will be available later on, if I want to take part in the ask/answer system here. Thanks a lot, Anthony 8:27 pm PST October 4th, 2008 _ Get more out of the Web. Learn 10 hidden secrets of Windows Live. http://windowslive.com/connect/post/jamiethomson.spaces.live.com-Blog-cns!550F681DAD532637!5295.entry?ocid=TXT_TAGLM_WL_domore_092008___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Tutor Digest, Vol 56, Issue 22
On Mon, 2008-10-06 at 05:32 +0200, [EMAIL PROTECTED] wrote: Message: 8 Date: Sun, 5 Oct 2008 20:27:39 -0700 From: Anthony Smith [EMAIL PROTECTED] Subject: [Tutor] first call - newcomer To: tutor@python.org Message-ID: [EMAIL PROTECTED] Content-Type: text/plain; charset=iso-8859-1 This is my first post - I will be brief... One: I have programmed before - but it has been DECADES...so just a few simple queries: 1. A brief (but complete) description regarding the use of script editor (I will be using Your script editor can be any plain text-editing tools, Notepad could do. command prompt in Windows), as: a. details about loading and saving programs (not in that order) and little specs about pathnames or other requirements (I will probably store all my little goodies in one folder or space). Pathnames is free, you can name your program anything your OS allows for a file. A convention is to name the script ending with .py/.pyw extension (command-line script/GUI script), although python doesn't complain if it is not in those extension (in Windows, the extension is associated with the interpreter). Calling a program from command line is done like this: python filename.py That should get me going ... a book and manual by my side should suffice for the rest - - - except for one thing: 2. I have been unable to locate the gizmo in the literature to get ascii codes in python. In the old days, it was a list of 256 (or so) characters that represented all keyboard symbols (A equalled 36; B equalled 37; et cetera). To assign a value, you used Let A$ = ASC (36) where A$ was a variable and 36 was the ASCII value for 'A'. I believe the reverse of this process was PRINT VAL(A$) or something. I want to play with a program that will assign a number to a word (using a simple algorhythm that will give a specific number to every word). Other stuff is pretty easy to find with the book and on-line literature. I will need to get an ascii code out of a string (whose content is not known to the programmer, as raw_input). Then to assign, I will need the actual list with assigned numbers. a = ord('A') b = chr(36) -- read on the help file: Built-in Functions You will be giving me probably the only boost I will need! I will be available later on, if I want to take part in the ask/answer system here. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor