Re: [Tutor] Nested list comprehensions
On 15/05/06, Kent Johnson <[EMAIL PROTECTED]> wrote: > You can come pretty close with generators, though it hurts to think > about what is actually going on behind the scenes here: > > In [1]: import itertools > > In [2]: def fibs(): > ...: yield 0 > ...: yield 1 > ...: fib1 = fibs() > ...: fib2 = fibs() > ...: fib2.next() > ...: for a, b in itertools.izip(fib1, fib2): > ...: yield a+b Yikes! f = fibs() for i in range(100): start = time.clock() x = f.next() dur = time.clock() - start print i, x, dur if dur > 10: break 0 0 2.03936533833e-005 1 1 5.5873022968e-006 2 1 1.59238115459e-005 3 2 1.25714301678e-005 4 3 1.9580388e-005 5 5 2.1538427e-005 6 8 2.9370582e-005 7 13 6.0699203e-005 8 21 7.87809623849e-005 9 34 0.000119568269152 10 55 0.000383568302675 11 89 0.000409269893241 12 144 0.000650082622233 13 233 0.000979454092629 14 377 0.00172200656787 15 610 0.00713330884232 16 987 0.00450979104886 17 1597 0.0128720270314 18 2584 0.0150373860365 19 4181 0.0283779083654 20 6765 0.0490199173359 21 10946 0.135228918759 22 17711 0.240615497221 23 28657 0.365666586116 24 46368 0.827867508301 25 75025 2.14721368219 26 121393 4.08266218193 27 196418 20.1769099145 Hmm, do you know an easy way to check how much memory python is using? My HDD was swapping like crazy by the end of that.. -- John. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Nested list comprehensions
John Fouhy wrote: > On 15/05/06, w chun <[EMAIL PROTECTED]> wrote: >> anyone know if list comps work the same way in haskell? > > Slightly not what you asked, but you can do some funky things with > list comprehensions in Haskell. > > Check this out: > > fibs = 0 : 1 : [ a + b | (a, b) <- zip fibs (tail fibs)] > > The python translation would be something like: > > fibs = [0, 1] + [ a+b for (a, b) in zip(fibs, fibs[1:])] > > But you can't quite do that in python :-) You can come pretty close with generators, though it hurts to think about what is actually going on behind the scenes here: In [1]: import itertools In [2]: def fibs(): ...: yield 0 ...: yield 1 ...: fib1 = fibs() ...: fib2 = fibs() ...: fib2.next() ...: for a, b in itertools.izip(fib1, fib2): ...: yield a+b ...: ...: In [3]: f=fibs() In [4]: [f.next() for i in range(10)] Out[4]: [0, 1, 1, 2, 3, 5, 8, 13, 21, 34] Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Nested list comprehensions
On 15/05/06, w chun <[EMAIL PROTECTED]> wrote: > anyone know if list comps work the same way in haskell? Slightly not what you asked, but you can do some funky things with list comprehensions in Haskell. Check this out: fibs = 0 : 1 : [ a + b | (a, b) <- zip fibs (tail fibs)] The python translation would be something like: fibs = [0, 1] + [ a+b for (a, b) in zip(fibs, fibs[1:])] But you can't quite do that in python :-) -- John. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Nested list comprehensions
i agree with everyone above, kent, alan, roel... in fact, i have do a doubletake everytime i write code like this and have to look things up and/or play with some sample code as you all have done, just to make sure i got it working. (kinda reminds of going back to look at a perl script i wrote... have to check with the camel book to figure out what i really did.) i like alan's idea of splitting up the loops onto different lines so that you can more clearly see the loops. the syntax does support it altho it seems kind of clunky as: print\ [ j for i in a for j in f(i) ] originally, i thought that this would work: print [j for j in [f(i) for i in a]] but then realized that because of the inner grouping, it works just the same as the original and obvious print [f(i) for i in a] i think the point of the EXPR in... [EXPR for i in XXX for j in YYY for k in ZZZ...] ...is that EXPR is an expression that can be made up of components of any of the inner loops, i.e., i, j, k, etc., so you should really just think about the loops inside. perhaps you can consider that you're digging a hole and jumping into it. while inside the hole, you dig another one, and jump into *that*, ad nauseum. back at the top, you're only collating the things you want to save and have thrown out from all the holes you've dug yourself into. :-) anyone know if list comps work the same way in haskell? -- wesley - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - "Core Python Programming", Prentice Hall, (c)2007,2001 http://corepython.com wesley.j.chun :: wescpy-at-gmail.com python training and technical consulting cyberweb.consulting : silicon valley, ca http://cyberwebconsulting.com ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Database topic now fixed
Alan, Does that mean you will soon fix the tutor.tgz :-) Thanks for all the hard work you put into this. Stan. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] How do I create the equivalent of a Java class in Python?
> How do I create the equivalent of a Java class in Python? I've been looking> at the reference, and it's been confusing to me at least. Adapted from my book: Java code: class Msg{ private String txt; public Msg(String s){ this.txt = s; } public void say(){ if (this.txt == "") System.out.println("No message"); else System.out.println(this.txt); } } Python code: class Msg: def __init__(self,s): self.s = s def say(self): if self.s: print "No message" else: print self.s Does that help? There is more on writing classes in the OOP topic of my tutor. Alan GauldAuthor of the Learn to Program web sitehttp://www.freenetpages.co.uk/hp/alan.gauld ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Treeview
Hi, I have managed to find the solution to my problem. I have defined iter as Iter = model.get_iter(combo3) The code now works. I will show it below for completeness: combo3 = self.wTree.get_widget("treeview1") model=gtk.TreeStore(gobject.TYPE_STRING,gobject.TYPE_STRING,gobject.TYPE_STRING,gobject.TYPE_STRING,gobject.TYPE_STRING,gobject.TYPE_STRING,gobject.TYPE_STRING,gobject.TYPE_STRING) self.hostsmodel = model combo3.set_model(model) combo3.connect("row_activated", self.callback53, combo3,model) def callback53(self,data,combo3,data2,data3,model): lookup = [] iter = model.get_iter(combo3) counter = 0 while counter < 8: result = model.get_value(iter,counter) lookup.append(result) counter = counter + 1 print lookup Technically this is the first solution that I have been able to post on the mailing list. Does this mean I am improving? Or does it not count as I have solved my own problem? Regards, John. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor