Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Toooo slow....
#1
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
Larz60+ write Nov-25-2020, 06:15 PM:
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.

Fixed for you this time. Please use bbcode on future posts.
Reply
#2
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.
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#3
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
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020