Re: [Tutor] List comprehension question

2010-11-13 Thread Steven D'Aprano
Richard D. Moores wrote: On Fri, Nov 12, 2010 at 15:26, Steven D'Aprano wrote: best = t.repeat(number=1, repeat=5) print round(best, 3) t.repeat(number=1, repeat=5) is a list of the 5 times, not the best of the 5. Oops! Good catch. Sorry about that. -- Steven

Re: [Tutor] List comprehension question

2010-11-12 Thread Richard D. Moores
On Fri, Nov 12, 2010 at 23:25, Richard D. Moores wrote: > See Sorry, forgot to paste. Dick ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: h

Re: [Tutor] List comprehension question

2010-11-12 Thread Richard D. Moores
On Fri, Nov 12, 2010 at 15:26, Steven D'Aprano wrote: > Richard D. Moores wrote: > >> OK, but why can't I do what the timeit doc suggests, only put 2 or >> more functions in the file, as I do here: >> >> >> def test(): >>    "Stupid test function" >>    L =

Re: [Tutor] List comprehension question

2010-11-12 Thread Steven D'Aprano
Richard D. Moores wrote: OK, but why can't I do what the timeit doc suggests, only put 2 or more functions in the file, as I do here: def test(): "Stupid test function" L = [] for i in range(100): L.append(i) if __name__=='__main__':

Re: [Tutor] List comprehension question

2010-11-12 Thread Richard D. Moores
On Fri, Nov 12, 2010 at 07:13, David Hutto wrote: > import timeit > > > def anyName(): >        pass > > t = timeit.Timer('anyName()','from __main__ import anyName') > print t.repeat(repeat=5) > > > If I get the gist of what you're asking. > Yes. That's it! Thank you! And if I don't want the def

Re: [Tutor] List comprehension question

2010-11-12 Thread David Hutto
Apologies, missed that part. Didn't mean to seem rude. import timeit def anyName(): pass for num in range(10): t = timeit.Timer('anyName()','from __main__ import anyName') print t.repeat(repeat=5) #or import timeit def anyName(): pass t = timeit.Timer('anyNa

Re: [Tutor] List comprehension question

