Hi all,
I need some help understanding the efficiency of my code. I am new to python(and coding in general) :(
if i have a code like this - L2 = []
-
L3 = []
-
-
i = 0
-
while i < len(L0):
-
if L1[i] == X:
-
L2.append(Y)
-
L3.append(Z)
-
else:
-
L2.append(0)
-
L3.append(0)
-
i = i + 1
Is there a more efficient way to code this. (my code goes through a list and if something matches it appends values to other lists)
Also - L2 = []
-
-
i = 0
-
while i < len(L0):
-
j = 0
-
while j < len(L1):
-
if L0[i] == L1[k]:
-
L2.append(X)
-
break
-
else:
-
L2.append(0)
-
j = j + 1
-
i = i + 1
the second code compares 2 lists and creates a 3rd list based on the results.
I am guessing this code is especially poor.
your help is greatly appreciated.
thanks for your time!
4 1182
You will find from some of the sharpest guys here that you could probably do that in 1 line and about 10% of the characters. Let me just start with a more efficient way of looping over a list. This code just deals with the actual for loop mechanism, but notice there is no counter or conditional test. In plain english, for every value in the list do something, you don't necessarily have to reference the value inside the loop. - >>> L0=(1,2,3,4,5)
-
>>> for value in L0:
-
... print value
-
...
-
1
-
2
-
3
-
4
-
5
-
>>> L0=(1,2,3)
-
>>> L1=('a','b','c')
-
>>> for number in L0:
-
... for letter in L1:
-
... print number,letter
-
...
-
1 a
-
1 b
-
1 c
-
2 a
-
2 b
-
2 c
-
3 a
-
3 b
-
3 c
If you want a simple counter copy the following into your interpreter - for i in range(5):print i
and see how that works.
thanks for your reply .. i am looking forward to seeing some ubercode :)
dshimer .. btw .. is - for i in range(5):print i
faster than -
-
i = 0
-
while i < 5:
-
print i
-
i = i + 1
-
If you are worried about performance you should take a look at the module "timeit".
Between the 2 previous codes (for & while), it's faster the while code.... but just a little, and makes the code ugliest.
BTW, never bench functions with "print" statements, because it's too slow and make no sense.
As far as cleaning up some of the other code. I would be interested in seeing where these values are coming from, how they are being stored, and what the goal is. It looks like you are reading through a sequence of coordinates and making decisions based on their relationship to other values. There may be code outside this loop which would influence the best way to accomplish the task.
Sign in to post your reply or Sign up for a free account.
Similar topics
by: silversurfer2025 |
last post by:
Hello everybdy,
I am a little confused for the following reason:
In my code I used a simple for-loop in order to initialize a 2D-array
of floats to zero. Because of efficiency reasons, I changed...
|
by: vamshi |
last post by:
Hi all,
This is a question about the efficiency of the code.
a :-
int i;
for( i = 0; i < 20; i++ )
printf("%d",i);
b:-
int i = 10;
|
by: Jim H |
last post by:
When dealing with reference types, is there an efficiency advantage in using
a for loop instead of a foreach? I though I read somewhere that when using
value types a for loop was more efficient...
|
by: a.mil |
last post by:
I am programming for code-speed, not for ansi or other nice-guy stuff
and I encountered the following problem:
When I have a for loop like this:
b=b0;
for (a=0,i=0;i<100;i++,b--) {
if (b%i)...
|
by: malkarouri |
last post by:
Hi everyone,
I have an algorithm in which I need to use a loop over a queue on
which I push values within the loop, sort of:
while not(q.empty()):
x = q.get()
#process x to get zero or more...
|
by: ADezii |
last post by:
If you are executing a code segment for a fixed number of iterations, always use a For...Next Loop instead of a Do...Loop, since it is significantly faster. Each pass through a Do...Loop that...
|
by: Philipp.Weissenbacher |
last post by:
Hi all!
This is most certainly a total newbie question, but why doesn't the
following code cause a segfault?
void insertion_sort(int a, int length)
{
int i;
for (i=0; i < length; i++)
{
int...
|
by: sam |
last post by:
(newbie)Technically what's the difference between memset() and
memcpy() functions?
|
by: Atropo |
last post by:
Hi all.
Having several strings how do i combine them to construct a command;
lets say to run the date command.
string str = "14/10/08 19:06:09";
strDD = str.substr(0,2);
strMM =...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
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,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
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...
|
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,...
| |