Python Forum
help! this 'for' loop makes me crazy
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
help! this 'for' loop makes me crazy
#1
I am beginner. Now I have a question, in the following codes I want to get a sorted list such as a = [6,8,10,20,50]
a=[10,20,8,50,6]
for i in range(5):

    if a[i]<a[i-1]:

    a[i],a[i-1]=a[i-1],a[i]

    i-=1

  else:
    pass
print(a)
Because I am learning sorting algorithms, so I don't want to use built in function 'sort'.
My question is:
when i=2,a[i]=8,so 'if'statement will be run,and afer 'i-=1', now i=1,the 'if'condition is fulfilled again,why a[i]=8 and a[i-1]=10 willnot be compared,and the 'for'loop seems go to next step? who can give me a explain, I will appreciate it very much.
Reply
#2
What kind of sorting algorithm you try to implement? Can you spell it out in spoken language?

This code is not indented correctly but anyway, for-loop will emit values 0, 1, 2, 3, 4. At first loop cycle you compare a[0] < a[0-1] - first element with last element. Is this your intention? Also - what is the purpose of i -= 1?
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
Compare the first element in list with the rest of the element and append the minimum value in the element of the list and then remove the existing minimum element in list

data_list = [10,20,8,50,6]
new_list = []

while data_list:
    minimum = data_list[0]  # arbitrary number in list 
    for x in data_list: 
        if x < minimum:
            minimum = x
    new_list.append(minimum)
    data_list.remove(minimum)    

print (new_list)
Reply
#4
Learning sorting algorithms is usually first and very important step in learning algorithms in general. One should take time and effort needed to wholly master different sort algorithms (bubble sort, short bubble, quicksort, heap sort to name a few).
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
#5
Thanks a lot for the answers. You all are very kind and helpful.
I amnot native speaker , so perhaps I havent expressed my question very exactly.  During writing and testing the mentioned codes, I find out the 'for' loop works very differently as I thought.
so I focus on the 'for' loop in this concrete case: how does it work and whats the reason behind it.
Especially in the third step: i=2, so a[i]=8, a[i-1]=20, then they are swapped, after that 'i-=1', so now we get i=1, but why a[1]=8 and a[0]=10 willnot be compared any more, what happened in the fourth step, the for loop goes on or not?
I am very confused.
the essential questions: what happens after 'i-=1'? what has the 'for'-loop done after this step?
Reply
#6
Simple answer is: you don't mutate list while iterating over it.

"If fact, in any programming language for most part if you mutate something while you iterating over it you living in state of sin and you deserve whatever happens to you" -- Raymond Hettinger, Python core-developer, Transforming Code into Beautiful, Idiomatic Python

However, in-place changing of list which happens in bubble sort is just this and one must be extra careful.

You can easily follow i value by printing it out:

>>> for i in range(5): 
...      print(f'before {i}') 
...      i -= 1 
...      print(f'after {i}')
before 0
after -1
before 1
after 0
before 2
after 1
before 3
after 2
before 4
after 3
As you can see for-loop emitting 0, 1, 2, 3, 4 (before values)
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


Possibly Related Threads…
Thread Author Replies Views Last Post
  What a difference print() makes Mark17 2 517 Oct-20-2023, 10:24 PM
Last Post: DeaD_EyE
  Tkinter + Multiprocessing startmap makes UI Freeze sunny9495 4 1,323 Nov-13-2022, 12:49 PM
Last Post: sunny9495
  Upgrade makes Code Error kucingkembar 6 2,971 Jul-28-2022, 06:44 PM
Last Post: kucingkembar
  The number of object makes code slower? fig0 1 2,467 Jan-25-2018, 11:16 PM
Last Post: Gribouillis

Forum Jump:

User Panel Messages

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