Faster way to do this...

Discussion in 'Python' started by Harlin Seritt, Mar 1, 2005.

  1. I've got the following code:

    nums = range(0)
    for a in range(100):
    nums.append(a)

    Is there a better way to have num initialized to a list of 100
    consecutive int values?

    Thanks,

    Harlin
     
    Harlin Seritt, Mar 1, 2005
    #1
    1. Advertising

  2. Harlin Seritt

    Will McGugan Guest

    Harlin Seritt wrote:
    > I've got the following code:
    >
    > nums = range(0)
    > for a in range(100):
    > nums.append(a)
    >
    > Is there a better way to have num initialized to a list of 100
    > consecutive int values?
    >


    Isn't that equivalent to simply..

    nums= range(100)

    Will McGugan
     
    Will McGugan, Mar 1, 2005
    #2
    1. Advertising

  3. Harlin Seritt

    Steve Holden Guest

    Harlin Seritt wrote:
    > I've got the following code:
    >
    > nums = range(0)
    > for a in range(100):
    > nums.append(a)
    >
    > Is there a better way to have num initialized to a list of 100
    > consecutive int values?
    >

    Why not the simplest solution?

    a = range(100)

    regards
    Steve
     
    Steve Holden, Mar 1, 2005
    #3
  4. Harlin Seritt wrote:
    > I've got the following code:
    >
    > nums = range(0)
    > for a in range(100):
    > nums.append(a)
    >
    > Is there a better way to have num initialized to a list of 100
    > consecutive int values?


    You mean like this?

    nums = range(100)

    ;-)

    --
    --------------------------------------------------------------------
    Aaron Bingham
    Software Engineer
    Cenix BioScience GmbH
    --------------------------------------------------------------------
     
    Aaron Bingham, Mar 1, 2005
    #4
  5. Harlin Seritt

    Roy Smith Guest

    Harlin Seritt <> wrote:
    >I've got the following code:
    >
    >nums = range(0)
    >for a in range(100):
    > nums.append(a)
    >
    >Is there a better way to have num initialized to a list of 100
    >consecutive int values?


    Step one would be to change the first line to

    nums = []

    which is simpler and results in the same thing. Or, you could write
    the whole thing as a one-liner using a list comprehension

    nums = [a for a in range(100)]

    and then you can take it one step further and just write

    nums = range(100)

    which I think is about as simple as you can get.
     
    Roy Smith, Mar 1, 2005
    #5
  6. Will McGugan wrote:

    > Isn't that equivalent to simply..
    >
    > nums= range(100)


    I remember the day I first realized that 900 lines of some C++ program I
    was working on could be expressed in three lines of python. Ahh.
    Rebirth. Then there was the phase of the python-newbie so enamored of
    map and lambda. ... Wait, actually, I'm not out of that yet. :)

    Warren
     
    Warren Postma, Mar 1, 2005
    #6
  7. Excellent point Warren. I have been working with Python for about 3
    years in all, but only really seriously for about a year. I am still
    utterly amazed that near everything that takes me about 5 to 20 lines
    of code can be done in 1, 2 or 3 lines of Python code (when done
    correctly). It is very frustrating that I am still using Python as
    though I would if I were writing Java or C++ code. One day I'll get the
    hang of this.

    Roy, I like what you showed: nums = [a for a in range(100)] . My
    mistake for not expressing my question as well as I should have. Not
    only am I looking for a way to fill in 100 spots (more or less) in an
    array errrrr... list, but I'd like to be able to do it in intervals of
    2, 4, 8 etc. as well as other things.

    Thanks,

    Harlin
     
    Harlin Seritt, Mar 2, 2005
    #7
  8. Harlin Seritt

    Robert Kern Guest

    Harlin Seritt wrote:

    > Roy, I like what you showed: nums = [a for a in range(100)] . My
    > mistake for not expressing my question as well as I should have. Not
    > only am I looking for a way to fill in 100 spots (more or less) in an
    > array errrrr... list, but I'd like to be able to do it in intervals of
    > 2, 4, 8 etc. as well as other things.


    Like

    nums = range(0, 100, 4)

    ?

    --
    Robert Kern


    "In the fields of hell where the grass grows high
    Are the graves of dreams allowed to die."
    -- Richard Harter
     
    Robert Kern, Mar 2, 2005
    #8
  9. Harlin Seritt

    Will McGugan Guest

    Warren Postma wrote:
    > Will McGugan wrote:
    >
    >> Isn't that equivalent to simply..
    >>
    >> nums= range(100)

    >
    >
    > I remember the day I first realized that 900 lines of some C++ program I
    > was working on could be expressed in three lines of python. Ahh.


    Lately I've found myself commenting C++ code with the equivalent Python
    code. I find it clearer and more terse than simply commenting in English!


    Will
     
    Will McGugan, Mar 2, 2005
    #9
  10. Harlin Seritt

    Robert Kern Guest

    Will McGugan wrote:
    > Warren Postma wrote:
    >
    >> Will McGugan wrote:
    >>
    >>> Isn't that equivalent to simply..
    >>>
    >>> nums= range(100)

    >>
    >>
    >>
    >> I remember the day I first realized that 900 lines of some C++ program
    >> I was working on could be expressed in three lines of python. Ahh.

    >
    >
    > Lately I've found myself commenting C++ code with the equivalent Python
    > code. I find it clearer and more terse than simply commenting in English!


    If you used literate programming tools, you might be able to get a
    Python version and a C++ version of your code in one go!

    --
    Robert Kern


    "In the fields of hell where the grass grows high
    Are the graves of dreams allowed to die."
    -- Richard Harter
     
    Robert Kern, Mar 2, 2005
    #10
  11. Harlin Seritt wrote:
    > Roy, I like what you showed: nums = [a for a in range(100)] . My
    > mistake for not expressing my question as well as I should have. Not
    > only am I looking for a way to fill in 100 spots (more or less) in an
    > array errrrr... list, but I'd like to be able to do it in intervals of
    > 2, 4, 8 etc. as well as other things.


    range(2, 100, 4)

    How about you fire up the interactive python and try
    help(range)

    ;)

    --
    Timo Virkkala
     
    Timo Virkkala, Mar 3, 2005
    #11
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Terry Olsen

    Any way to make pages faster?

    Terry Olsen, Aug 1, 2005, in forum: ASP .Net
    Replies:
    6
    Views:
    389
    clintonG
    Aug 2, 2005
  2. Matt Stephens

    XSLT is way faster using Java 5

    Matt Stephens, Feb 6, 2005, in forum: Java
    Replies:
    0
    Views:
    415
    Matt Stephens
    Feb 6, 2005
  3. steve
    Replies:
    17
    Views:
    718
    Mike Smith
    Sep 13, 2004
  4. Jon Hyland
    Replies:
    4
    Views:
    6,123
    Jon Hyland
    Oct 5, 2004
  5. Eamonn Sullivan

    A faster way of finding historical highs/lows

    Eamonn Sullivan, Jun 11, 2004, in forum: Python
    Replies:
    6
    Views:
    368
    Peter Hansen
    Jun 14, 2004
Loading...

Share This Page