Re: [Tutor] Advise...
What is the function? 3*x*x What is the minimum? 2 What is the maximum? 5 117.000435 Which, considering that it is supposed to be exactly 117, It's darn good. Unfortunately, it also takes about 10 seconds to do all that. Any suggestions? Any advice? TIA Jacob Schmidt Jacob, You can get better accuracy with orders of magnitude fewer steps by evaluating the function at the midpoint of each step rather than the low value. This has the added benefit of yielding the same result when stepping x up (2 to 5) or down (5 to 2). Here's some modified code (I don't have psyco): from __future__ import division import time def reimannSum(func_x, min_x, max_x, steps): start = time.time() totalArea = 0 #precalculate step size delta_x = 1 / steps #set x to midpoint of first step x = min_x + delta_x / 2 while min_x <= x <= max_x: totalArea += eval(func_x) * delta_x x += delta_x return totalArea, steps, time.time() - start stepsList = [10, 1, 1000, 500, 200, 100] fmt = 'Area: %f Steps: %d Time: %f' for steps in stepsList: print fmt % reimannSum('3 * x * x', 2, 5, steps) The results on my machine are: Area: 117.00 Steps: 10 Time: 44.727405 Area: 117.00 Steps: 1 Time: 4.472391 Area: 116.99 Steps: 1000 Time: 0.454841 Area: 116.97 Steps: 500 Time: 0.223208 Area: 116.81 Steps: 200 Time: 0.089651 Area: 116.25 Steps: 100 Time: 0.044431 TJ ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Should this be a list comprehension or something?
On 26 Jan 2005, [EMAIL PROTECTED] wrote: > The following Python code works correctly; but I can't help but wonder if > my for loop is better implemented as something else: a list comprehension > or something else more Pythonic. [Code] > > > w1 = Water(50,0) > w2 = Water(50,100) > w3 = Water(25,50) > > print CombineWater((w1,w2,w3)) > > > prints, as expected: 125.00, 50.00 What about overloading »+«? class Water(object): def __init__(self, mass, temp): self.mass = mass self.temp = temp def __repr__(self): return ("%.2f, %.2f" % (self.mass, self.temp)) def __add__(self, other): m = self.mass + other.mass return Water(m, (self.mass*self.temp + other.mass*other.temp)/m) .>>> w1 = Water(50,0) .>>> w2 = Water(50, 100) .>>> w3 = Water(25,50) .>>> w1 + w2 + w3 .125.00, 50.00 Karl -- Please do *not* send copies of replies to me. I read the list ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Advise...
There was a discussion about this same question (in the context of graphing a user-supplied function) just a few weeks ago. My suggestion was to use exec to create a real function object that you can call directly; you can read it here: http://mail.python.org/pipermail/tutor/2005-January/034696.html Read the whole thread for other ideas. Kent Jacob S. wrote: Hi all. Long time no see. (About five days, right?) Anyway, I know the first thing that some of you are going to say is using eval(). I don't want a whole guilt trip on security risks and all that. I do not want to share the code with anyone else while it's on my computer, and I sure don't have anyone near me that knows python. I would be surprised if more than 50 people in Portland, IN knew anything about python. So security is not a problem. I guess what I'm looking for is someone who knows the Reimann Sum better than I do and can tell me whether I can do something to make it more efficient. It's horribly slow with ten thousand steps-- I don't know the notation very well, but it loops the loop O(step*(maximum-minimum)) times, which is horribly sucky. In case anyone doesn't know, Reimann's sum is the computer's version of the definite integral, the area under a curve in a particular domain. Basic input and output. If a curve is a straight line, say y = x, the area under the line on an interval can be found by geometric means. However, if you use a parabola, or any other function, say y = 3*x**2, What is the function? 3*x*x What is the minimum? 2 What is the maximum? 5 117.000435 Which, considering that it is supposed to be exactly 117, It's darn good. Unfortunately, it also takes about 10 seconds to do all that. Any suggestions? Any advice? TIA Jacob Schmidt from __future__ import division import psyco psyco.full() fofx = raw_input("What is the function? ") minimum = raw_input("What is the minimum? ") maximum = raw_input("What is the maximum? ") minimum = float(minimum) maximum = float(maximum) total = 0 step = 10 x = minimum while minimum <= x <= maximum: area = eval(fofx)*1/step total = total+area x = x+1/step print total # ___ 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] sorting a 2 gb file- i shrunk it and turned it around
My guess is that your file is small enough that Danny's two-pass approach will work. You might even be able to do it in one pass. If you have enough RAM, here is a sketch of a one-pass solution: # This will map each result to a list of queries that contain that result results= {} # Iterate the file building results for line in file: if isQueryLine(line): current_query = line elif isResultLine(line): key = getResultKey(line) results.setdefault(key, []).append(current_query) # see explanation below # Now go through results looking for entries with more than one query for key, queries in results.iteritems(): if len(queries) > 1: print print key for query in queries: print query You have to fill in the details of isQueryLine(), isResultLine() and getResultKey(); from your earlier posts I'm guessing you can figure them out. What is this: results.setdefault(key, []).append(current_query) setdefault() is a handy method of a dict. It looks up the value corresponding to the given key. If there is no value, it *sets* the value to the given default, and returns that. So after results.setdefault(key, []) results[key] will have a list in it, and we will have a reference to the list. Then appending the list adds the query to the list in the dict. Please let us know what solution you end up using, and how much memory it needs. I'm interested... Kent Scott Melnyk wrote: Thanks for the thoughts so far. After posting I have been thinking about how to pare down the file (much of the info in the big file was not relevant to this question at hand). After the first couple of responses I was even more motivated to shrink the file so not have to set up a db. This test will be run only now and later to verify with another test set so the db set up seemed liked more work than might be worth it. I was able to reduce my file down about 160 mb in size by paring out every line not directly related to what I want by some simple regular expressions and a couple tests for inclusion. The format and what info is compared against what is different from my original examples as I believe this is more clear. my queries are named by the lines such as: ENSE1387275.1|ENSG0187908.1|ENST0339871.1 ENSE is an exon ENSG is the gene ENST is a transcript They all have the above format, they differ in in numbers above following ENS[E,G orT]. Each query is for a different exon. For background each gene has many exons and there are different versions of which exons are in each gene in this dataset. These different collections are the transcripts ie ENST0339871.1 in short a transcript is a version of a gene here transcript 1 may be formed of exons a,b and c transcript 2 may contain exons a,b,d the other lines (results) are of the format hg17_chainMm5_chr7_random range=chr10:124355404-124355687 5'pad=...44 0.001 hg17_chainMm5_chr14 range=chr10:124355392-124355530 5'pad=0 3'pa...44 0.001 "hg17_chainMm5_chr7_random range=chr10:124355404-124355687" is the important part here from "5'pad" on is not important at this point What I am trying to do is now make a list of any of the results that appear in more than one transcript ## FILE SAMPLE: This is the number 1 query tested. Results for scoring against Query= ENSE1387275.1|ENSG0187908.1|ENST0339871.1 are: hg17_chainMm5_chr7_random range=chr10:124355404-124355687 5'pad=...44 0.001 hg17_chainMm5_chr14 range=chr10:124355392-124355530 5'pad=0 3'pa...44 0.001 hg17_chainMm5_chr7 range=chr10:124355391-124355690 5'pad=0 3'pad...44 0.001 hg17_chainMm5_chr6 range=chr10:124355389-124355690 5'pad=0 3'pad...44 0.001 hg17_chainMm5_chr7 range=chr10:124355388-124355687 5'pad=0 3'pad...44 0.001 hg17_chainMm5_chr7_random range=chr10:124355388-124355719 5'pad=...44 0.001 This is the number 3 query tested. Results for scoring against Query= ENSE1365999.1|ENSG0187908.1|ENST0339871.1 are: hg17_chainMm5_chr14 range=chr10:124355392-124355530 5'pad=0 3'pa...60 2e-08 hg17_chainMm5_chr7 range=chr10:124355391-124355690 5'pad=0 3'pad...60 2e-08 hg17_chainMm5_chr6 range=chr10:124355389-124355690 5'pad=0 3'pad...60 2e-08 hg17_chainMm5_chr7 range=chr10:124355388-124355687 5'pad=0 3'pad...60 2e-08 ## I would like to generate a file that looks for any results (the hg17_etc line) that occur in more than transcript (from the query line ENSE1365999.1|ENSG0187908.1|ENST0339871.1) so if hg17_chainMm5_chr7_random range=chr10:124355404-124355687 shows up again later in the file I want to know and want to record where it is used more than once, otherwise I will ignore it. I am think another reg expression to capture the transcript id followed by something that captures each of the results, and writes to another file anytime a result appears more than once, and ties the transcript ids to them somehow. Any suggestions? I agree if I had more tim
Re: [Tutor] Advise...
There has been alot of talk on this list about using list comprehensions lately, and this could be one of those useful places. While I don't have time to experiment with real code, I would suggest changing your function to look like: steps = [ min_x + i*delta_x for i in range(steps) ] totalarea = sum([ eval(func_x)*delta_x for x in steps ]) Since list comprehensions are significantly faster than while loops, this could be a big speed boost. There may be a mistake or two in the above code, but hopefully the idea will be helpful. Bill TJ wrote: What is the function? 3*x*x What is the minimum? 2 What is the maximum? 5 117.000435 Which, considering that it is supposed to be exactly 117, It's darn good. Unfortunately, it also takes about 10 seconds to do all that. Any suggestions? Any advice? TIA Jacob Schmidt Jacob, You can get better accuracy with orders of magnitude fewer steps by evaluating the function at the midpoint of each step rather than the low value. This has the added benefit of yielding the same result when stepping x up (2 to 5) or down (5 to 2). Here's some modified code (I don't have psyco): from __future__ import division import time def reimannSum(func_x, min_x, max_x, steps): start = time.time() totalArea = 0 #precalculate step size delta_x = 1 / steps #set x to midpoint of first step x = min_x + delta_x / 2 while min_x <= x <= max_x: totalArea += eval(func_x) * delta_x x += delta_x return totalArea, steps, time.time() - start stepsList = [10, 1, 1000, 500, 200, 100] fmt = 'Area: %f Steps: %d Time: %f' for steps in stepsList: print fmt % reimannSum('3 * x * x', 2, 5, steps) The results on my machine are: Area: 117.00 Steps: 10 Time: 44.727405 Area: 117.00 Steps: 1 Time: 4.472391 Area: 116.99 Steps: 1000 Time: 0.454841 Area: 116.97 Steps: 500 Time: 0.223208 Area: 116.81 Steps: 200 Time: 0.089651 Area: 116.25 Steps: 100 Time: 0.044431 TJ ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Unique Items in Lists
Hi, I am a new member to this group and relatively to python. I have a list with 4 columns and column1 elements are unique. I wanted to extract unique elements in column3 and and place the other elements of the column along with unique elements in column 4 as a tab delim text. Table: col1col2col3 col4 A Apple 5Chennai B Baby 11Delhi I Baby* 1Delhi M Dasheri+ 5Mumbai K Apple 12 Copenhagen * -> Baby => Infant + -> Dasheri => Mango for ele in table: col2 = ele.split('\t')[1] col2.append(col2) col2_set = sets.Set(col2) col2_set -> (Apple,Baby Dasheri) Apple A,K 5,12Chennai, Copenhagen Baby B,I 1,11Delhi Dasheri M 5 Mumbai. for ele in col1_set: nam = ele.strip() for k in list: m = re.search(nam,k) cols = k.split('\t') a = cols[0] n = cols[2] c = cols[3] print nam+'\t'+a+'\t'+n+'\t'+c A dictionary option does not work beca This isnt working properly. please help me any one question 2: A list with 100 unique items, repeated several times. Apples - repeated 1000 times in list Vegie - repeated 300 times in list how can I get a uniq elements that repeated several times: for item in range(len(list)): for k in range(len(list)): if item == k: if list[item] != k[list]: print item This isnt working either. logic appears correct. looking forward for help pleas. thank you Srini __ Do you Yahoo!? The all-new My Yahoo! - What will yours do? http://my.yahoo.com ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Advise...
On Wed, 26 Jan 2005, Bill Kranec wrote: > There has been alot of talk on this list about using list comprehensions > lately, and this could be one of those useful places. While I don't > have time to experiment with real code, I would suggest changing your > function to look like: > > steps = [ min_x + i*delta_x for i in range(steps) ] > totalarea = sum([ eval(func_x)*delta_x for x in steps ]) > > Since list comprehensions are significantly faster than while loops, > this could be a big speed boost. > > There may be a mistake or two in the above code, but hopefully the idea > will be helpful. Calling eval() there in the inner loop might be costly, because Python needs to do extra work to tokenize and parse the string, every time through the iteration. We want to reduce the work done in tight inner loops like that. We can do some of that work up front by compiling the code. Here's some hacky code to do the compilation up front: ### >>> def makeFunction(expressionString): ... compiledExp = compile(expressionString, 'makeFunction', 'eval') ... def f(x): ... return eval(compiledExp, {}, {'x' : x}) ... return f ... ### Some of the stuff there is a bit obscure, but the idea is that we get Python to parse and compile the expression down once. Later on, we can evaluation the compiled code, and that should be faster than evaluating a string. Once we have this, we can use it like this: ### >>> myFunction = makeFunction("3*x*x") >>> myFunction(0) 0 >>> myFunction(1) 3 >>> myFunction(2) 12 >>> myFunction(3) 27 ### So although makeFunction()'s internals are weird, it shouldn't be too hard to treat it as a black box. *grin* Let's see how this performs against that 3x^2 expression we saw before. The original approach that calls eval() on the string takes time: ### >>> def timeIt(f, n=1000): ... start = time.time() ... for i in xrange(n): ... f(i) ... end = time.time() ... return end - start ... >>> def myFunctionOriginal(x): ... return eval("3*x*x") ... >>> timeIt(myFunctionOriginal) 0.036462068557739258 ### The precompiled expression can work more quickly: ### >>> timeIt(myFunction) 0.0050611495971679688 ### And we should still get the same results: ### >>> for i in range(2000): ... assert myFunction(i) == myFunctionOriginal(i) ... >>> ### I hope this helps! ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Advise...
> > There has been alot of talk on this list about using list comprehensions > > lately, and this could be one of those useful places. While I don't > > have time to experiment with real code, I would suggest changing your > > function to look like: > > > > steps = [ min_x + i*delta_x for i in range(steps) ] > > totalarea = sum([ eval(func_x)*delta_x for x in steps ]) > > Calling eval() there in the inner loop might be costly, because Python > needs to do extra work to tokenize and parse the string, every time > through the iteration. We want to reduce the work done in tight inner > loops like that. > > We can do some of that work up front by compiling the code. Here's some > hacky code to do the compilation up front: > > ### > >>> def makeFunction(expressionString): > ... compiledExp = compile(expressionString, 'makeFunction', 'eval') > ... def f(x): > ... return eval(compiledExp, {}, {'x' : x}) > ... return f > ... > ### Oh! There's a slightly simpler way to write that makeFunction(): ### >>> def makeFunction(s): ... return eval("lambda x: " + s) ... ### It even has a little less overhead than the previous code. ### >>> timeIt(myFunctionOriginal) 0.035856008529663086 >>> >>> timeIt(makeFunction("3*x*x")) 0.00087714195251464844 ### Best of wishes! ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Read file line by line
> 1. Why does the assignment-and-test in one line not allowed in Python? > For example, while ((content = fd.readline()) != ""): Because Guido didn't write it that way? ;-) And that may have been because it is such a common source of bugs. So common in fact that many compilers now offer to emit a warning when they detect it...just in case you meant to use ==... > 2. I know Perl is different, but there's just no equivalent of while > ($line = ) { } ? Take a look at the fileinput module and of course for line in fd: #do something works too. Alan G Author of the Learn to Program web tutor http://www.freenetpages.co.uk/hp/alan.gauld ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] ascii encoding
> Whether or not it's positive or negative depends on which side of GMT/UTC > you are, of course :) Note that the result in is seconds, too: Which is insane since timezones have nothing to do with time offsets. Especially at the second level! Oh well, nothing is perfect! Alan G. (Feeling picky today) ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Should this be a list comprehension or something?
Personally no I don;t think it should be a comprehension. LCs are for building lists, although they sometimes are abused for other things, but you do not appear to be building a list per se. The for loop is clearer IMHO. > Here's my code: > > class Water: > def __init__(self, WaterMass, WaterTemperature): > self.mass = WaterMass > self.temperature = WaterTemperature > def __repr__(self): > return ("%.2f, %.2f" % (self.mass, self.temperature)) > > def CombineWater(WaterList): > totalmass=0 > numerator = 0; denominator = 0 > for WaterObject in WaterList: > totalmass += WaterObject.mass > numerator += WaterObject.mass * WaterObject.temperature > return Water(totalmass, numerator/totalmass) Alan G Author of the Learn to Program web tutor http://www.freenetpages.co.uk/hp/alan.gauld ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Class within class, or...?
class Reader: def __init__(self, filePath=""): self.dataA=SchemaA() self.dataB=SchemaB() ... class SchemaA(): class SchemaB(): You probaly should put the Schema definitions before the Reader definition. Otherwise what you suggest is absolutely the norm for OO programming. Alan G. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Unique Items in Lists
On Wed, 26 Jan 2005, Srinivas Iyyer wrote: > I have a list with 4 columns and column1 elements are unique. I wanted > to extract unique elements in column3 and and place the other elements > of the column along with unique elements in column 4 as a tab delim > text. > > Table: > > col1col2col3 col4 > A Apple 5Chennai > B Baby 11Delhi > I Baby* 1Delhi > M Dasheri+ 5Mumbai > K Apple 12 Copenhagen [Meta: we seem to be getting a run of similar questions this week. Scott Melnyk also asked about grouping similar records together: http://mail.python.org/pipermail/tutor/2005-January/035185.html.] Hi Srinivas, I see that you are trying to group records based on some criterion. You may find the problem easier to do if you fist do a sort on that criterion column: that will make related records "clump" together. For your sample data above, if we sort against the second column, the records will end up in the following order: ### A Apple 5Chennai K Apple 12 Copenhagen B Baby 11 Delhi I Baby 1Delhi M Dasheri 5Mumbai ### In this sorting approach, you can then run through the sorted list in order. Since all the related elements should be adjacent, grouping related lines together should be much easier, and you should be able to produce the final output: ### Apple A,K 5,12Chennai,Copenhagen Baby B,I 1,11Delhi Dasheri M 5 Mumbai ### without too much trouble. You can do this problem without dictionaries at all, although you may find the dictionary approach a little easier to implement. > A dictionary option does not work A dictionary approach is also very possible. The thing you may be stuck on is trying to make a key associate with multiple values. Most examples of dictionaries in tutorials use strings as both the keys and values, but dictionaries are more versatile: we can also make a dictionary whose values are lists. For example, here is a small program that groups words by their first letters: ### >>> def groupAlpha(words): ... groups = {} ... for w in words: ... firstLetter = w[0] ... if firstLetter not in groups: ... groups[firstLetter] = [] ... groups[firstLetter].append(w) ... return groups ... >>> groupAlpha("this is a test of the emergency broadcast system".split()) {'a': ['a'], 'b': ['broadcast'], 'e': ['emergency'], 'i': ['is'], 'o': ['of'], 's': ['system'], 't': ['this', 'test', 'the']} ### If you have more questions, please feel free to ask. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Advise...
> If the user must be able to enter in the function, then it would be better > to evaluate this once and turn it into some sort of function that you can > call inside the loop (it's the eval that is so expensive). How to do that > depends a lot on how complex the possible functions can be (if they'll only > include 'x*+/-' and numbers, for example, it's not so tricky). exp = raw_input('Type expression') func = eval('lambda x: " + exp) print func(42) etc... Or if you really can't grokm lambda: exec('def func(x): return " + exp) should do the same... Alan G. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Convert string to variable name
Hello tutors, This is something I've been trying to figure out for some time. Is there a way in Python to take a string [say something from a raw_input] and make that string a variable name? I want to to this so that I can create class instances on-the-fly, using a user-entered string as the instance name. I can do it with an "exec" statement, but that would force me to use more "execs" to reference the newly created class instance[s] and I would rather not do that. I'm sure I could accomplish something similar using a plain old dictionary, but I was playing around with the OOP stuff and thought it might be a neat thing to try out. I've attached the small function I've come up with to the end of this message. This is just a model I created for testing purposes, so it's not the most elegant code. Any suggestions would be greatly appreciated. TIA, tony giunta def generateInstance(): class test: def __init__(self, title, price): self.title = title self.price = price def theTitle(self, title): return self.title def thePrice(self, price): return self.price myName = raw_input("Name: ") myTitle= raw_input("Title: ") myPrice = raw_input("Price: ") exec '%s = test("%s", %s)' % (myName, myTitle, myPrice) ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Advise...
Thanks everyone! Kent started the suggestion of making a code object, but everyone else seems to have worked their way to it. Beautiful! I only have to call exec once, and it cuts down time considerably. Here is the new code. Jacob Schmidt Please remind me if I've forgotten anything. ### Start ### from __future__ import division from math import * import psyco psyco.full() def reimannsum(fofx,x,max1): total = 0 step = 1e-5 exec "def f(x): return %s" % fofx while x <= max1: total = total+f(x) x = x+step return abs(total*step) fofx = raw_input("What is the function? ") minimum = raw_input("What is the minimum? ") maximum = raw_input("What is the maximum? ") minimum = float(minimum) maximum = float(maximum) print reimannsum(fofx,minimum,maximum) End ## If the user must be able to enter in the function, then it would be better to evaluate this once and turn it into some sort of function that you can call inside the loop (it's the eval that is so expensive). How to do that depends a lot on how complex the possible functions can be (if they'll only include 'x*+/-' and numbers, for example, it's not so tricky). exp = raw_input('Type expression') func = eval('lambda x: " + exp) print func(42) etc... Or if you really can't grokm lambda: exec('def func(x): return " + exp) should do the same... Alan G. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Convert string to variable name
On Wed, 26 Jan 2005, Tony Giunta wrote: > This is something I've been trying to figure out for some time. Is > there a way in Python to take a string [say something from a raw_input] > and make that string a variable name? Hi Tony, Conceptually, yes: you can collect all those on-the-fly variables in a dictionary. > I'm sure I could accomplish something similar using a plain old > dictionary, Yup. *grin* > but I was playing around with the OOP stuff and thought it might be a > neat thing to try out. We can use exec() to create dynamic variable names, but it's almost always a bad idea. Dynamic variable names make it very difficult to isolate where variables are coming from in a program. It'll also prevent us from reliably using the excellent PyChecker program: http://pychecker.sourceforge.net/ So I'd recommend just using a plain old dictionary: it's not exciting, but it's probably the right thing to do. Looking back at the code: ### def generateInstance(): class test: def __init__(self, title, price): self.title = title self.price = price def theTitle(self, title): return self.title def thePrice(self, price): return self.price myName = raw_input("Name: ") myTitle= raw_input("Title: ") myPrice = raw_input("Price: ") exec '%s = test("%s", %s)' % (myName, myTitle, myPrice) ### In Python, we actually can avoid writing attribute getters and setters --- the "JavaBean" interface --- because Python supports a nice feature called "properties". See: http://dirtsimple.org/2004/12/python-is-not-java.html http://www.python.org/2.2.2/descrintro.html#property The upside is that we usually don't need methods like theTitle() or thePrice() in Python. Our revised code looks like: ### class test: def __init__(self, title, price): self.title = title self.price = price instances = {} def generateInstance(): myName = raw_input("Name: ") myTitle = raw_input("Title: ") myPrice = raw_input("Price: ") instances[myName] = test(myTitle, myPrice) ### If you have more questions, please feel free to ask. Best of wishes to you! ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Unique Items in Lists
col2_set = sets.Set(col2) how can I get a uniq elements that repeated several times: for item in range(len(list)): for k in range(len(list)): if item == k: if list[item] != k[list]: print item First off, this would never work. You are iterating over the same list so go through each step on paper or in your head. Oh, and don't use list for a variable name--it's a builtin function that you don't want to write over. An example a = [1,1,4,2,3,1,5] for item in range(len(a)): for k in range(len(a)): if item == k: if a[item]!=a[k]: ## I think this is what you meant to put--otherwise you would be trying to index an index! print item ## This returns an index, maybe you want the element instead? Ok... here's how it runs with all the variables replaced with the values defined by the loop. if 0 == 0: if 1!=1: print 1 That is okay. if 0 == 1: if 1 != 1: ## The first 1 comes from a[0] (item=0), the second 1 from a[1] (k=1) print 1 ## This is obviously not what you want, so immediately we see a problem. Then it goes through (0,2),(0,3),..(1,0),(1,1)(1,2),(2,0),(2,1), Anyway, this is all irrevelant because sets takes care of doubles. import sets a = sets.Set([1,2,1,4,3,5,5,2,3,3,2]) a Set([1, 2, 3, 4, 5]) list(a) ## This is one of the uses of the builtin list -- to make a list of an iterable. [1, 2, 3, 4, 5] HTH, Jacob This isnt working either. logic appears correct. looking forward for help pleas. thank you Srini __ Do you Yahoo!? The all-new My Yahoo! - What will yours do? http://my.yahoo.com ___ 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] Convert string to variable name
On Wed, 26 Jan 2005 17:11:59 -0800 (PST), Danny Yoo <[EMAIL PROTECTED]> wrote: > > > On Wed, 26 Jan 2005, Tony Giunta wrote: > > We can use exec() to create dynamic variable names, but it's almost always > a bad idea. Dynamic variable names make it very difficult to isolate > where variables are coming from in a program. Yeah, that's why I was wondering if there was a better way to do it. > So I'd recommend just using a plain old dictionary: it's not exciting, but > it's probably the right thing to do. Thanks. I thought I might have to resort to this. =) > Looking back at the code: > > ### > def generateInstance(): > class test: > def __init__(self, title, price): > self.title = title > self.price = price > def theTitle(self, title): > return self.title > def thePrice(self, price): > return self.price > > myName = raw_input("Name: ") > myTitle= raw_input("Title: ") > myPrice = raw_input("Price: ") > > exec '%s = test("%s", %s)' % (myName, myTitle, myPrice) > ### > > In Python, we actually can avoid writing attribute getters and setters --- > the "JavaBean" interface --- because Python supports a nice feature called > "properties". See: > > http://dirtsimple.org/2004/12/python-is-not-java.html > http://www.python.org/2.2.2/descrintro.html#property Ahh. Thanks for the links, I shall check them out. > The upside is that we usually don't need methods like theTitle() or > thePrice() in Python. Our revised code looks like: > > ### > class test: > def __init__(self, title, price): > self.title = title > self.price = price > > instances = {} > > def generateInstance(): > myName = raw_input("Name: ") > myTitle = raw_input("Title: ") > myPrice = raw_input("Price: ") > instances[myName] = test(myTitle, myPrice) > ### > > If you have more questions, please feel free to ask. Best of wishes to > you! Thanks again Danny. This is exactly what I was looking for. tony ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] New to Python
Greetings all, I'm new to python and thought I'd pop in here for advice. I've done object oriented design and programmed in perl, java, c++, basic, etc. I haven't done a lot of development, mostly just glorified oject-oriented scripts. I'm curious about good tutorial websites and books to buy. I also have a development question for anybody who might know. The project I'm working on now to develop my python skills is a prgram to script control another windows program. The program doesn't have a published API so I'll probably need to locate memory addresses data fields and button routines. Am I in way over my head for a Python beginner or does anybody have any advice for where to start poking around in memory and which python classes I should use to do so? ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] New to Python
On Jan 27, 2005, at 02:09, Jason White wrote: I'm curious about good tutorial websites and books to buy. I learned Python (well, the basics thereof -- enough to do useful stuff on my summer job, anyway ^^) in an afternoon using the official tutorial that's found somewhere on http://www.python.org/ . It's very good provided you already have some programming experience (which seems to be your case). I hear that Alan Gauld's tutorial is also very good, but geared more towards people new to programming. You'll find the address in his sig (which itself should be in the post that he sent to the list while I was writing this one, if my predictions are accurate :p ). I also have a development question for anybody who might know. The project I'm working on now to develop my python skills is a prgram to script control another windows program. The program doesn't have a published API so I'll probably need to locate memory addresses data fields and button routines. Am I in way over my head for a Python beginner or does anybody have any advice for where to start poking around in memory and which python classes I should use to do so? Eeh? Peeks and pokes? Are you even allowed to do that? I mean, doesn't Windows have a protected memory mechanism, like any OS should have nowadays? -- Max maxnoel_fr at yahoo dot fr -- ICQ #85274019 "Look at you hacker... A pathetic creature of meat and bone, panting and sweating as you run through my corridors... How can you challenge a perfect, immortal machine?" ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
RE: [Tutor] New to Python
[Jason White] >> I'm curious about good tutorial websites and books to buy. [Max Noel] > I learned Python (well, the basics thereof -- enough to do useful > stuff on my summer job, anyway ^^) in an afternoon using the official > tutorial that's found somewhere on http://www.python.org/. It's very > good provided you already have some programming experience If you (Jason) are using Windows, then you absolutely want to install Mark Hammond's pywin32 extensions as well as Python itself. If you use PythonWin as your IDE (and you might as well, at least at first), then to get to the tutorial, you can just open PythonWin, and select "Python Manuals" from the Help menu, then click "Tutorial". I absolutely agree that it's the best place to start. >> I also have a development question for anybody who might know. The >> project I'm working on now to develop my python skills is a prgram to >> script control another windows program. The program doesn't have a >> published API so I'll probably need to locate memory addresses data >> fields and button routines. There's a Python library for controlling Windows in this sort of way (simulating mouse clicks and so on), although the name escapes me at the moment. However, are you positive that you can't control it properly? Check to see if it has a COM interface (you can use PythonWin to do that), and use that if possible. =Tony.Meyer ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] New to Python
I also recommend the book "Dive Into Python" - it gets awesome reviews, and the book is under Creative Commons license, so it's free to download and distribute. http://diveintopython.org I also have the book "Core Python Programming" which is pretty good, and has a nice way of leaping right into code so that if you have any prior knowledge, you can use it to learn faster. -Jay On Wednesday 26 January 2005 09:09 pm, Jason White wrote: > > Greetings all, I'm new to python and thought I'd pop in here for > advice. I've done object oriented design and programmed in perl, > java, c++, basic, etc. I haven't done a lot of development, mostly > just glorified oject-oriented scripts. I'm curious about good > tutorial websites and books to buy. > > I also have a development question for anybody who might know. The > project I'm working on now to develop my python skills is a prgram to > script control another windows program. The program doesn't have a > published API so I'll probably need to locate memory addresses data fields > and button routines. Am I in way over my head for a Python beginner > or does anybody have any advice for where to start poking around in memory > and which python classes I should use to do so? > ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Preffered way to search posix filesystem
I would like to search filesystem structures using globs on Posix systems from within Python. I don't see an obvious method to do this with in the standard modules. What is the preferred way of doing this? Should I just use the find command or is there a good module out there for searching? Thanks. -- Miles Stevenson [EMAIL PROTECTED] PGP FP: 035F 7D40 44A9 28FA 7453 BDF4 329F 889D 767D 2F63 pgpLQhrIGjLES.pgp Description: PGP signature ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] New to Python
I too once had trouble remembering (and finding) the name of this library so here it is. http://www.tizmoi.net/watsup/intro.html I have not used it but the documentation by example, seemed to me to be approachable. Tony Meyer wrote: >There's a Python library for controlling Windows in this sort of way >(simulating mouse clicks and so on), although the name escapes me at the >moment. However, are you positive that you can't control it properly? >Check to see if it has a COM interface (you can use PythonWin to do that), >and use that if possible. > >=Tony.Meyer > __ Do you Yahoo!? The all-new My Yahoo! - What will yours do? http://my.yahoo.com ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Preffered way to search posix filesystem
Try the os module. I think this should probably get you there. http://docs.python.org/lib/module-os.html Miles Stevenson wrote: >I would like to search filesystem structures using globs on Posix systems from >within Python. I don't see an obvious method to do this with in the standard >modules. What is the preferred way of doing this? Should I just use the find >command or is there a good module out there for searching? > >Thanks. > > __ Do You Yahoo!? Tired of spam? Yahoo! Mail has the best spam protection around http://mail.yahoo.com ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Unique Items in Lists
Dear Jacob, thank you for your suggestion. however, i think my question was not clear. what i meant to ask in my previous question was, how to know which elements repeated and how many times they were repeated. while my question was flying, i did a small test: took a list: >>> a [1, 1, 2, 3, 4, 2, 2] wanted to know which elements repeated and how many times: for i in range(len(a)): for k in range(len(a)): if i != k: if a[i] == a[k]: print a[i] break 1 1 2 2 2 In this very huge list (:-) kidding) of 7 elements, it is easy know by the above primitive code that there 1 s repeated two times, and 2 s repeated three times and finally 1 and 2 numbers are repeated in list a: With sets option i get a list of unique elements, but by no means i get to know which elements were repeated and how many times. With the above primitive code , I know that 1 and 2 are peated. However, in case where there are thousands of entries, how can I get to kno wich elements got repeated and how many times. Do you see any flaws in this code, how can that be made to look more pyhonian way. Hope my question is clear now and appreciate your suggestions. Thank you in advance Srini > >>> import sets > >>> a = sets.Set([1,2,1,4,3,5,5,2,3,3,2]) > >>> a > Set([1, 2, 3, 4, 5]) > >>> list(a) ## This is one of the uses of the > builtin list -- to make a > >>> list of an iterable. > [1, 2, 3, 4, 5] > >>> > > HTH, > Jacob > > > This isnt working either. logic appears correct. > > looking forward for help pleas. > > > > thank you > > > > Srini > > > > > > > > > > > > > > __ > > Do you Yahoo!? > > The all-new My Yahoo! - What will yours do? > > http://my.yahoo.com > > ___ > > Tutor maillist - Tutor@python.org > > http://mail.python.org/mailman/listinfo/tutor > > > > > > __ Do You Yahoo!? Tired of spam? Yahoo! Mail has the best spam protection around http://mail.yahoo.com ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Unique Items in Lists
Ok. I think I understand and I happen to be up at 1:30 my time so here is the solution as I understand the problem. This is a very common problem and has a fairly easy solution. You can then take adict.keys() which returns a list of unique elements. Good Luck import random l=[random.randrange(1,20) for x in range(100)] l [7, 18, 17, 17, 6, 11, 14, 9, 4, 16, 2, 9, 3, 13, 4, 2, 5, 15, 15, 3, 3, 11, 18, 12, 6, 8, 15, 3, 7, 9, 9, 7, 12, 11, 11, 9, 19, 19, 15, 2, 17, 18, 16, 8, 15, 3, 19, 19, 19, 1, 3, 17, 3, 8, 16, 1, 5, 19, 17, 16, 19, 6, 3, 8, 16, 11, 12, 7, 10, 13, 13, 11, 6, 2, 18, 15, 17, 8, 12, 13, 5, 12, 2, 19, 2, 19, 7, 10, 4, 14, 15, 14, 5, 1, 16, 1, 9, 10, 17, 12] adict={} for x in l: if adict.has_key(x): #then there is already an item adict[x]+=1 #increment the count by one else: #there is no key the item hasn't been seen adict[x]=1 #there is one instance so far adict {1: 4, 2: 6, 3: 8, 4: 3, 5: 4, 6: 4, 7: 5, 8: 5, 9: 6, 10: 3, 11: 6, 12: 6, 13: 4, 14: 3, 15: 7, 16: 6, 17: 7, 18: 4, 19: 9} Srinivas Iyyer wrote: >Dear Jacob, thank you for your suggestion. > >however, i think my question was not clear. what i >meant to ask in my previous question was, how to know >which elements repeated and how many times they were >repeated. > >while my question was flying, i did a small test: > >took a list: > > a >[1, 1, 2, 3, 4, 2, 2] > >wanted to know which elements repeated and how many >times: > __ Do you Yahoo!? Yahoo! Mail - now with 250MB free storage. Learn more. http://info.mail.yahoo.com/mail_250 ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Cluster algorithms
Hi: I am still trying to learn the OOPs side of python. however, things/circumstances dont seems to stop until I finish my practise and attaing higher understanding. may be, i am being pushed by circumstances into the stream and i am being tested if I can swim efficiently while I struggle with basic steps of swimming. The 100% analogy my perspective of learning python :-) I have a couple of questions to ask tutors: Are there any example programs depicting Clustering algorithms such as agglomerative, complete link, partional , squared error clustering, k-means or clustering algos based on Neural networks or genetic algorithm. although I just learned python, (to major extent in programming also), I need to apply some of these algos to my data. Any suggestions/recommendations? Do I have to know to code well using OOP methods to apply these algorithms? -Kumar __ Do you Yahoo!? Yahoo! Mail - Easier than ever with enhanced search. Learn more. http://info.mail.yahoo.com/mail_250 ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Unique Items in Lists
Srinivas Iyyer said unto the world upon 2005-01-27 01:17: Dear Jacob, thank you for your suggestion. however, i think my question was not clear. what i meant to ask in my previous question was, how to know which elements repeated and how many times they were repeated. while my question was flying, i did a small test: took a list: a [1, 1, 2, 3, 4, 2, 2] wanted to know which elements repeated and how many times: for i in range(len(a)): for k in range(len(a)): if i != k: if a[i] == a[k]: print a[i] break In this very huge list (:-) kidding) of 7 elements, it is easy know by the above primitive code that there 1 s repeated two times, and 2 s repeated three times and finally 1 and 2 numbers are repeated in list a: With sets option i get a list of unique elements, but by no means i get to know which elements were repeated and how many times. With the above primitive code , I know that 1 and 2 are peated. However, in case where there are thousands of entries, how can I get to kno wich elements got repeated and how many times. Do you see any flaws in this code, how can that be made to look more pyhonian way. Hope my question is clear now and appreciate your suggestions. Thank you in advance Srini Hi Srini, for the task of finding out which items are repeated and how many times, I'd do this: def dups_in_list_report(a_list): '''Prints a duplication report for a list.''' items_dict = {} for i in a_list: if i in items_dict: items_dict[i] = items_dict[i] + 1 else: items_dict[i] = 1 for key in items_dict.copy(): # Try it without the .copy() if items_dict[key] == 1:# and see what happens. del items_dict[key] dict_keys = items_dict.keys() dict_keys.sort() for key in dict_keys: print '%s occurred %s times' %(key, items_dict[key]) f = [1,1,2,3,3,3,3,4,4,4,4,4,4,4,5] dups_in_list_report(f) And, now that I get back on-line, I see that Chad posted the same basic idea. But, perhaps the extra stuff here is of use, too. HTH, Brian vdB ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Should this be a list comprehension or something?
Terry Carroll wrote: > My goal here is not efficiency of the code, but efficiency in my Python thinking; so I'll be thinking, for example, "ah, this should be a list comprehension" instead of a knee-jerk reaction to use a for loop. as Alan says, list comprehensions, like map should be used to generate new lists. What you should have thought was -- hmm, this looks like a job for reduce! def sumWater(w1, w2): total_mass = w1.mass + w2.mass numerator = (w1.mass * w1.temperature) + (w2.mass * w2.temperature) return Water(total_mass, numerator / total_mass) def CombineWater(WaterList): return reduce(sumWater, WaterList) if __name__ == '__main__': w1 = Water(50,0) w2 = Water(50,100) w3 = Water(25,50) print CombineWater2((w1,w2,w3)) Now, the sum_water function could also be folded into the Water class as an __add__ method if you like as someone else suggested. See, the problem with your old code was that the list walking and the summation were done by the same code. By pulling sumWater out you can test it separately. You could also pass different functions to CombineWater. And now, for the pedant in me. I would recommend against naming functions with initial capital letters. In many languages, this implies a new type (like your Water class). so CombineWater should be combineWater. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Should this be a list comprehension or something?
Sean Perry said unto the world upon 2005-01-27 02:13: And now, for the pedant in me. I would recommend against naming functions with initial capital letters. In many languages, this implies a new type (like your Water class). so CombineWater should be combineWater. Do you mean implies by the dominant coding conventions, or by language syntax? (Indulging the curious pedant in me.) Best, Brian vdB ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor