473,406 Members | 2,336 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

newbie loop efficiency

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


Expand|Select|Wrap|Line Numbers
  1. L2 = []
  2. L3 = []
  3.  
  4. i = 0
  5. while i < len(L0):
  6.     if L1[i] == X:
  7.         L2.append(Y)
  8.         L3.append(Z)
  9.     else:
  10.         L2.append(0)
  11.         L3.append(0)
  12.     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


Expand|Select|Wrap|Line Numbers
  1. L2 = []
  2.  
  3. i = 0
  4. while i < len(L0):
  5.     j = 0
  6.     while j < len(L1):
  7.         if L0[i]  == L1[k]:
  8.             L2.append(X)
  9.             break
  10.         else:
  11.             L2.append(0)
  12.        j = j + 1
  13.     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!
Feb 15 '08 #1
4 1182
dshimer
136 Expert 100+
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.
Expand|Select|Wrap|Line Numbers
  1. >>> L0=(1,2,3,4,5)
  2. >>> for value in L0:
  3. ...     print value
  4. ...     
  5. 1
  6. 2
  7. 3
  8. 4
  9. 5
  10. >>> L0=(1,2,3)
  11. >>> L1=('a','b','c')
  12. >>> for number in L0:
  13. ...     for letter in L1:
  14. ...         print number,letter
  15. ...         
  16. 1 a
  17. 1 b
  18. 1 c
  19. 2 a
  20. 2 b
  21. 2 c
  22. 3 a
  23. 3 b
  24. 3 c
If you want a simple counter copy the following into your interpreter
Expand|Select|Wrap|Line Numbers
  1.  for i in range(5):print i
and see how that works.
Feb 15 '08 #2
thanks for your reply .. i am looking forward to seeing some ubercode :)

dshimer .. btw .. is

Expand|Select|Wrap|Line Numbers
  1. for i in range(5):print i
faster than


Expand|Select|Wrap|Line Numbers
  1.  
  2. i = 0
  3. while i < 5:
  4.     print i
  5.     i = i + 1
  6.  
Feb 15 '08 #3
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.
Feb 15 '08 #4
dshimer
136 Expert 100+
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.
Feb 16 '08 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

22
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...
19
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;
5
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...
26
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)...
8
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...
2
ADezii
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...
12
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...
18
by: sam | last post by:
(newbie)Technically what's the difference between memset() and memcpy() functions?
12
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 =...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
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
0
BarryA
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...
0
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...
0
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,...
0
Oralloy
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,...
0
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...
0
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...
0
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,...

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.