Python Forum

Full Version: Toooo slow....
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi,
My code is meant to go through list 't' and check if any object from it is smaller than the first object in list 'a', if there is one, than it changes the value to '100001' which is just a sign to me that its already used, and than it goes to the next object in list 'a' and does that until it goes through the whole list 'a'. Also the values from list 't' can only be used once each. Does anyone have an idea or a clue on how to optimize or speed up this code?
for k in range(m):
    u=0
    for u in range(n): 
        if t[u]<a[k]: 
            t[u]=100001 
            a[k]=100001 
            l+=1 
            break
Start with clarity in mind. Your problem description:

  • go through list 't' and check if any object from it is smaller than the first object in list 'a'
    • if there is one, than it changes the value to '100001'
  • it goes to the next object in list 'a' and does that until it goes through the whole list 'a'. [just iterates over list 'a'?]
  • the values from list 't' can only be used once each [what means 'used'?]

I may only assume what you want to accomplish.... like finding minimum value in list 'a' and then checking which items in list 't' are smaller of it.
This is a test to see if you think things through before you start coding. min(a) removes the outer loop and will speed things greatly