[Tutor] List Splicing

Robert Berman bermanrl at cfl.rr.com
Thu Jun 18 02:11:18 CEST 2009


Emille,

Thank you for the example of list splicing.  Do you know if this is
faster than a more conventional loop statement as in my code for
primearray which is in my original post (reprinted here)

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 at python.org
> http://mail.python.org/mailman/listinfo/tutor
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20090617/7650f23e/attachment.htm>


More information about the Tutor mailing list