Re: [Tutor] help on dic creation
On 31/10/2012 01:01, Brayden Zhao wrote: def fieldict(filename): D={} with open(filename) as FileObject: for lines in FileObject: linelist=lines.split('\t') Key=linelist[0] ValCity=(linelist[12]).strip() ValState=linelist[13] ValOne=linelist[2] ValTwo=linelist[6] ValThree=boolean(linelist[7]) D={Key:(ValOne, ValTwo, ValThree, ValCity,ValState)} Put the line above inside the for loop :) return D print fieldict("DOT500.txt") -- Cheers. Mark Lawrence. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] help on dic creation
On 11/06/2012 09:01 AM, Mark Lawrence wrote: > On 31/10/2012 01:01, Brayden Zhao wrote: > >> def fieldict(filename): >>D={} >>with open(filename) as FileObject: >> for lines in FileObject: >>linelist=lines.split('\t') >>Key=linelist[0] >>ValCity=(linelist[12]).strip() >>ValState=linelist[13] >>ValOne=linelist[2] >>ValTwo=linelist[6] >>ValThree=boolean(linelist[7]) >>D={Key:(ValOne, ValTwo, ValThree, ValCity,ValState)} > > Put the line above inside the for loop :) > Won't help. Then he'd be reassigning the D each time, with the same net result. What he needs INSIDE the loop is something like: D[Key] = (ValOne, ValTwo, ValThree, ValCity, ValState) >>return D >> print fieldict("DOT500.txt") >> > -- DaveA ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] how to keep track of sorted lists
> On 11/03/2012 10:40 AM, Albert-Jan Roskam wrote: >>> On 11/03/2012 09:04 AM, Albert-Jan Roskam wrote: >> Hello, >>> >>> (I haven't run the code, as it was not presented in a form that I > could >>> do a single copy/paste. So I may have missed some subtlety in the > code.) >> >> Hi, sorry about that. Here's a copy/pastable version. I also added a > 'data' parameter as my original code was too synthetic in this respect. >> The more realistically, the data come from some getter method. >> >> import bisect >> class TestOne(object): >> def __init__(self, data, param="x"): >> self.param = param >> self.data = data # <-- NOTE: this would in reality be a > getter method of some sort >> def get(self, key, default=None): >> sorted_ = "sorted_" + self.param >> if not hasattr(self, sorted_): >> setattr(self, sorted_, sorted(self.data)) >> return bisect.bisect_right(getattr(self, sorted_), x=key) >> >> t = TestOne(range(10, 1, -1), "x") >> t.get(1) >> >> class TestTwo(object): >> def __init__(self, data, param="x"): >> self.param = param >> self.data = range(10, 1, -1) >> def get(self, key, default=None): >> k = "sorted_" + self.param >> if not hasattr(self, "sorted_"): >> setattr(self, "sorted_", {k: sorted(self.data)}) >> return bisect.bisect_right(getattr(self, "sorted_")[k], > x=key) >> t = TestTwo(range(10, 1, -1), "x") >> t.get(1) >> >> >> return bisect.bisect_right(getattr(self, sorted_), x=key) >>> >>> Why have multiple copies of the sorted data, when there's only one > list? >>> I was already half way writing a reply when I (finally!) realized that you are absolutely right! Maybe it's because I also considered using 'param' as a parameter of get() instead of __init__(). The code is needlessly complicated indeed, and mentioning bisect was distracting (and indeed, the method was an index() method --I believe it was Steven who pointed that out). Thanks you all. Cheers, Albert-Jan (with occasionally fuzzy cortex ;-) ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] help on dic creation
On 06/11/2012 14:16, Dave Angel wrote: On 11/06/2012 09:01 AM, Mark Lawrence wrote: On 31/10/2012 01:01, Brayden Zhao wrote: def fieldict(filename): D={} with open(filename) as FileObject: for lines in FileObject: linelist=lines.split('\t') Key=linelist[0] ValCity=(linelist[12]).strip() ValState=linelist[13] ValOne=linelist[2] ValTwo=linelist[6] ValThree=boolean(linelist[7]) D={Key:(ValOne, ValTwo, ValThree, ValCity,ValState)} Put the line above inside the for loop :) Won't help. Then he'd be reassigning the D each time, with the same net result. What he needs INSIDE the loop is something like: D[Key] = (ValOne, ValTwo, ValThree, ValCity, ValState) return D print fieldict("DOT500.txt") Thanks for the correction, that'll teach me to pay attention :( -- Cheers. Mark Lawrence. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] why different result from two similar ways
Steven D'Aprano wrote: > On 30/10/12 12:36, Frank Pontius wrote: > > Hello, > > I have code that works. Then tried to move some of it into function > > IncrementAndRebuildInput, then result changes, I no longer have same result > > as when code in function was inline - why? > > Have you tried running it in isolation to see what it does? > > When I try it, it works for me (apart from printing a lot of unnecessary > intermediate results): > > py> result = IncrementAndRebuildInput("abc def 123 xyz 456") > ['abc', 'def', '123', 'xyz', '456'] > 124 > ['abc', 'def', '124', 'xyz', '456'] > NOWHERE > 457 > ['abc', 'def', '124', 'xyz', '457'] > NOWHERE > ['abc', 'def', '124', 'xyz', '457'] > Point6 > > > > Now check the returned result: > > py> result > ['abc', 'def', '124', 'xyz', '457'] > > So it certainly does increment the numbers in the string. The only > bit it doesn't do is rebuild the string, but that takes just one > minor change: instead of "return newstring" (by the way, that's false > advertising -- newstring is not a string, it is a list), use: > > return ' '.join(newstring) > > > You also use this function: > > > def IsNum(string): > > #print "IsNum string", string > > for char in string: #checks string groupings to be all nums > > if not char.isdigit(): > > #print "false" > > return False > > #print "true" > > return True > > You don't need it! The isdigit method doesn't only work on a single character > at a time, it works on an entire string: > > py> "12345".isdigit() > True > py> "12345a".isdigit() > False I just want to point to the OP (Frank) that this only works for "digits" i.e. integers. It will fail for other types of numbers. >>> '12.3'.isdigit() False >>> '12.3'.isalnum() False ~Ramit This email is confidential and subject to important disclaimers and conditions including on offers for the purchase or sale of securities, accuracy and completeness of information, viruses, confidentiality, legal privilege, and legal entity disclaimers, available at http://www.jpmorgan.com/pages/disclosures/email. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Help with class example
Ramit Prasad wrote: > You would be better off trying to run this from the command > line. I just wanted to clarify on this. The reason you will have a better results running this from the command line is that Python will normally give you very good error traceback. An IDE might hide or obscure the problem, but it should be fairly easy to figure out if you see the error traceback. The error will usually be on that line, but sometimes (especially with syntax errors) it might be the line (or two) previous to the line shown. If you do not know how to run on the command line, post back letting us know (and what operating system) and we can guide you. It is not difficult but can sometimes be awkward or intimidating for people. Ramit This email is confidential and subject to important disclaimers and conditions including on offers for the purchase or sale of securities, accuracy and completeness of information, viruses, confidentiality, legal privilege, and legal entity disclaimers, available at http://www.jpmorgan.com/pages/disclosures/email. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] why different result from two similar ways
On 07/11/12 03:31, Prasad, Ramit wrote: Steven D'Aprano wrote: The isdigit method doesn't only work on a single character at a time, it works on an entire string: py> "12345".isdigit() True py> "12345a".isdigit() False I just want to point to the OP (Frank) that this only works for "digits" i.e. integers. It will fail for other types of numbers. That's why it's called "isdigit" not "isnumber" :) -- Steven ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor