0

I want to get a ordered list with ascending order, but leave out the first element in the original list. But the first for-loop only runs 4 times. After 4 times loop in first for-loop:

lst=[6, 5, 4]

I think the first for-loop should keep running, but it finishes. I don't know why?

Source code below:

#coding: utf-8
lst = [0, 1, 6, 2, 5, 3, 4]
new_lst = []  # the ordered list, [0, 1, 2, 3, 4, 5, 6]
p0 = lst[0]  # the first element can be arbitrary
lst.remove(p0)
new_lst.append(p0)
for temp in lst:
    lst.remove(temp)
    while 1:
        for pt in lst:
            if temp < pt:
                continue
            else:
                lst.append(temp)
                temp = pt
                lst.remove(pt)
                break
        else:
            new_lst.append(temp)
            break

print("the ordered list:", new_lst)

At last, I only get:

('the ordered list:', [0, 1, 2, 3])
16
  • 1
    Welcome to StackOverflow. Please read and follow the posting guidelines in the help documentation. Minimal, complete, verifiable example applies here. We cannot effectively help you until you post your MCVE code and accurately describe the problem. We should be able to paste your posted code into a text file and reproduce the problem you described. "cannot be accomplished correctly" is not a problem specification. Commented Jan 24, 2018 at 1:28
  • 1
    What is your desired result, [1, 2, 3, 4, 5, 6]? Commented Jan 24, 2018 at 1:30
  • @JohnKugelman, Judging by his code, I'd assume so Commented Jan 24, 2018 at 1:30
  • Can you just use sorted? A lot simpler that way (new_lst = sorted(lst[1:]) could replace it all). Either way, you can't safely mutate a list while iterating it. Commented Jan 24, 2018 at 1:31
  • 1
    @fanronghong: As I stated before, the "why" is that it's not safe to mutate a list while iterating it; in practice on CPython, it doesn't error out, but if you remove the item you're currently on, it ends up skipping the following item in the iteration. Click the link in this comment, and view the other linked questions from there; several of them are of the form "I call list.remove in a loop that iterates over the list and it breaks, why?" Commented Jan 24, 2018 at 2:01

3 Answers 3

1

To ignore first element in sorting:

newlst = [lst[0]]+sorted(lst[1:])

First element lst[0] has to be enclosed in [ ] to get a list which can be extended using + with rest of the list (lst[1:]).

Sign up to request clarification or add additional context in comments.

2 Comments

Yeah the User isn't very clear as to what he wants. possible flag?
I think this is exactly what user wanted.
0

try new_lst = sorted(lst). This sorts a list by integer value (and ascii value if characters are present). This is a much nicer solution for your problem and is way more pythonic.

Comments

0

If you can use sorted, that's the easiest way to do it. If not, this is my solution:

lst = [0, 1, 6, 2, 5, 3, 4]
new = lst[1:]
for num in range(len(new)-1, 0, -1):
    for i in range(num):
        if new[i] > new[i+1]:
            new[i], new[i+1] = new[i+1], new[i]
new.insert(0, lst[0])
print(new) 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.