Re: Tuple question

From: Alex Martelli (aleaxit_at_yahoo.com)
Date: 09/04/04


Date: Sat, 4 Sep 2004 11:00:30 +0200

Peter Hansen <peter@engcorp.com> wrote:
   ...
> Consider, for example, that one actually has to build the
> tuple in the first place... how can you do that without
> having the info in a list to begin with? (I'm sure there
> are ways if one is ingenious, but I think the answers
> would just go to prove the point I was making.)

tuple(somegenerator(blah)) will work excellently well. In 2.4, you can
even often code that 'somegenerator' inline as a generator
comprehension. So this 'having the info in a list' argument sounds just
totally bogus to me.

Say I want to work with some primes and I have a primes generator.
Primes aren't going to change, so a tuple is a natural. I start with,
e.g.,

ps = tuple(itertools.islice(primes(), 999999))

...and then I'm stumped because I can't index into ps to find, say, the
progressive number of some given prime N by ps.index(N). How silly,
having to keep ps a list, i.e. mutable (when it intrinsically isn't)
just to be able to index into it! [I can usefully exploit ps's
sortedness via module bisect... ignoring the latter's specs and docs
that keep screamign LISTS, bisect.bisect DOES work on tuples... but I
wouldn't feel comfy about that surviving, given said docs and
specs...:-)

Alex



Relevant Pages

  • Re: Towards a Formula for Primes
    ... An Axiomatic Theorem on the Decidability of Primes. ... I was thinking in a Hilbert way. ... it would have given zero and 123 for the two lists. ...
    (sci.math)
  • Re: Length of sequence of consecutive primes starting at 2
    ... >I would like to know how long the list of consecutive primes (i.e. not ... >skipping any primes in between) is and can I find it somewhere. ... lists of all primes up to at least 10^12 ... into permanent or even semi-permanent storage. ...
    (sci.math)
  • Re: Chez Watt: Prime Idiocy
    ... figured criticism for slop was better than criticism for ommission. ... There is a disclaimer that the lists are only ... "primes" if you haven't got a clue what you are talking about. ...
    (talk.origins)
  • Re: Prime lists and Computation
    ... > Q2 Is their a complete list of primes to X Published? ... sufficient storage is available to store them all. ... prime lists for trial division. ... of digits by 3 would double the factoring time. ...
    (sci.math)
  • Question about generators
    ... def factor: ... The primes generator I wrote finds all primes up to n, except for 2, 3 and 5, so I must check those explicitly. ... Is there anyway to concatenate the hard coded list of and the generator I wrote so that I don't need two for loops that do the same thing? ...
    (comp.lang.python)