Re: [Tutor] Determine Filetype
Thankyou guys. > The search feature of the web site is quite effective! :-) Alan, i know that every site has a search function, but they're not so simple to be used if you're looking for a very common word ("in") and don't know other keys to find it. Regards, Giorgio ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] What is this an example of (and how can i use it?)
I am afraid that in the long layoff in python has meant some new constructs have passed me by. In googling around I found some nice little code I want to use, but i don't quite understand it, how it is called, and what it is an example of. I guess there are generators and iterators now and it seems this might be an example of one of those new constructs. Can anyone explain what it is i am looking at, how it is called, and what it is an example of so that I can look it up: def roundrobin(*iterables): "roundrobin('ABC', 'D', 'EF') --> A D E B F C" # Recipe credited to George Sakkis pending = len(iterables) nexts = cycle(iter(it).next for it in iterables) while pending: try: for next in nexts: yield next() except StopIteration: pending -= 1 nexts = cycle(islice(nexts, pending)) ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] list sort problem solved
list = bigMethod() # this method makes the list list.sort() # here is the sort thanks for x in list: # out put the sort print x it all works Thanks a lot -- From: "Dave Angel" Sent: Saturday, September 19, 2009 7:46 PM To: "Rayon" Cc: Subject: Re: [Tutor] list sort problem Rayon wrote: ok so here it is I think this one should be very clear: I have some data in a list, the data in question: 0.0046,0.095,0.0904,521456,['MCI 521456 0.0904'],['ATT 521 0.0919'],['IDT 521 0.095'],['None'] 0.0083,0.0192,0.0109,39023821,['MCI 39023821 0.0109'],['ATT 39 0.012'],['IDT 39 0.0192'],['SPR 39 0.0135'] 0.042,0.0681,0.026,73462,['MCI 73462 0.0260'],['ATT 7 0.026'],['IDT 73462 0.028'],['SPR 7 0.0681'] 0.0176,0.1035,0.0859,126872,['MCI 126872 0.0859'],['ATT 1268 0.0919'],['IDT 126872 0.1035'],['None'] 0.0215,0.1614,0.1399,5032130,['MCI 5032130 0.1614'],['ATT 5032130 0.1399'],['IDT 503 0.152'],['None'] 0.0206,0.0385,0.0179,1868,['MCI 1868 0.0179'],['ATT 1868 0.0385'],['IDT 1868 0.036'],['None'] 0.0325,0.087,0.0545,5027889,['MCI 5027889 0.0602'],['ATT 5027889 0.0545'],['IDT 502 0.087'],['None'] 0.0325,0.087,0.0545,5027888,['MCI 5027888 0.0602'],['ATT 5027888 0.0545'],['IDT 502 0.087'],['None'] 0.0046,0.095,0.0904,521455,['MCI 521455 0.0904'],['ATT 521 0.0919'],['IDT 521 0.095'],['None'] 0.1292,0.1762,0.047,5989,['MCI 5989 0.1762'],['ATT 598 0.05'],['IDT 5989 0.173'],['SPR 598 0.047'] 0.0706,0.2011,0.1305,1284499,['MCI 1284499 0.2011'],['ATT 1284499 0.1932'],['IDT 1284499 0.1305'],['None'] 0.0325,0.087,0.0545,5027881,['MCI 5027881 0.0602'],['ATT 5027881 0.0545'],['IDT 502 0.087'],['None'] my problem is that I want to order it by the first item so for line number one that would be 0.0046: this value is not unique it might repeat, if the last 10 values like this that might be the same and there are the lowest in the over list. I want them first when I display this list and the bigger values after in order is size. I'll have to make some assumptions, please correct or confirm them. Each line of your message represents one item in a list. Each such item is in turn a sublist. Each of these sublists contains 3 floats, an int, and a few more subsublists Each of these subsublists contains a string Anyway, you want to sort the top-level list. Since the first item in each sublist is a float, by default they will be ordered by the first item, which is what you're asking for. In case of a collision, the second item is examined, and so on. mylist.sort() will sort the list in-place, based on the first element of each item in the list mylist = [] mylist.append( [0.0046,0.095,0.0904,521456,['MCI 521456 0.0904'],['ATT 521 0.0919'],['IDT 521 0.095'],['None']]) mylist.append( [0.0083,0.0192,0.0109,39023821,['MCI 39023821 0.0109'],['ATT 39 0.012'],['IDT 39 0.0192'],['SPR 39 0.0135']]) mylist.append( [0.042,0.0681,0.026,73462,['MCI 73462 0.0260'],['ATT 7 0.026'],['IDT 73462 0.028'],['SPR 7 0.0681']]) mylist.append( [0.0176,0.1035,0.0859,126872,['MCI 126872 0.0859'],['ATT 1268 0.0919'],['IDT 126872 0.1035'],['None']]) mylist.append( [0.0215,0.1614,0.1399,5032130,['MCI 5032130 0.1614'],['ATT 5032130 0.1399'],['IDT 503 0.152'],['None']]) mylist.append( [0.0206,0.0385,0.0179,1868,['MCI 1868 0.0179'],['ATT 1868 0.0385'],['IDT 1868 0.036'],['None']]) mylist.append( [0.0325,0.087,0.0545,5027889,['MCI 5027889 0.0602'],['ATT 5027889 0.0545'],['IDT 502 0.087'],['None']]) mylist.append( [0.0325,0.087,0.0545,5027888,['MCI 5027888 0.0602'],['ATT 5027888 0.0545'],['IDT 502 0.087'],['None']]) mylist.append( [0.0046,0.095,0.0904,521455,['MCI 521455 0.0904'],['ATT 521 0.0919'],['IDT 521 0.095'],['None']]) mylist.append( [0.1292,0.1762,0.047,5989,['MCI 5989 0.1762'],['ATT 598 0.05'],['IDT 5989 0.173'],['SPR 598 0.047']]) mylist.append( [0.0706,0.2011,0.1305,1284499,['MCI 1284499 0.2011'],['ATT 1284499 0.1932'],['IDT 1284499 0.1305'],['None']]) mylist.append( [0.0325,0.087,0.0545,5027881,['MCI 5027881 0.0602'],['ATT 5027881 0.0545'],['IDT 502 0.087'],['None']]) def show_list_partial(thelist): for item in thelist: print item[0], item[1] show_list_partial(mylist) mylist.sort() print "" show_list_partial(mylist) DaveA. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] list sort problem solved
Rayon wrote: list = bigMethod() # this method makes the list list.sort() # here is the sort thanks for x in list: # out put the sort print x it all works Thanks a lot -- From: "Dave Angel" Sent: Saturday, September 19, 2009 7:46 PM To: "Rayon" Cc: Subject: Re: [Tutor] list sort problem Rayon wrote: ok so here it is I think this one should be very clear: I have some data in a list, the data in question: 0.0046,0.095,0.0904,521456,['MCI 521456 0.0904'],['ATT 521 0.0919'],['IDT 521 0.095'],['None'] 0.0083,0.0192,0.0109,39023821,['MCI 39023821 0.0109'],['ATT 39 0.012'],['IDT 39 0.0192'],['SPR 39 0.0135'] 0.042,0.0681,0.026,73462,['MCI 73462 0.0260'],['ATT 7 0.026'],['IDT 73462 0.028'],['SPR 7 0.0681'] 0.0176,0.1035,0.0859,126872,['MCI 126872 0.0859'],['ATT 1268 0.0919'],['IDT 126872 0.1035'],['None'] 0.0215,0.1614,0.1399,5032130,['MCI 5032130 0.1614'],['ATT 5032130 0.1399'],['IDT 503 0.152'],['None'] 0.0206,0.0385,0.0179,1868,['MCI 1868 0.0179'],['ATT 1868 0.0385'],['IDT 1868 0.036'],['None'] 0.0325,0.087,0.0545,5027889,['MCI 5027889 0.0602'],['ATT 5027889 0.0545'],['IDT 502 0.087'],['None'] 0.0325,0.087,0.0545,5027888,['MCI 5027888 0.0602'],['ATT 5027888 0.0545'],['IDT 502 0.087'],['None'] 0.0046,0.095,0.0904,521455,['MCI 521455 0.0904'],['ATT 521 0.0919'],['IDT 521 0.095'],['None'] 0.1292,0.1762,0.047,5989,['MCI 5989 0.1762'],['ATT 598 0.05'],['IDT 5989 0.173'],['SPR 598 0.047'] 0.0706,0.2011,0.1305,1284499,['MCI 1284499 0.2011'],['ATT 1284499 0.1932'],['IDT 1284499 0.1305'],['None'] 0.0325,0.087,0.0545,5027881,['MCI 5027881 0.0602'],['ATT 5027881 0.0545'],['IDT 502 0.087'],['None'] my problem is that I want to order it by the first item so for line number one that would be 0.0046: this value is not unique it might repeat, if the last 10 values like this that might be the same and there are the lowest in the over list. I want them first when I display this list and the bigger values after in order is size. I'll have to make some assumptions, please correct or confirm them. Each line of your message represents one item in a list. Each such item is in turn a sublist. Each of these sublists contains 3 floats, an int, and a few more subsublists Each of these subsublists contains a string Anyway, you want to sort the top-level list. Since the first item in each sublist is a float, by default they will be ordered by the first item, which is what you're asking for. In case of a collision, the second item is examined, and so on. mylist.sort() will sort the list in-place, based on the first element of each item in the list mylist = [] mylist.append( [0.0046,0.095,0.0904,521456,['MCI 521456 0.0904'],['ATT 521 0.0919'],['IDT 521 0.095'],['None']]) mylist.append( [0.0083,0.0192,0.0109,39023821,['MCI 39023821 0.0109'],['ATT 39 0.012'],['IDT 39 0.0192'],['SPR 39 0.0135']]) mylist.append( [0.042,0.0681,0.026,73462,['MCI 73462 0.0260'],['ATT 7 0.026'],['IDT 73462 0.028'],['SPR 7 0.0681']]) mylist.append( [0.0176,0.1035,0.0859,126872,['MCI 126872 0.0859'],['ATT 1268 0.0919'],['IDT 126872 0.1035'],['None']]) mylist.append( [0.0215,0.1614,0.1399,5032130,['MCI 5032130 0.1614'],['ATT 5032130 0.1399'],['IDT 503 0.152'],['None']]) mylist.append( [0.0206,0.0385,0.0179,1868,['MCI 1868 0.0179'],['ATT 1868 0.0385'],['IDT 1868 0.036'],['None']]) mylist.append( [0.0325,0.087,0.0545,5027889,['MCI 5027889 0.0602'],['ATT 5027889 0.0545'],['IDT 502 0.087'],['None']]) mylist.append( [0.0325,0.087,0.0545,5027888,['MCI 5027888 0.0602'],['ATT 5027888 0.0545'],['IDT 502 0.087'],['None']]) mylist.append( [0.0046,0.095,0.0904,521455,['MCI 521455 0.0904'],['ATT 521 0.0919'],['IDT 521 0.095'],['None']]) mylist.append( [0.1292,0.1762,0.047,5989,['MCI 5989 0.1762'],['ATT 598 0.05'],['IDT 5989 0.173'],['SPR 598 0.047']]) mylist.append( [0.0706,0.2011,0.1305,1284499,['MCI 1284499 0.2011'],['ATT 1284499 0.1932'],['IDT 1284499 0.1305'],['None']]) mylist.append( [0.0325,0.087,0.0545,5027881,['MCI 5027881 0.0602'],['ATT 5027881 0.0545'],['IDT 502 0.087'],['None']]) def show_list_partial(thelist): for item in thelist: print item[0], item[1] show_list_partial(mylist) mylist.sort() print "" show_list_partial(mylist) DaveA. Two comments: 1) don't top-post. In these forums, it really confuses things to put your message out of order. 2) list already has a meaning in Python. Name your attribute something else. I used mylist in my example. Better would be something plural that's descriptive of what's contained in this list, such as shoes= big_method() ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] What is this an example of (and how can i use it?)
On Sun, Sep 20, 2009 at 9:10 AM, kevin parks wrote: > I am afraid that in the long layoff in python has meant some new constructs > have passed me by. In googling around I found some nice little code I want > to use, but i don't quite understand it, how it is called, and what it is an > example of. I guess there are generators and iterators now and it seems this > might be an example of one of those new constructs. Can anyone explain what > it is i am looking at, how it is called, and what it is an example of so > that I can look it up: > > def roundrobin(*iterables): > "roundrobin('ABC', 'D', 'EF') --> A D E B F C" > # Recipe credited to George Sakkis The original is here, with an explanation of what it does. > pending = len(iterables) > nexts = cycle(iter(it).next for it in iterables) cycle() is part of itertools: http://docs.python.org/library/itertools.html#itertools.cycle You can read about iter() and iterators here: http://docs.python.org/library/functions.html#iter http://docs.python.org/glossary.html#term-iterator (iter(it).next for it in iterables) is a generator expression. It creates an iterator. The above statement as a whole makes an iterator which will return the iterators of the arguments in turn, repeatedly. > while pending: > try: > for next in nexts: > yield next() The yield statement makes this into a generator function. It's return value is a generator - a kind of iterator. > except StopIteration: next() will raise StopIteration when its underlying iterable is exhausted. > pending -= 1 > nexts = cycle(islice(nexts, pending)) This is kind of tricky - it makes a new cycle of iterators that omits the one that just finished. I have a writeup of iterators and generators here: http://personalpages.tds.net/~kent37/kk/4.html HTH, Kent ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Determine Filetype
ad...@gg-lab.net wrote: The search feature of the web site is quite effective! :-) simple to be used if you're looking for a very common word ("in") and don't know other keys to find it. To be honest I was being a bit optimistic but I did just type "in" for the search and the first 5 or so hits had all the right bits... Alan G ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] What is this an example of (and how can i use it?)
kevin parks wrote: called, and what it is an example of. I guess there are generators and iterators now and it seems this might be an example of one of those new This is a generator expression. It is like a list comprehension (you know about those right?) except it doesn't create the list it just returns each item on demand. You could think of a list as a list constructed using a generator expression. def roundrobin(*iterables): "roundrobin('ABC', 'D', 'EF') --> A D E B F C" # Recipe credited to George Sakkis pending = len(iterables) nexts = cycle(iter(it).next for it in iterables) note this is storing the next methods not the results of them. while pending: try: for next in nexts: yield next() So the yield calls the stored method and returns the result. HTH, Alan G. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] What is this an example of (and how can i use it?)
On Sep 21, 2009, at 1:32 AM, Alan Gauld wrote: kevin parks wrote: called, and what it is an example of. I guess there are generators and iterators now and it seems this might be an example of one of those new This is a generator expression. That's unfortunate news for me. It is like a list comprehension (you know about those right?) Yes. I know and use and love them daily. Even if there were implemented backwards :) [for x in range(10) x**2] would have been easier than: [x**2 for x in range(10)] But i am used to it now. except it doesn't create the list it just returns each item on demand. You could think of a list as a list constructed using a generator expression. def roundrobin(*iterables): "roundrobin('ABC', 'D', 'EF') --> A D E B F C" # Recipe credited to George Sakkis pending = len(iterables) nexts = cycle(iter(it).next for it in iterables) note this is storing the next methods not the results of them. while pending: try: for next in nexts: yield next() So the yield calls the stored method and returns the result. So... then to call (by call i mean use/execute/doit) i would do, what? foo.next() I kinda understand conceptually what iterators and generators do and why they are "a honking good idea" (why create 100 of x when we just want the 100th, etc.) what i don't get is the syntax and how they are used in real life. How generator and iterators behave in the wild. I am also bummed since generators have methods, which means they are OO which means i am i'd be in for 16 years of computer science study and super arcane, obscure and opaque concepts like what to do with __self__ and all that junk before i can use them. Anyway i needed a pea shooter that does a round robin. This one does it, but i don't know how to use it. I read up on gennies and itties and see if i can get my head around it. They are really poorly addressed nearly everywhere i look. They are explained so that really smart folks who know a lot of CS and are fluent in 15 computer languages can understand them, but not us mortals. Even the Lutz is too terse and generally poor on these two complex and relatively new constructs. They are a dark and obscure magic. I'll try the links Kent pointed me to first and see how that goes. thanks, -kp-- ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] real world decorators
Hi, I think I understand what decorators are and how they work. Maybe it's just me but I don't know where I'd use them in my real world programming. I see how they work with profile or coverage but does anyone have real world uses. I mostly create wxPython apps and don't see where they might apply. I know the tutors will enlighten me! Johnf ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] eazy python question involving functions and parameters
assume that jade2 is a function that expects two int parameters and returns the value of the larger one. Also assume that four variables, population1 , population2 , population3 , and population4 have already been defined and associated with int values. Write an expression (not a statement!) whose value is the largest of population1 , population2 , population3 , and population4 by calling jade2 . how can i do this??? -- View this message in context: http://www.nabble.com/eazy-python-question-involving-functions-and-parameters-tp25530128p25530128.html Sent from the Python - tutor mailing list archive at Nabble.com. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] eazy python question involving functions and parameters
On Sun, Sep 20, 2009 at 01:19:08PM -0700, daggerdvm wrote: > assume that jade2 is a function that expects two int parameters and returns > the value of the larger one. This really smells like a school assignment, which as a general policy we don't solve for you. But as a hint in the right direction, you might look at how parameters are passed to functions in Python (and it's irrelevant that they are int values as far as parameter passing goes, but that will simplify your expression for the sake of keeping the assignment easy to do). You might want to look at some built-in functions which will look at a list of values and tell you which is biggest or smallest. What do you think you can do with those pieces? > > Also assume that four variables, population1 , population2 , population3 > , and population4 have already been defined and associated with int > values. > > Write an expression (not a statement!) whose value is the largest of > population1 , population2 , population3 , and population4 by calling > jade2 . > > how can i do this??? > > > -- > View this message in context: > http://www.nabble.com/eazy-python-question-involving-functions-and-parameters-tp25530128p25530128.html > Sent from the Python - tutor mailing list archive at Nabble.com. > > ___ > Tutor maillist - Tutor@python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor -- Steve Willoughby| Using billion-dollar satellites st...@alchemy.com | to hunt for Tupperware. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] real world decorators
On Sunday 20 September 2009, John wrote: > Hi, > > I think I understand what decorators are and how they work. Maybe > it's just me but I don't know where I'd use them in my real world > programming. I see how they work with profile or coverage but does > anyone have real world uses. Frequently used are @classmethod and @staticmethod: http://docs.python.org/library/functions.html#classmethod An other interesting usage of decorators is Phillip J. Eby's 'simplegeneric' library, where decorated functions replace big if... elif... constructions: http://cheeseshop.python.org/pypi/simplegeneric/0.6 But really decorators are just syntactical sugar. They are an elegant way to write a special case of a function call. Kind regards, Eike. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] eazy python question involving functions and parameters
On Mon, Sep 21, 2009 at 5:19 AM, daggerdvm wrote: > > assume that jade2 is a function that expects two int parameters and returns > the value of the larger one. > > Also assume that four variables, population1 , population2 , population3 > , and population4 have already been defined and associated with int > values. > > Write an expression (not a statement!) whose value is the largest of > population1 , population2 , population3 , and population4 by calling > jade2 . Just think: 4 players left means that this is the semi final. Regards, benno ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] What is this an example of (and how can i use it?)
kevin parks wrote: This is a generator expression. That's unfortunate news for me. It is like a list comprehension (you know about those right?) Yes. I know and use and love them daily. If you can grok comprehensions then you are only a step away from generator expressions. Most of the time you don't need to think about the hairy CS stuff underneath, just use them. I found the jump to using comprehensions much bigger than the jump from them to generator expressions! You could think of a list as a list constructed using a generator expression. should have been a list comprehension as a list nexts = cycle(iter(it).next for it in iterables) note this is storing the next methods not the results of them. while pending: for next in nexts: yield next() So the yield calls the stored method and returns the result. So... then to call (by call i mean use/execute/doit) i would do, what? foo.next() That depends on what next() actually returns. e started off with a list of iterables (eg files strings, lists, tuples - possibly a mixture of them. We then build a list of their next methods. So when we call this methods we get back an instance of whatever was in the original list of iterators. Hee isa slightly less complex example: >>> its = [[1,2,3],'abc'] >>> nxts = [iter(n).next for n in its] >>> for repeats in range(2): for n in nxts: print n() 1 a 2 b So the first for n loop printed out the first item in each iterable, the next time round we printed the second item etc. Each iteration prints a number and a letter So when your yield returns its value you have to do with it whatever is appropriate! I kinda understand conceptually what iterators and generators do and why they are "a honking good idea" (why create 100 of x when we just want the 100th, etc.) what i don't get is the syntax and how they are used in real life. ignore the memory management magic and just think of them returning the next item in a list on demand. Have you ever used xrange() instead of range()? Its the same thing. bummed since generators have methods, which means they are OO So do lists but you said you use lists every day! You use objects everywhere in Python, everything is an object and has methods. try: >>> dir(6) don't be freaked by objects, you use them already every day. means i am i'd be in for 16 years of computer science study and super arcane, obscure and opaque concepts like what to do with __self__ and You can do all sorts of clever black magic type stuff in Python even without OOP but mostly you don't need to, just keep it simple. Anyway i needed a pea shooter that does a round robin. This one does it, but i don't know how to use it. Actually the example you quoted is a fairly sophisticated bit of code that probably does more than you need. What exactly are you trying to achieve, there is probably an easier way! I read up on gennies and itties and see if i can get my head around it. They are really poorly addressed nearly everywhere i look. They are explained so that really smart folks who know a lot of CS and are fluent in 15 computer languages can understand them, but not us mortals. Even They are difficult conceptually, but if you concentrate on the examples of use and don't try to be too ambitious initially they will grow on you like comprehensions did. And remember, you never need them. There are always alternative ways to do it, just using a bit more code usually. HTH, Alan G. http://www.alan-g.me.uk ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] eazy python question involving functions and parameters
Just think: 4 players left means that this is the semi final. What a brilliant answer! It tells him how to do it if he just stops and thinks but gives nothing away. I love it. :-) Alan G. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] real world decorators
On Sun, Sep 20, 2009 at 3:40 PM, John wrote: > Hi, > > I think I understand what decorators are and how they work. Maybe it's just > me but I don't know where I'd use them in my real world programming. I see > how they work with profile or coverage but does anyone have real world uses. Here are some examples: http://wiki.python.org/moin/PythonDecoratorLibrary TurboGears uses decorators to expose functions to the web. Django has decorators for authentication and caching. Kent ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] What is this an example of (and how can i use it?)
On Sun, Sep 20, 2009 at 1:27 PM, kevin parks wrote: > > On Sep 21, 2009, at 1:32 AM, Alan Gauld wrote: > >> kevin parks wrote: >> >>> called, and what it is an example of. I guess there are generators and >>> iterators now and it seems this might be an example of one of those new >> >> This is a generator expression. > > That's unfortunate news for me. Why? > So... then to call (by call i mean use/execute/doit) i would do, what? > foo.next() Are you asking how to use a generator function? There is a simple example on the recipe page (sorry, I omitted the link earlier) http://code.activestate.com/recipes/528936/ >>> list(roundrobin('abc', [], range(4), (True,False))) ['a', 0, True, 'b', 1, False, 'c', 2, 3] Calling a generator function gives you something that can be iterated. You can create a list out of it (by passing it to the list() function) or you can iterate the items in it directly with a for loop. Using the example above, you could say for item in roundrobin('abc', [], range(4), (True,False)): print item > I kinda understand conceptually what iterators and generators do and why > they are "a honking good idea" (why create 100 of x when we just want the > 100th, etc.) what i don't get is the syntax and how they are used in real > life. How generator and iterators behave in the wild. It's really not that bad. They are just a generalization of what you have already been doing with lists. > Even the Lutz is too > terse and generally poor on these two complex and relatively new constructs. > They are a dark and obscure magic. No, really they are not difficult. Read my essay and ask questions if you don't understand. Kent ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] eazy python question involving functions and, parameters
assume that jade2 is a function that expects two int parameters and returns the value of the larger one. Also assume that four variables, population1 , population2 , population3 , and population4 have already been defined and associated with int values. Write an expression (not a statement!) whose value is the largest of population1 , population2 , population3 , and population4 by calling jade2 . how can i do this??? What are you trying to do?? I would like to help, but like Alan said, it really does reek of school. I don't quite understand what you are saying, could you elaborate? <>___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] mailing list submission
-- have a good day ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor