Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Python (http://www.velocityreviews.com/forums/f43-python.html)
-   -   Faster way to do this... (http://www.velocityreviews.com/forums/t342103-faster-way-to-do-this.html)

 Harlin Seritt 03-01-2005 12:21 PM

Faster way to do this...

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

 Will McGugan 03-01-2005 12:26 PM

Re: Faster way to do this...

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

 Steve Holden 03-01-2005 12:29 PM

Re: Faster way to do this...

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

 Aaron Bingham 03-01-2005 01:14 PM

Re: Faster way to do this...

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
--------------------------------------------------------------------

 Roy Smith 03-01-2005 03:43 PM

Re: Faster way to do this...

Harlin Seritt <harlinseritt@yahoo.com> 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.

 Warren Postma 03-01-2005 03:49 PM

Re: Faster way to do this...

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

 Harlin Seritt 03-02-2005 01:51 AM

Re: Faster way to do this...

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

 Robert Kern 03-02-2005 02:00 AM

Re: Faster way to do this...

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
rkern@ucsd.edu

"In the fields of hell where the grass grows high
Are the graves of dreams allowed to die."
-- Richard Harter

 Will McGugan 03-02-2005 12:02 PM

Re: Faster way to do this...

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

 Robert Kern 03-02-2005 08:27 PM

Re: Faster way to do this...

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
rkern@ucsd.edu

"In the fields of hell where the grass grows high
Are the graves of dreams allowed to die."
-- Richard Harter

All times are GMT. The time now is 05:46 PM.