On 6/17/2009 5:11 PM Robert Berman said...
Emille,
Thank you for the example of list splicing. Do you know if this is
faster than a more conventional loop statement
Faster can be exactly determined using timeit. (for some definition of
exact -- the one we use mostly around here anyway)
So, for the bits you want to know faster about, you'd do something like:
>>> from timeit import Timer
>>> t = Timer("aa = range(0,100,2)")
>>> t.timeit()
1.6688069739620928
>>> t = Timer("aa = range(100)[::2]")
>>> t.timeit()
3.3360306112492282
>>>
I'm not sure this applies specifically to what you're doing, and
crafting what you are timing is important, but read up on timeit and try
it out.
as in my code for
primearray which is in my original post (reprinted here)
Well, there's the confusion. This is the code for something called
BuildSieve. In your prior post you show:
-----
Printing a range, say primearray[21]
through primearray[50] will give you all prime numbers between 21 and
50.
23
29
31
37
41
43
47
-----
... but this isn't what BuildSieve yields:
>>> BuildSieve(20)
[0, 0, 2, 3, 0, 5, 0, 7, 0, 0, 0, 11, 0, 13, 0, 0, 0, 17, 0, 19, 0]
So I still don't know what primearray is/does.
Emile
The code is as follows:
def BuildSieve(itemsin):
TheSieve=list()
TheSieve = range(0,itemsin+1)
TheSieve[1]=0
for i in range(2,itemsin+1):
if (TheSieve[i] > 0):
j = i + i
while (j <= itemsin):
TheSieve[j] = 0
j+=i
return TheSieve
It is called with PrimaryList = BuildSieve(1000000)
Again, thank you for your help.
Robert
On Wed, 2009-06-17 at 17:01 -0700, Emile van Sebille wrote:
On 6/17/2009 4:48 PM Robert Berman said...
> Emile,
>
> Thank your for your comments. I do have a list running from 0-1000001.
> Yes, it is true, I only needed 0 - 100000 and yes I will change it.
> However, if you use primearray
you haven't posted the primearray code...
<snip>
>
> However, for the time being, can you perhaps share some suggestions on
> list splicing?
So, this part of your original post--
> Out[53]: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
>
> Since 3 is a prime, we can eliminate all multiples of 3. Within l1,
> these are expressed as
>
> In [52]: l1[n+n:len(l1):n]
> Out[52]: [6, 9, 12]
>
> when n = 3. ( do know 12 would have been eliminated by the prime
> number 2)
>
> It would be great if I could say l1[n+n:len(l1):n] = 0
but you can say:
for ii in l1[n+n:len(l1):n]: l1[ii] = 0
Is something like that what you're after?
Emile
> but obviously
>
> that will fail for obvious reasons. I am looking for the right hand
> side of the statement to set a list within the list to all zeros.
---
_______________________________________________
Tutor maillist - Tutor@python.org <mailto:Tutor@python.org>
http://mail.python.org/mailman/listinfo/tutor
------------------------------------------------------------------------
_______________________________________________
Tutor maillist - Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor
_______________________________________________
Tutor maillist - Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor