Re: Idea for key parameter in all() builting, would it be feasible?
>All you need is the iterator version of map(). In Python 3, that's the >normal map(); in Python 2, use this: from itertools import imap all(imap(lambda x: bool(x), xrange(10**9))) >False >It's roughly instant, like you would expect. >ChrisA This probably isn't the way to post a reply on your own thread (I added an angle bracket to every line above :P) but last time clicked reply to author not knowing that is email. Anyways... Thanks, I like itertools but had no idea imap. Although I did suspect Python3 would have something to say about this just to make me envious :D It works like a charm! -- http://mail.python.org/mailman/listinfo/python-list
Idea for key parameter in all() builting, would it be feasible?
I was mucking around, trying to code a prime sieve in one line. I don't know about filters and bit shifting and stuff like that but I thought I could do it with builtins, albeit a very long one line. This is the part of my stupid trick in question that got me wondering about a key parameter for all() and the "why" is below that. [n for n in xrange(3, int(pow(upper, 0.5) + 1), 2) if all(map(lambda d: n%d!=0, xrange(2, int(pow(n, 0.5) + 1] Where upper is the upper bound for the sieve. That list comprehension will provide a list of prime numbers up to the square root of upper using trial division. Then that list was going to be used for the rest of the sieve using the set.difference method to remove multiples of all those primes. That's when I realized that all() doesn't necessarily go through every item in the alterable. It's stops the moment it finds a false value. You can test that that's true with. >>> all(xrange(10**9)) It's instant because 0 is the false value and so it stops and returns false after only checking the first value. Also because we're using xrange the generator function cousin of range. The following on the other hand should take a while. >>> all(xrange(1, 10**9)) And the following, although the same thing really as all(xrange(10**9)), is not as instant and will take even longer than the above. >>> all(map(lambda x: bool(x), xrange(10**9))) However if all by some chance (I don't know how this stuff works underneath) has a key parameter then we could do something like. >>> all(xrange(10**9), key=lambda x: bool(x)) Which would return False instantly (ideally). -- http://mail.python.org/mailman/listinfo/python-list
Re: My son wants me to teach him Python
I couldn't read every post here so don't know if this has been suggested, or if there is perhaps a better suggestion which I haven't read in this thread, but in as far as I've read I feel the need to recommend: learnpythonthehardway.org Knowing a little JavaScript and even allot of HTML doesn't take him out of the total noob category when it comes to programming (did someone say game programming? Hold your horses!). I took a visual basic course (which I dropped out of admittedly after 3 months) and still knew absolutely nothing, which isn't necessarily just because I'm dumb. After eventually learning Python in incremental and sporadic episodes of free time, I did come across a few resources and by virtue of the frustration of having taken so long to learn to code in the easiest damn programming language to learn, I found myself scrutinizing allot of the tutorials I'd been passing by. I noticed developers.google.com somewhere up there. That's just a no no. Sorry. Maybe some of the people here are more than "pretty smart" but there's a good chance it'll be over his head at first, and at first is a bad place to be in over your head when you're learning the fundamentals. I also notice Invent with python. I personally would go for 2.x rather than 3 but that aside, for reasons I'm too tired to word, I didn't find it a good fit for me. I takes a "dive right in" approach and well, I never did learn to swim. Udacity was the third suggestion I noticed. This is also a no no. I completed the cs101 udacity course which I'm sure is the course in question here, and I loved it! Really I learn crap load from it, but at every step I asked myself, would this had helped if it was the first place I went to to learn to code? No. There were allot of gaps I noticed when looking from a complete beginners perspective and even though the course claims to has no prerequisites, I would have hated if I started with that. However that was last year and I think it was only a few months old, so it may be allot different now, I haven't checked. I read How to think like a computer scientist, A byte of python, and even the official docs. The only one I came across that made me say "&*#! why didn't I google that?" was learnpythonthehardway. I do think it depends a great deal on the individual, and for me personally, that style of learning was just it. For one you learn from the bottom up. It's a compulsion for some to know that that know a thing before they're brave enough to move forward. In cases where a "leap" is the only way forward, the tutor pulls you across that divide by your ankles. You feel a sense of obligation to take to his instruction. And above all, it greatly emphasizes the "learn by doing" approach, in small steps, not big projects that you end up completing just to get through it but don't learn much from. So that's my recommendation. But all that aside, my biggest point would be, just pick one do it. As you can see if you read that, my biggest flaw was simply the lack of devotion to one path. Game programming if he still wants to do that is another question entirely I feel. Fundamentals are fundamentals. The only variable is how long it might take him to get passed it. Even with Python, some people just never get it. -- http://mail.python.org/mailman/listinfo/python-list
Re: Simple program question.
input() is a function which returns a string. You can assign this return value to a variable. That's what variables are for. option = input() Now you can use the variable named option in place of all those calls to input(). i.e: ...instead of.. if input() == 'parry': # etc ...do this... option = input() if option == 'parry': # etc elif option == 'whatever': # etc -- http://mail.python.org/mailman/listinfo/python-list
Re: Reply to post 'Tryign to add a valkue to a set'
Just try this in the interpreter and see. for key, value in sorted(months.items(), key=lambda x:x[1]): print "%s %s" % (value, key) -- http://mail.python.org/mailman/listinfo/python-list
Re: Reply to post 'Tryign to add a valkue to a set'
for key, value in sorted(months.items(), key=lambda x:x[1]): print("""'\t%s'\n""" % (value, key)) Explanation: - - - - - - dict.items is a method associated with dicts just like dict.keys or dict.values, and returns a list of (key, value) pairs. sorted and some other builtin functions have an optional key argument, which tells the function what exactly to look at when sorting the sequence. (I say sequence because you can sort a string or tuple or dict and get a list back in return). In this case we use a simple lambda function to tell it to look at the value not the key (i.e. (key, value)[1] returns value) Alternatively you could do this. def get_value(item): return item[1] for key, value in sorted(months.items(), key=get_value): ...etc... You might also wonder what this would do for key, value in sorted(months.items()): ...etc... Which becomes a question of what would something like this do sorted([(1,3), (2,2), (3,1)]) Well sorted is a function that expects to get some iterable (sequence of some kinda items) to sort. If those items happens to be sequences themselves, like (key, value) which is a sequence of two items, then it's only going to care about the first item and ignore the rest (i.e ignore value). So the above will return [(1,3), (2,2), (3,1)] which as far as sorted() is concerned, is already sorted. About the string being printed: - - - - - - - - - - - - - - - - I'm not sure why or if you want a single quotation around the entire element to be printed as well but if so then that's how I'd do it. The \t is for a tab. You also don't have to worry about adding newlines because print will print each statement on a newline anyways by default unless you tell it otherwise by specifying the sep argument. Sorry my first post, I said allot didn't I. -- http://mail.python.org/mailman/listinfo/python-list