473,657 Members | 2,389 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

Jul 18 '05 #1
10 1246
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
Jul 18 '05 #2
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
Jul 18 '05 #3
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
--------------------------------------------------------------------

Jul 18 '05 #4
Harlin Seritt <ha**********@y ahoo.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.
Jul 18 '05 #5
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
Jul 18 '05 #6
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

Jul 18 '05 #7
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
rk***@ucsd.edu

"In the fields of hell where the grass grows high
Are the graves of dreams allowed to die."
-- Richard Harter
Jul 18 '05 #8
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
Jul 18 '05 #9
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
rk***@ucsd.edu

"In the fields of hell where the grass grows high
Are the graves of dreams allowed to die."
-- Richard Harter
Jul 18 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

36
2622
by: Armin Rigo | last post by:
Hi! This is a rant against the optimization trend of the Python interpreter. Sorting a list of 100000 integers in random order takes: * 0.75 seconds in Python 2.1 * 0.51 seconds in Python 2.2 * 0.46 seconds in Python 2.3
23
4742
by: YinTat | last post by:
Hi, I learned C++ recently and I made a string class. A code example is this: class CString { public: inline CString(const char *rhs) { m_size = strlen(rhs);
98
14400
by: jrefactors | last post by:
I heard people saying prefix increment is faster than postfix incerement, but I don't know what's the difference. They both are i = i+1. i++ ++i Please advise. thanks!!
3
2055
by: mathilda | last post by:
When I'm opening a recordset for add new, is it faster to put a limiting clause on it? i.e. does "Select from Where Keyfield = " & value & " return a recordset faster than "Select from "
18
1654
by: junky_fellow | last post by:
which of the following is faster and why ? if ( x == 1 ) { } or if ( x != 0 ) {
1
2721
by: James dean | last post by:
I done a test and i really do not know the reason why a jagged array who has the same number of elements as a multidimensional array is faster here is my test. I assign a value and do a small calculation. Even if i initialise the jagged array inside the function it is still much faster. Are these results correct?. If i put the initialisation loop in the constructor its ridiculously faster but even here its 4 times faster...is this correct?...
1
1848
by: anagai | last post by:
Im wondering if generating html objects such as tabels and rows in javascript is faster than typing the html directly? Seems when you do it in javascript you have to download alot of code and would slow down displaying the page. while if you just type the html, it requires less bandwidth and display faster? is parsing html to display in browser slower than doing it through dom to display the same html objects on the page?
11
2982
by: ctman770 | last post by:
Hi Everyone, Is it faster to save the precise location of an html dom node into a variable in js, or to use getElementById everytime you need to access the node? I want to make my application as fast as possible. I have about 10-20 id tags that need to be accessed and modified from time to time. Would the jvm perform slowly if I stored all of the dom node strings "document.node.child...." into a huge js array?
23
2511
by: Python Maniac | last post by:
I am new to Python however I would like some feedback from those who know more about Python than I do at this time. def scrambleLine(line): s = '' for c in line: s += chr(ord(c) | 0x80) return s def descrambleLine(line):
41
2671
by: c | last post by:
Hi every one, Me and my Cousin were talking about C and C#, I love C and he loves C#..and were talking C is ...blah blah...C# is Blah Blah ...etc and then we decided to write a program that will calculate the factorial of 10, 10 millions time and print the reusult in a file with the name log.txt.. I wrote something like this
0
8413
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8617
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7352
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6176
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5642
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4173
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4330
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2742
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1970
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.