A suggestion for a possible Python module

Steven Taschuk staschuk at telusplanet.net
Wed Mar 5 14:02:16 EST 2003


Quoth Jp Calderone:
  [...]
>   For the first part,
> 
>     s[i:j:k] = whatever
> 
>   is the same as
> 
>     s.__setitem__(slice(i, j, k), whatever)
> 
>   so it's legal, syntatically, but only works if the invoked __setitem__
> implementation actually implements it sensibly.
> 
>   I can't think of any real use cases for it either, though.

Erastothenes' sieve comes to mind.

    numbers = range(2, n)
    zeroes = [0]*len(numbers)
    primes = []
    for i in range(len(numbers)):
        if numbers[i]:
            prime = numbers[i]
            primes.append(prime)
            numbers[i::prime] = zeroes[i::prime]
    # primes is now a list of primes in [2, n)

-- 
Steven Taschuk                Aral: "Confusion to the enemy, boy."
staschuk at telusplanet.net      Mark: "Turn-about is fair play, sir."
                              (_Mirror Dance_, Lois McMaster Bujold)





More information about the Python-list mailing list