2010-11-12 Thread Richard D. Moores
On Fri, Nov 12, 2010 at 05:15, David Hutto wrote: >>        repeatedly, returning a list of results. ... >> >> I'm sorry, Steven, but I could you revise this code to use repeat=5 >> instead of the for loop? I can't see how to do it. > help(timeit.Timer > >  repeat(self, repeat=3, number=1

Re: [Tutor] List comprehension question

2010-11-12 Thread David Hutto
>        repeatedly, returning a list of results. ... > > I'm sorry, Steven, but I could you revise this code to use repeat=5 > instead of the for loop? I can't see how to do it. >>> help(timeit.Timer repeat(self, repeat=3, number=100) | Call timeit() a few times. | | This is a

Re: [Tutor] List comprehension question

2010-11-12 Thread Richard D. Moores
On Fri, Nov 12, 2010 at 02:11, Steven D'Aprano wrote: > Richard D. Moores wrote: > >> I find using that at the interactive prompt a bit onerous -- lots of >> copy and pasting. And doubly so when comparing times for 2 or more >> functions. > > Does your Python not support readline? Normally, if you

Re: [Tutor] List comprehension question

2010-11-12 Thread Steven D'Aprano
Richard D. Moores wrote: I find using that at the interactive prompt a bit onerous -- lots of copy and pasting. And doubly so when comparing times for 2 or more functions. Does your Python not support readline? Normally, if you press UP ARROW or DOWN ARROW, Python will cycle through the previ

Re: [Tutor] List comprehension question

2010-11-11 Thread Stefan Behnel
Steven D'Aprano, 12.11.2010 06:07: Richard D. Moores wrote: On Wed, Nov 10, 2010 at 01:30, Steven D'Aprano wrote: P.S. don't take that as a put down -- you should be pleased that your code is around as fast as Tim Peter's code :) Nah. But where is Tim Peter's code? The timeit module was w

Re: [Tutor] List comprehension question

2010-11-11 Thread Steven D'Aprano
Richard D. Moores wrote: On Wed, Nov 10, 2010 at 01:30, Steven D'Aprano wrote: P.S. don't take that as a put down -- you should be pleased that your code is around as fast as Tim Peter's code :) Nah. But where is Tim Peter's code? The timeit module was written by Tim Peters. Since then,

Re: [Tutor] List comprehension question

2010-11-11 Thread Richard D. Moores
On Wed, Nov 10, 2010 at 02:35, Steven D'Aprano wrote: > Richard D. Moores wrote: >> >> On Tue, Nov 9, 2010 at 12:54, Steven D'Aprano wrote: >>> >>> Richard D. Moores wrote: >>> See for a speed test with n = 100,000 and 100,000 loops >>> >>> As a g

Re: [Tutor] List comprehension question

2010-11-11 Thread Richard D. Moores
On Wed, Nov 10, 2010 at 01:30, Steven D'Aprano wrote: > > Richard D. Moores wrote: > >> def proper_divisors_sum(n): >>    return sum(list(divisors(n))) - n > > There's no need to call list first. sum() will happily operate on any sort of > iterable -- lists, sums, iterators, generators, range obj

Re: [Tutor] List comprehension question

2010-11-10 Thread Steven D'Aprano
Richard D. Moores wrote: On Tue, Nov 9, 2010 at 12:54, Steven D'Aprano wrote: Richard D. Moores wrote: See for a speed test with n = 100,000 and 100,000 loops As a general rule, you shouldn't try to roll your own speed tests. There are various subtleti

Re: [Tutor] List comprehension question

2010-11-10 Thread Steven D'Aprano
Richard D. Moores wrote: def proper_divisors_sum(n): return sum(list(divisors(n))) - n There's no need to call list first. sum() will happily operate on any sort of iterable -- lists, sums, iterators, generators, range objects. Anything except strings, which would be pointless even if yo

Re: [Tutor] List comprehension question

2010-11-09 Thread Stefan Behnel
Richard D. Moores, 10.11.2010 08:24: def proper_divisors_sum(n): pd = set((1,)) for x in range(2, int(n**.5)+1): if n % x == 0: pd.update((x, n//x)) return sum(list(pd)) You keep using redundant operations. What "sum(list(s))" does, for s being a set, is:

Re: [Tutor] List comprehension question

2010-11-09 Thread Richard D. Moores
On Tue, Nov 9, 2010 at 18:29, C or L Smith wrote: > >From sympy you simply do: > from sympy import divisors list(divisors(256)) > [1, 2, 4, 8, 16, 32, 64, 128, 256] > > So your proper divisors would just be sum(divisors(n)) - n. > The divisors function there is in the ntheory/factor_.py

Re: [Tutor] List comprehension question

2010-11-09 Thread C or L Smith
>> 1. Re: List comprehension question (Richard D. Moores) >>> ?: def proper_divisors_sum(n): >>> A few questions--noting, of course, that I'm not reading this with >>> an eye toward performance, which it seems you are, but these occur >>> to me: Tim Peters had a beautiful little version of divi

Re: [Tutor] List comprehension question

2010-11-09 Thread Alan Gauld
"Richard D. Moores" wrote Question: When I dump in more that one line of any code (properly indented), after running it once, I've never known how to run it again without a redump. The up arrow just gets me one line at a time. So, how to do it? It depends on your IDE. On IDLE I think its

Re: [Tutor] List comprehension question

2010-11-09 Thread Richard D. Moores
On Tue, Nov 9, 2010 at 12:54, Steven D'Aprano wrote: > Richard D. Moores wrote: > >> See for a speed test with n = >> 100,000 and 100,000 loops > > As a general rule, you shouldn't try to roll your own speed tests. There are > various subtleties that can thr

Re: [Tutor] List comprehension question

2010-11-09 Thread Albert-Jan Roskam
, and public health, what have the Romans ever done for us? ~~ From: Steven D'Aprano To: tutor@python.org Sent: Tue, November 9, 2010 9:54:24 PM Subject: Re: [Tutor] List comprehension que

Re: [Tutor] List comprehension question

2010-11-09 Thread Steven D'Aprano
Richard D. Moores wrote: See for a speed test with n = 100,000 and 100,000 loops As a general rule, you shouldn't try to roll your own speed tests. There are various subtleties that can throw your results right out. Timing small code snippets on modern

Re: [Tutor] List comprehension question

2010-11-09 Thread Richard D. Moores
On Tue, Nov 9, 2010 at 05:51, Martin A. Brown wrote: > > Hello, > >  : def proper_divisors_sum(n): >  :     pd_list = [] >  :     for x in range(1, int(n**.5)+1): >  :         if n % x == 0: >  :             factor = int(x) >  :             pd_list.append(factor) >  :             factor2 = int(n/f

Re: [Tutor] List comprehension question

2010-11-09 Thread Martin A. Brown
Hello, : def proper_divisors_sum(n): : pd_list = [] : for x in range(1, int(n**.5)+1): : if n % x == 0: : factor = int(x) : pd_list.append(factor) : factor2 = int(n/factor) : pd_list.append(factor2) : pd_list = list(set(

Re: [Tutor] List comprehension question

2010-11-09 Thread Richard D. Moores
On Tue, Nov 9, 2010 at 03:35, Stefan Behnel wrote: > Richard D. Moores, 09.11.2010 12:07: > > That sqrt(n) works for speeding up the finding of primes, but here we > want to use int(n/2) (and why didn't I think of that?), which results > in about a 2x speedup. See

Re: [Tutor] List comprehension question

2010-11-09 Thread Stefan Behnel
Richard D. Moores, 09.11.2010 12:07: That sqrt(n) works for speeding up the finding of primes, but here we want to use int(n/2) (and why didn't I think of that?), which results in about a 2x speedup. See. NO! Use int(n/2)+1 . I'll correct that in

Re: [Tutor] List comprehension question

2010-11-09 Thread Richard D. Moores
On Mon, Nov 8, 2010 at 23:49, Richard D. Moores wrote: > On Mon, Nov 8, 2010 at 22:47, Richard D. Moores wrote: >> On Mon, Nov 8, 2010 at 21:31, Richard D. Moores wrote: >> >>> That sqrt(n) works for speeding up the finding of primes, but here we >>> want to use int(n/2) (and why didn't I think

Re: [Tutor] List comprehension question

2010-11-08 Thread Richard D. Moores
On Mon, Nov 8, 2010 at 22:47, Richard D. Moores wrote: > On Mon, Nov 8, 2010 at 21:31, Richard D. Moores wrote: > >> That sqrt(n) works for speeding up the finding of primes, but here we >> want to use int(n/2) (and why didn't I think of that?), which results >> in about a 2x speedup. See

Re: [Tutor] List comprehension question

2010-11-08 Thread Richard D. Moores
On Mon, Nov 8, 2010 at 21:31, Richard D. Moores wrote: > That sqrt(n) works for speeding up the finding of primes, but here we > want to use int(n/2) (and why didn't I think of that?), which results > in about a 2x speedup. See . NO! Use int(n/2)+1 . I'll

Re: [Tutor] List comprehension question

2010-11-08 Thread Stefan Behnel
Richard D. Moores, 09.11.2010 06:31: On Mon, Nov 8, 2010 at 03:43, Steven D'Aprano wrote: Richard D. Moores wrote: Coming back to your function: def proper_divisors(n): sum_ = 0 for x in range(1,n): if n % x == 0: sum_ += x return sum_ we can write that much

Re: [Tutor] List comprehension question

2010-11-08 Thread Richard D. Moores
On Mon, Nov 8, 2010 at 03:43, Steven D'Aprano wrote: > Richard D. Moores wrote: > Coming back to your function: > > def proper_divisors(n): >    sum_ = 0 >    for x in range(1,n): >        if n % x == 0: >            sum_ += x >    return sum_ > > > we can write that much more simply: > > def pro

Re: [Tutor] List comprehension question

2010-11-08 Thread Steven D'Aprano
Stefan Behnel wrote: Hugo Arts, 08.11.2010 00:53: [...] On another note, getting rid of the list comprehension and using a generator expression will be even faster, since you won't have to build the list. I gave this suggestion a try. It is true for me when run in CPython 3.2: [...] However

Re: [Tutor] List comprehension question

2010-11-08 Thread Steven D'Aprano
Alan Gauld wrote: "Steven D'Aprano" wrote I'm going to be pedantic here... but to quote the Middleman, specificity is the soul of all good communication. Be pedantic! :-) I really liked the explanation although I already sort of knew most of it. But there were a few nuggets in there I'd mis

Re: [Tutor] List comprehension question

2010-11-08 Thread Alan Gauld
"Stefan Behnel" wrote On another note, getting rid of the list comprehension and using a generator expression will be even faster, since you won't have to build the list. I gave this suggestion a try. It is true for me when run in CPython 3.2: However, it is no longer true when I run the

Re: [Tutor] List comprehension question

2010-11-08 Thread Alan Gauld
"Stefan Behnel" wrote Why use math.sqrt() instead of just using the ** operator? return sum(x for x in range(1, int(n**0.5)) if n%x == 0) Since this operation is only evaluated once in the whole runtime of the loop, I think readability beats the likely very tiny performance difference here

Re: [Tutor] List comprehension question

2010-11-08 Thread Stefan Behnel
Hugo Arts, 08.11.2010 00:53: On Mon, Nov 8, 2010 at 12:36 AM, Richard D. Moores wrote: def proper_divisors(n): """ Return the sum of the proper divisors of positive integer n """ return sum([x for x in range(1,n) if int(n/x) == n/x]) The list comprehension is this function i

Re: [Tutor] List comprehension question

2010-11-08 Thread Stefan Behnel
Alan Gauld, 08.11.2010 17:28: "Steven D'Aprano" wrote def proper_divisors(n): return sum(x for x in range(1, int(math.sqrt(n))) if n%x == 0) Why use math.sqrt() instead of just using the ** operator? return sum(x for x in range(1, int(n**0.5)) if n%x == 0) I'd have expected ** to be sign

Re: [Tutor] List comprehension question

2010-11-08 Thread Alan Gauld
"Steven D'Aprano" wrote I'm going to be pedantic here... but to quote the Middleman, specificity is the soul of all good communication. Be pedantic! :-) I really liked the explanation although I already sort of knew most of it. But there were a few nuggets in there I'd missed, like range()

Re: [Tutor] List comprehension question

2010-11-08 Thread Steven D'Aprano
Richard D. Moores wrote: So this version of my function uses a generator, range(), no? def proper_divisors(n): sum_ = 0 for x in range(1,n): if n % x == 0: sum_ += x return sum_ I'm going to be pedantic here... but to quote the Middleman, specificity is the so

Re: [Tutor] List comprehension question

2010-11-07 Thread Richard D. Moores
On Sun, Nov 7, 2010 at 17:47, Wayne Werner wrote: > On Sun, Nov 7, 2010 at 7:15 PM, Richard D. Moores > wrote: >> >> On Sun, Nov 7, 2010 at 16:41, Wayne Werner wrote: >> > On Sun, Nov 7, 2010 at 6:31 PM, Hugo Arts wrote: >> >> I should have mentioned that I'm using 3.1 . >> >> So this version

Re: [Tutor] List comprehension question

2010-11-07 Thread Wayne Werner
On Sun, Nov 7, 2010 at 7:15 PM, Richard D. Moores wrote: > On Sun, Nov 7, 2010 at 16:41, Wayne Werner wrote: > > On Sun, Nov 7, 2010 at 6:31 PM, Hugo Arts wrote: > > I should have mentioned that I'm using 3.1 . > > So this version of my function uses a generator, range(), no? > Correct. The ru

Re: [Tutor] List comprehension question

2010-11-07 Thread Richard D. Moores
On Sun, Nov 7, 2010 at 16:41, Wayne Werner wrote: > On Sun, Nov 7, 2010 at 6:31 PM, Hugo Arts wrote: >> >> >> here's a list comprehension >> >>> a = [x*2 for x in range(10)] >> >>> a >> [0, 2, 4, 6, 8, 10, 12, 14, 16, 18] >> >> here's the equivalent generator expression: >> >>> a = (x*2 for x in

Re: [Tutor] List comprehension question

2010-11-07 Thread Alan Gauld
"Hugo Arts" wrote Yes. A cast or typecast means converting some data to a different type, like converting floats to integers, strings to integers, The term cast can be misleading however since in some languages - those decended from C it means treating a piece of data as if it were another t

Re: [Tutor] List comprehension question

2010-11-07 Thread Wayne Werner
On Sun, Nov 7, 2010 at 6:31 PM, Hugo Arts wrote: > > here's a list comprehension > >>> a = [x*2 for x in range(10)] > >>> a > [0, 2, 4, 6, 8, 10, 12, 14, 16, 18] > > here's the equivalent generator expression: > >>> a = (x*2 for x in range(10)) Since you're talking about generators and effi

Re: [Tutor] List comprehension question

2010-11-07 Thread Hugo Arts
On Mon, Nov 8, 2010 at 1:16 AM, Richard D. Moores wrote: > On Sun, Nov 7, 2010 at 15:53, Hugo Arts wrote: >> n is a proper >> divisor of x if there is no remainder after division, after all. This >> also means you won't have to do a cast, which tend to be fairly >> expensive. > > I don't know wha

Re: [Tutor] List comprehension question

2010-11-07 Thread Richard D. Moores
On Sun, Nov 7, 2010 at 15:53, Hugo Arts wrote: > On Mon, Nov 8, 2010 at 12:36 AM, Richard D. Moores wrote: >> def proper_divisors(n): >>     """ >>     Return the sum of the proper divisors of positive integer n >>     """ >>     return sum([x for x in range(1,n) if int(n/x) == n/x]) >> >> The li

Re: [Tutor] List comprehension question

2010-11-07 Thread Hugo Arts
On Mon, Nov 8, 2010 at 12:36 AM, Richard D. Moores wrote: > def proper_divisors(n): >     """ >     Return the sum of the proper divisors of positive integer n >     """ >     return sum([x for x in range(1,n) if int(n/x) == n/x]) > > The list comprehension is this function is inefficient in that

[Tutor] List comprehension question

2010-11-07 Thread Richard D. Moores
def proper_divisors(n): """ Return the sum of the proper divisors of positive integer n """ return sum([x for x in range(1,n) if int(n/x) == n/x]) The list comprehension is this function is inefficient in that it computes n/x twice. I'd like to do an a = n/x and use a in "if int(a

Re: [Tutor] List Comprehension question

2010-02-08 Thread Luke Paireepinart
On Mon, Feb 8, 2010 at 4:15 PM, wrote: > I've been trying to work my way through some 'beginner projects' I found > around the web, one of them involves generating some random numbers. I > decided to use a list of lists, and I'm wondering if this is a valid > comprehension...IDLE doesn't seem to

[Tutor] List Comprehension question

2010-02-08 Thread internets
I've been trying to work my way through some 'beginner projects' I found around the web, one of them involves generating some random numbers. I decided to use a list of lists, and I'm wondering if this is a valid comprehension...IDLE doesn't seem to mind, but maybe I lack the experience to know be