[Tutor] The better Python approach
Good Morning, Given a string consisting of numbers separated by spaces such as '1234 5678 1 233 476'. I can see I have two obvious choices to extract or parse out the numbers. The first relying on iteration so that as I search for a blank, I build a substring of all characters found before the space and then, once the space is found, I can then use the int(n) function to determine the number. From my C++ background, that is the approach that seems not only most natural but also most efficient..butthe rules of Python are different and I easily see that I can also search for the first blank, then using the character count, I can use the slice operation to get the characters. Of even further interest I see a string built-in function called split which, I think, will return all the distinct character sub strings for me. My question is what is the most correct python oriented solution for extracting those substrings? Thanks, Robert Berman ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] The better Python approach
a = '1234 5678 1 233 476' a.split() ['1234', '5678', '1', '233', '476'] Where the '' are the command prompt from python. Don't type those. A space is the default split delimiter. If you wish to use a '-' or new line feed them as strings to the split method. John On Wed, 2009-01-21 at 08:33 -0500, Robert Berman wrote: Good Morning, Given a string consisting of numbers separated by spaces such as '1234 5678 1 233 476'. I can see I have two obvious choices to extract or parse out the numbers. The first relying on iteration so that as I search for a blank, I build a substring of all characters found before the space and then, once the space is found, I can then use the int(n) function to determine the number. From my C++ background, that is the approach that seems not only most natural but also most efficient..butthe rules of Python are different and I easily see that I can also search for the first blank, then using the character count, I can use the slice operation to get the characters. Of even further interest I see a string built-in function called split which, I think, will return all the distinct character sub strings for me. My question is what is the most correct python oriented solution for extracting those substrings? Thanks, Robert Berman ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] The better Python approach
Robert Berman wrote: Given a string consisting of numbers separated by spaces such as '1234 5678 1 233 476'. I can see I have two obvious choices to extract or parse out the numbers. The first relying on iteration so that as I search for a blank, I build a substring of all characters found before the space and then, once the space is found, I can then use the int(n) function to determine the number. From my C++ background, that is the approach that seems not only most natural but also most efficient..butthe rules of Python are different and I easily see that I can also search for the first blank, then using the character count, I can use the slice operation to get the characters. Of even further interest I see a string built-in function called split which, I think, will return all the distinct character sub strings for me. My question is what is the most correct python oriented solution for extracting those substrings? Correct? I don't know. Working; try this: code s = '1234 5678 1 233 476' nums = [int (i) for i in s.split ()] print nums /code If you had some more sophisticated need (he says, inventing requirements as he goes along) such as pulling the numbers out of a string of mixed numbers and letters, you could use a regular expression: code import re s = '1234 abc 5678 *** 1 233 xyz 476' nums = [int (i) for i in re.findall (\d+, s)] print nums /code TJG ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] The better Python approach
Thanks to everyone who responded. Perhaps i should not have said the most Python correct. It looks as if it may well be the approach using the least amount of work the interpreter must complete. At that point of speculation I am well out of my league. I will be using the split method, and I thank everyone who replied. Robert Berman Tim Golden wrote: Robert Berman wrote: Given a string consisting of numbers separated by spaces such as '1234 5678 1 233 476'. I can see I have two obvious choices to extract or parse out the numbers. The first relying on iteration so that as I search for a blank, I build a substring of all characters found before the space and then, once the space is found, I can then use the int(n) function to determine the number. >From my C++ background, that is the approach that seems not only most natural but also most efficient..butthe rules of Python are different and I easily see that I can also search for the first blank, then using the character count, I can use the slice operation to get the characters. Of even further interest I see a string built-in function called split which, I think, will return all the distinct character sub strings for me. My question is what is the most correct python oriented solution for extracting those substrings? Correct? I don't know. Working; try this: code s = '1234 5678 1 233 476' nums = [int (i) for i in s.split ()] print nums /code If you had some more sophisticated need (he says, inventing requirements as he goes along) such as pulling the numbers out of a string of mixed numbers and letters, you could use a regular _expression_: code import re s = '1234 abc 5678 *** 1 233 xyz 476' nums = [int (i) for i in re.findall ("\d+", s)] print nums /code TJG ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] The better Python approach
jadrifter wrote: a = '1234 5678 1 233 476' a.split() ['1234', '5678', '1', '233', '476'] Where the '' are the command prompt from python. Don't type those. A space is the default split delimiter. If you wish to use a '-' or new line feed them as strings to the split method. Ok, that's it. That was the last straw. I'm digging up my copy of Dive into Python and starting today! ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] The better Python approach
Robert Berman berma...@cfl.rr.com wrote Perhaps i should not have said the most Python correct. It looks as if it may well be the approach using the least amount of work the interpreter must complete. That's generally true. Python can always do things the long way but its generally more efficient both in programmer time and performance speed to use the built-in functions/methods as much as possible. Most of them are written in C after all! Alan G ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] The better Python approach
Am Wed, 21 Jan 2009 05:40:00 -0800 schrieb jadrifter jadrif...@gmail.com: a = '1234 5678 1 233 476' a.split() ['1234', '5678', '1', '233', '476'] [int(x) for x in a.split()] # [1234, 5678, 1, 233, 476] Andreas ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Finding the shortest word in a list of words
Am Tue, 20 Jan 2009 09:33:40 -0600 schrieb Paul McGuire pt...@austin.rr.com: No need for a defaultdict, all dicts have a setdefault method that works fine for this assign an empty dict/list as starting point problem: wordlendict = {} for w in words: wordlendict.setdefault(len(w), []).append(w) try: minlen, minlist = min(wordlendict.items()) except ValueError: minlen = 0 minlist = [] Andreas from collections import defaultdict wordlendict = defaultdict(list) for w in words: wordlendict[len(w)].append(w) minlen = min(wordlendict.keys()) minlist = wordlendict[minlen] print minlist prints: ['he', 'me'] Now we are getting a more complete answer! A second approach uses the groupby method of the itertools module in the stdlib. groupby usually takes me several attempts to get everything right: the input must be sorted by the grouping feature, and then the results returned by groupby are in the form of an iterator that returns key-iterator tuples. I need to go through some mental gymnastics to unwind the data to get the group I'm really interested in. Here is a step-by-step code to use groupby: from itertools import groupby grpsbylen = groupby(sorted(words,key=len),len) mingrp = grpsbylen.next() minlen = mingrp[0] minlist = list(mingrp[1]) print minlist The input list of words gets sorted, and then passed to groupby, which uses len as the grouping criteria (groupby is not inherently aware of how the input list was sorted, so len must be repeated). The first group tuple is pulled from the grpsbylen iterator using next(). The 0'th tuple element is the grouping value - in this case, it is the minimum length 2. The 1'th tuple element is the group itself, given as an iterator. Passing this to the list constructor gives us the list of all the 2-character words, which then gets printed: ['he', 'me'] Again, 'me' no longer gets left out. Maybe this will get you some extra credit... -- Paul ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Tutor Digest, Vol 59, Issue 109
of Python are different and I easily see that I can also search for the first blank, then using the character count, I can use the slice operation to get the characters. Of even further interest I see a string built-in function called split which, I think, will return all the distinct character sub strings for me. My question is what is the most correct python oriented solution for extracting those substrings? Correct? I don't know. Working; try this: code s = '1234 5678 1 233 476' nums = [int (i) for i in s.split ()] print nums /code If you had some more sophisticated need (he says, inventing requirements as he goes along) such as pulling the numbers out of a string of mixed numbers and letters, you could use a regular expression: code import re s = '1234 abc 5678 *** 1 233 xyz 476' nums = [int (i) for i in re.findall (\d+, s)] print nums /code TJG -- Message: 4 Date: Wed, 21 Jan 2009 08:50:29 -0500 From: Robert Berman berma...@cfl.rr.com Subject: Re: [Tutor] The better Python approach To: Tutor@python.org Message-ID: 49772825.5060...@cfl.rr.com Content-Type: text/plain; charset=us-ascii An HTML attachment was scrubbed... URL: http://mail.python.org/pipermail/tutor/attachments/20090121/a0b41acf/attach ment-0001.htm -- Message: 5 Date: Wed, 21 Jan 2009 08:59:08 -0500 From: Vince Teachout tea...@taconic.net Subject: Re: [Tutor] The better Python approach To: Tutor@python.org Message-ID: 49772a2c.6010...@taconic.net Content-Type: text/plain; charset=ISO-8859-1; format=flowed jadrifter wrote: a = '1234 5678 1 233 476' a.split() ['1234', '5678', '1', '233', '476'] Where the '' are the command prompt from python. Don't type those. A space is the default split delimiter. If you wish to use a '-' or new line feed them as strings to the split method. Ok, that's it. That was the last straw. I'm digging up my copy of Dive into Python and starting today! -- Message: 6 Date: Wed, 21 Jan 2009 14:44:18 - From: Alan Gauld alan.ga...@btinternet.com Subject: Re: [Tutor] The better Python approach To: tutor@python.org Message-ID: gl7ccd$5a...@ger.gmane.org Content-Type: text/plain; format=flowed; charset=iso-8859-1; reply-type=original Robert Berman berma...@cfl.rr.com wrote Perhaps i should not have said the most Python correct. It looks as if it may well be the approach using the least amount of work the interpreter must complete. That's generally true. Python can always do things the long way but its generally more efficient both in programmer time and performance speed to use the built-in functions/methods as much as possible. Most of them are written in C after all! Alan G -- Message: 7 Date: Wed, 21 Jan 2009 18:29:03 +0100 From: Andreas Kostyrka andr...@kostyrka.org Subject: Re: [Tutor] The better Python approach To: jadrif...@gmail.com Cc: Tutor@python.org Message-ID: 20090121182903.0602a...@andi-lap Content-Type: text/plain; charset=US-ASCII Am Wed, 21 Jan 2009 05:40:00 -0800 schrieb jadrifter jadrif...@gmail.com: a = '1234 5678 1 233 476' a.split() ['1234', '5678', '1', '233', '476'] [int(x) for x in a.split()] # [1234, 5678, 1, 233, 476] Andreas -- ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor End of Tutor Digest, Vol 59, Issue 109 ** ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] The better Python approach
Alan, Thank you for the clarification. Using that as my guide, I revamped my solution to this small challenge and attempted to make the script as concise as possible. The challenge is at the Challenge-You web page, http://www.challenge-you.com/challenge?id=61 I am relatively certain I could have made it more concise with some more detailed examination. If there are some obvious glaring deficiencies, please, anyone feel free to comment. Thanks, Robert Berman #!/usr/bin/env python #findsum.py import string def openinput(thepath = '/home/bermanrl/usbdirbig/Challenges/sum.txt'): ''' Opens text file ''' try: infile = open(thepath, 'r') except: print 'Open file failure.' return infile def runprocess(): '''Reads file and passes string to parsing function ''' bigtotal = 0 myfile = openinput() for line in myfile: jlist = line.split() for x in jlist: bigtotal += int(x) print bigtotal return if __name__ == '__main__': runprocess() Alan Gauld wrote: Robert Berman berma...@cfl.rr.com wrote Perhaps i should not have said the most Python correct. It looks as if it may well be the approach using the least amount of work the interpreter must complete. That's generally true. Python can always do things the long way but its generally more efficient both in programmer time and performance speed to use the built-in functions/methods as much as possible. Most of them are written in C after all! Alan G ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] The better Python approach
On Wed, Jan 21, 2009 at 2:02 PM, Robert Berman berma...@cfl.rr.com wrote: myfile = openinput() for line in myfile: jlist = line.split() for x in jlist: bigtotal += int(x) Python has a sum() function that sums the elements of a numeric sequence, so the inner loop can be written as bigtotal += sum(int(x) for x in line.split()) But this is just summing another sequence - the line sums - so the whole thing can be written as bigtotal = sum(sum(int(x) for x in line.split()) for line in myfile) or more simply as a single sum over a double loop: bigtotal = sum(int(x) for line in myfile for x in line.split()) which you may or may not see as an improvement... Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] The better Python approach
Kent, Wow! That is all worth knowing. I am fascinated by the single sum over a double loop. Thanks, Robert Berman Kent Johnson wrote: On Wed, Jan 21, 2009 at 2:02 PM, Robert Berman berma...@cfl.rr.com wrote: myfile = openinput() for line in myfile: jlist = line.split() for x in jlist: bigtotal += int(x) Python has a sum() function that sums the elements of a numeric sequence, so the inner loop can be written as bigtotal += sum(int(x) for x in line.split()) But this is just summing another sequence - the line sums - so the whole thing can be written as bigtotal = sum(sum(int(x) for x in line.split()) for line in myfile) or more simply as a single sum over a double loop: bigtotal = sum(int(x) for line in myfile for x in line.split()) which you may or may not see as an improvement... Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] bruteforce match word in text file
I have to ask for a pointer, not sure what I am doing wrong. thanks -david #!/usr/bin/python password = 'loser' wordlist = '/home/david/Challenge-You/wordlist.txt' try: words = open(wordlist, 'r').readlines() except IOError, e: print Sorry no words for word in words: word = word.replace(\n,) if password in word: print word else: print 'You are a loser' -- Powered by Gentoo GNU/LINUX http://www.linuxcrazy.com pgp.mit.edu ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] bruteforce match word in text file
David wrote: I have to ask for a pointer, not sure what I am doing wrong. The first thing you are doing wrong is failing to tell us what is in the wordlist file and what results you get when you run the program. Please re-post with that information. #!/usr/bin/python password = 'loser' wordlist = '/home/david/Challenge-You/wordlist.txt' try: words = open(wordlist, 'r').readlines() except IOError, e: print Sorry no words for word in words: word = word.replace(\n,) if password in word: print word else: print 'You are a loser' -- Bob Gailer Chapel Hill NC 919-636-4239 ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] bruteforce match word in text file
bob gailer wrote: David wrote: I have to ask for a pointer, not sure what I am doing wrong. The first thing you are doing wrong is failing to tell us what is in the wordlist file and what results you get when you run the program. Please re-post with that information. #!/usr/bin/python password = 'loser' wordlist = '/home/david/Challenge-You/wordlist.txt' try: words = open(wordlist, 'r').readlines() except IOError, e: print Sorry no words for word in words: word = word.replace(\n,) if password in word: print word else: print 'You are a loser' wordlist.txt next block is the meat the script will go through the list of words create our form with information encode that form and then apply that loser get source added comments above each line to help you understand results; ./py_bruteforce.py next block is the meat the script will go through the list of words create our form with information encode that form and then apply that loser get source added comments above each line to help you understand You are a loser -- Powered by Gentoo GNU/LINUX http://www.linuxcrazy.com pgp.mit.edu ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] bruteforce match word in text file
David wrote: bob gailer wrote: David wrote: I have to ask for a pointer, not sure what I am doing wrong. The first thing you are doing wrong is failing to tell us what is in the wordlist file and what results you get when you run the program. Please re-post with that information. #!/usr/bin/python password = 'loser' wordlist = '/home/david/Challenge-You/wordlist.txt' try: words = open(wordlist, 'r').readlines() except IOError, e: print Sorry no words for word in words: word = word.replace(\n,) if password in word: print word else: print 'You are a loser' wordlist.txt next block is the meat the script will go through the list of words create our form with information encode that form and then apply that loser get source added comments above each line to help you understand results; ./py_bruteforce.py next block is the meat the script will go through the list of words create our form with information encode that form and then apply that loser get source added comments above each line to help you understand You are a loser Thank you. Please always post the relevant information, and if you get an exception, post the full traceback. It looks like the file contains 1 line. So the for statement will put that line in word. 'loser' is in word so the print statement prints the line. The program is working as written. Now what can you change to get it to do what (I assume) you want - examine each word in the file, print the word if 'loser' is in it, and print 'You are a loser' ONLY if no words match. -- Bob Gailer Chapel Hill NC 919-636-4239 ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] The better Python approach
Robert Berman berma...@cfl.rr.com wrote Wow! That is all worth knowing. I am fascinated by the single sum over a double loop. or more simply as a single sum over a double loop: bigtotal = sum(int(x) for line in myfile for x in line.split()) which you may or may not see as an improvement... And that nicely illustrates the caveat with using the most concise form. You have to make a judgement about when it becomes too concise to be readable and therefore maintainable. Personally I prefer the double loop to the double sum, but in practice I'd probably have been happy to stick with the outer for loop and only one sum() call inside that. But that uis where taste comes into programming, there is no absolute correct form - which takes us back to your original post! :-) -- Alan Gauld Author of the Learn to Program web site http://www.freenetpages.co.uk/hp/alan.gauld ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] bruteforce match word in text file
bob gailer wrote: David wrote: bob gailer wrote: David wrote: I have to ask for a pointer, not sure what I am doing wrong. The first thing you are doing wrong is failing to tell us what is in the wordlist file and what results you get when you run the program. Please re-post with that information. #!/usr/bin/python password = 'loser' wordlist = '/home/david/Challenge-You/wordlist.txt' try: words = open(wordlist, 'r').readlines() except IOError, e: print Sorry no words for word in words: word = word.replace(\n,) if password in word: print word else: print 'You are a loser' Now what can you change to get it to do what (I assume) you want - examine each word in the file, print the word if 'loser' is in it, and print 'You are a loser' ONLY if no words match. Thanks Bob, I changed the wordlist.txt to next block is meat snip and the program; #!/usr/bin/python import re password = 'loser' wordlist = '/home/david/Challenge-You/wordlist.txt' try: words = open(wordlist, 'r').readlines() except IOError, e: print Sorry no words for word in words: word = word.replace(\n,) if password in word: print The password is: , word else: pass I could not figure out how to split the file into words. -- Powered by Gentoo GNU/LINUX http://www.linuxcrazy.com pgp.mit.edu ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] bruteforce match word in text file
#!/usr/bin/python import re password = 'loser' wordlist = '/home/david/Challenge-You/wordlist.txt' try: words = open(wordlist, 'r').readlines() except IOError, e: print Sorry no words for word in words: word = word.replace(\n,) if password in word: print The password is: , word else: pass I could not figure out how to split the file into words. I removed the import re as that was left over from my experimenting. -- Powered by Gentoo GNU/LINUX http://www.linuxcrazy.com pgp.mit.edu ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] bruteforce match word in text file
David wrote: Thanks Bob, I changed the wordlist.txt to next block is meat snip and the program; #!/usr/bin/python password = 'loser' wordlist = '/home/david/Challenge-You/wordlist.txt' try: words = open(wordlist, 'r').readlines() except IOError, e: print Sorry no words for word in words: word = word.replace(\n,) if password in word: print The password is: , word else: pass Good work! I could not figure out how to split the file into words. look up the string method split. Also take a look at the syntax of the for statement and notice it has an else clause. -- Bob Gailer Chapel Hill NC 919-636-4239 ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Volunteer Opportunities?
-- eric dorsey | www.perfecteyedesign.com ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Volunteer opportunities
(My apologies if a blank message comes up first with this heading -- The first time I meant to type this I managed to tab accidently to Send and hit it before even filling out the message.. :/ ) Does anyone know of any good volunteer opportunities for projects that learning Python coders can work on? Or possibly nonprofits or the like that need smaller-type applications worked on? Much thanks! -- eric dorsey | www.perfecteyedesign.com ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Volunteer opportunities
Eric Dorsey wrote: Does anyone know of any good volunteer opportunities for projects that learning Python coders can work on? Or possibly nonprofits or the like that need smaller-type applications worked on? I have an open source project that could use some help. See http://en.wikipedia.org/wiki/Python_Pipelines for a brief description. Let me know if you want to know more. -- Bob Gailer Chapel Hill NC 919-636-4239 ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] dict() versus {}
When creating a list of dictionaries through a loop, I ran into a strange issue. I'll let the code talk: l = 'i am a special new list'.split() t = [] for thing in l: ... t.append({thing: 1}) ... t [{'i': 1}, {'am': 1}, {'a': 1}, {'special': 1}, {'new': 1}, {'list': 1}] This is what I expected. {} says to make a dictionary. Thing, not being quoted, is clearing a variable, which needs to be evaluated and used as the key. t = [] for thing in l: ... t.append(dict(thing=1)) ... t [{'thing': 1}, {'thing': 1}, {'thing': 1}, {'thing': 1}, {'thing': 1}, {'thing': 1}] This was what threw me. Why would the dict() function not evaluate thing? How can it take it as a literal string without quotes? Thanks for any insight, Sam ___ Samuel Huckins Homepage - http://samuelhuckins.com Tech blog - http://dancingpenguinsoflight.com/ Photos - http://www.flickr.com/photos/samuelhuckins/ AIM - samushack | Gtalk - samushack | Skype - shuckins ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] dict() versus {}
wormwood_3 wrote: When creating a list of dictionaries through a loop, I ran into a strange issue. I'll let the code talk: l = 'i am a special new list'.split() t = [] for thing in l: ... t.append({thing: 1}) ... t [{'i': 1}, {'am': 1}, {'a': 1}, {'special': 1}, {'new': 1}, {'list': 1}] This is what I expected. {} says to make a dictionary. Thing, not being quoted, is clearing a variable, which needs to be evaluated and used as the key. t = [] for thing in l: ... t.append(dict(thing=1)) ... t [{'thing': 1}, {'thing': 1}, {'thing': 1}, {'thing': 1}, {'thing': 1}, {'thing': 1}] This was what threw me. Why would the dict() function not evaluate thing? How can it take it as a literal string without quotes? I suggest you look dict up in the Python documentation. There it shows the result you got as an example. When in doubt read the manual. -- Bob Gailer Chapel Hill NC 919-636-4239 ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] dict() versus {}
Hmm, looked through the latest docs, in the sections on dictionary types, don't see examples that point to this case well. Can you link to what you had in mind? ___ Samuel Huckins Homepage - http://samuelhuckins.com Tech blog - http://dancingpenguinsoflight.com/ Photos - http://www.flickr.com/photos/samuelhuckins/ AIM - samushack | Gtalk - samushack | Skype - shuckins From: bob gailer bgai...@gmail.com To: wormwood_3 wormwoo...@yahoo.com Cc: Python Tutorlist tutor@python.org Sent: Wednesday, January 21, 2009 11:02:35 PM Subject: Re: [Tutor] dict() versus {} wormwood_3 wrote: When creating a list of dictionaries through a loop, I ran into a strange issue. I'll let the code talk: l = 'i am a special new list'.split() t = [] for thing in l: ... t.append({thing: 1}) ... t [{'i': 1}, {'am': 1}, {'a': 1}, {'special': 1}, {'new': 1}, {'list': 1}] This is what I expected. {} says to make a dictionary. Thing, not being quoted, is clearing a variable, which needs to be evaluated and used as the key. t = [] for thing in l: ... t.append(dict(thing=1)) ... t [{'thing': 1}, {'thing': 1}, {'thing': 1}, {'thing': 1}, {'thing': 1}, {'thing': 1}] This was what threw me. Why would the dict() function not evaluate thing? How can it take it as a literal string without quotes? I suggest you look dict up in the Python documentation. There it shows the result you got as an example. When in doubt read the manual. -- Bob Gailer Chapel Hill NC 919-636-4239___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] dict() versus {}
wormwood_3 wrote: Hmm, looked through the latest docs, in the sections on dictionary types, don't see examples that point to this case well. Can you link to what you had in mind? 2.1 Built-in Functions ... dict( [mapping-or-sequence]) ... these all return a dictionary equal to {"one": 2, "two": 3}: ... dict( two=3) From: bob gailer bgai...@gmail.com To: wormwood_3 wormwoo...@yahoo.com Cc: Python Tutorlist tutor@python.org Sent: Wednesday, January 21, 2009 11:02:35 PM Subject: Re: [Tutor] dict() versus {} wormwood_3 wrote: When creating a list of dictionaries through a loop, I ran into a strange issue. I'll let the code talk: l = 'i am a special new list'.split() t = [] for thing in l: ... t.append({thing: 1}) ... t [{'i': 1}, {'am': 1}, {'a': 1}, {'special': 1}, {'new': 1}, {'list': 1}] This is what I expected. {} says to make a dictionary. Thing, not being quoted, is clearing a variable, which needs to be evaluated and used as the key. t = [] for thing in l: ... t.append(dict(thing=1)) ... t [{'thing': 1}, {'thing': 1}, {'thing': 1}, {'thing': 1}, {'thing': 1}, {'thing': 1}] This was what threw me. Why would the dict() function not evaluate thing? How can it take it as a literal string without quotes? I suggest you look dict up in the Python documentation. There it shows the result you got as an example. When in doubt read the manual. -- Bob Gailer Chapel Hill NC 919-636-4239 ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor -- Bob Gailer Chapel Hill NC 919-636-4239 ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Volunteer opportunities
jadrifter wrote: On Wed, 2009-01-21 at 22:53 -0500, bob gailer wrote: Eric Dorsey wrote: Does anyone know of any good volunteer opportunities for projects that learning Python coders can work on? Or possibly nonprofits or the like that need smaller-type applications worked on? I have an open source project that could use some help. See http://en.wikipedia.org/wiki/Python_Pipelines for a brief description. Let me know if you want to know more. Bob, Your project looks interesting but I wonder what kind of help you're looking for. Depends on your knowledge of Python and (if any) CMS Pipelines. Testing Design critique (devil's advocate) Cleaning up and fine-tuning the parser Coding built-in stages Documentation Financial support Let me know what sparks your interest. And say a bit about your background and why this interests you. -- Bob Gailer Chapel Hill NC 919-636-4239 ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] dict() versus {}
dict( [arg]) Return a new dictionary initialized from an optional positional argument or from a set of keyword arguments. If no arguments are given, return a new empty dictionary. If the positional argument arg is a mapping object, return a dictionary mapping the same keys to the same values as does the mapping object. But why doesn't the optional positional argument arg in this case, not being a mapping type, get evaluated?: dict(thing=1) And even if it makes sense for it not to be evaluated, wouldn't it be better for dict() to complain that it didn't get a string or an int as it expects for a keyword argument? Maybe I am missing the use case, so far it just seems strange to force the keyword to a string. -Sam From: bob gailer bgai...@gmail.com To: wormwood_3 wormwoo...@yahoo.com Cc: Python Tutorlist tutor@python.org Sent: Wednesday, January 21, 2009 11:25:12 PM Subject: Re: [Tutor] dict() versus {} wormwood_3 wrote: Hmm, looked through the latest docs, in the sections on dictionary types, don't see examples that point to this case well. Can you link to what you had in mind? 2.1 Built-in Functions ... dict( [mapping-or-sequence]) ... these all return a dictionary equal to {one: 2, two: 3}: ... dict(one=2, two=3) From: bob gailer bgai...@gmail.com To: wormwood_3 wormwoo...@yahoo.com Cc: Python Tutorlist tutor@python.org Sent: Wednesday, January 21, 2009 11:02:35 PM Subject: Re: [Tutor] dict() versus {} wormwood_3 wrote: When creating a list of dictionaries through a loop, I ran into a strange issue. I'll let the code talk: l = 'i am a special new list'.split() t = [] for thing in l: ... t.append({thing: 1}) ... t [{'i': 1}, {'am': 1}, {'a': 1}, {'special': 1}, {'new': 1}, {'list': 1}] This is what I expected. {} says to make a dictionary. Thing, not being quoted, is clearing a variable, which needs to be evaluated and used as the key. t = [] for thing in l: ... t.append(dict(thing=1)) ... t [{'thing': 1}, {'thing': 1}, {'thing': 1}, {'thing': 1}, {'thing': 1}, {'thing': 1}] This was what threw me. Why would the dict() function not evaluate thing? How can it take it as a literal string without quotes? I suggest you look dict up in the Python documentation. There it shows the result you got as an example. When in doubt read the manual. -- Bob Gailer Chapel Hill NC 919-636-4239 ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor -- Bob Gailer Chapel Hill NC 919-636-4239___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Volunteer opportunities
On Wed, 2009-01-21 at 23:31 -0500, bob gailer wrote: Depends on your knowledge of Python and (if any) CMS Pipelines. Testing Design critique (devil's advocate) Cleaning up and fine-tuning the parser Coding built-in stages Documentation Financial support Let me know what sparks your interest. And say a bit about your background and why this interests you. -- Bob Gailer Chapel Hill NC 919-636-4239 Bob, Honestly the first thing that caught my eye was that you were in Chapel Hill. I grew up in Durham NC. I'm in Tacoma WA now. I'm a disabled vet who got into computers and programming from an accounting career. Most of what I've done has been database oriented. I started playing with Python a few years ago and just enjoy the language and would like to find some practical application to use it with. I'm more of a consumer than producer on the tutor list. I read a lot, mostly technical stuff, but nothing that complex. I took a peek at the 889 manual for Pipelines from the wiki page you linked to. It seems to assume a LOT of knowledge I don't have so I'm not sure I'm suitable at all for the project. John Purser ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] dict() versus {}
This: dict(one=1,two=2,three=3) {'three': 3, 'two': 2, 'one': 1} Is a shortcut for the longer: dict((('one',1),('two',2),('three',3))) {'three': 3, 'two': 2, 'one': 1} and given how this works: def function(**kwargs): ... print kwargs ... function(one=1,two=2,three=3) {'three': 3, 'two': 2, 'one': 1} One can guess how the shortcut is implemented. -Mark wormwood_3 wormwoo...@yahoo.com wrote in message news:454349.94041...@web110811.mail.gq1.yahoo.com... dict( [arg]) Return a new dictionary initialized from an optional positional argument or from a set of keyword arguments. If no arguments are given, return a new empty dictionary. If the positional argument arg is a mapping object, return a dictionary mapping the same keys to the same values as does the mapping object. But why doesn't the optional positional argument arg in this case, not being a mapping type, get evaluated?: dict(thing=1) And even if it makes sense for it not to be evaluated, wouldn't it be better for dict() to complain that it didn't get a string or an int as it expects for a keyword argument? Maybe I am missing the use case, so far it just seems strange to force the keyword to a string. -Sam -- From: bob gailer bgai...@g mail.com To: wormwood_3 wormwoo...@yahoo.com Cc: Python Tutorlist tutor@python.org Sent: Wednesday, January 21, 2009 11:25:12 PM Subject: Re: [Tutor] dict() versus {} wormwood_3 wrote: Hmm, looked through the latest docs, in the sections on dictionary types, don't see examples that point to this case well. Can you link to what you had in mind? 2.1 Built-in Functions ... dict( [mapping-or-sequence]) ... these all return a dictionary equal to {one: 2, two: 3}: ... dict(one=2, two=3) From: bob gailer bgai...@gmail.com To: wormwood_3 wormwoo...@yahoo.com Cc: Python Tutorlist tutor@python.org Sent: Wednesday, January 21, 2009 11:02:35 PM Subject: Re: [Tutor] dict() versus {} wormwood_3 wrote: When creating a list of dictionaries through a loop, I ran into a strange issue. I'll let the code talk: l = 'i am a special new list'.split() t = [] for thing in l: ... t.append({thing: 1}) ... t [{'i': 1}, {'am': 1}, {'a': 1}, {'special': 1}, {'new': 1}, {'list': 1}] This is what I expected. {} says to make a dictionary. Thing, not being quoted, is clearing a variable, which needs to be evaluated and used as the key. t = [] for thing in l: ... t.append(dict(thing=1)) ... t [{'thing': 1}, {'thing': 1}, {'thing': 1}, {'thing': 1}, {'thing': 1}, {'thing': 1}] This was what threw me. Why would the dict() function not evaluate thing? How can it take it as a literal string without quotes? I suggest you look dict up in the Python documentation. There it shows the result you got as an example. When in doubt read the manual. -- Bob Gailer Chapel Hill NC 919-636-4239 ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor -- Bob Gailer Chapel Hill NC 919-636-4239 -- ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor