我对编程还很陌生;我只学了几个星期的Python。最近有一个练习要求我生成一个整数列表,然后在一个单独的列表中手动将数字从最低到最高排序。import random
unordered = list(range(10))
ordered = []
lowest = 0
i = 0
random.shuffle(unordered)
lowest = unordered[0]
while i in unordered:
if unordered[i] < lowest:
lowest = unordered[i]
i += 1
if i >= len(unordered):
i = 0
ordered.append(lowest)
unordered.remove(lowest)
lowest = unordered[i]
print(ordered)
这就是我目前所拥有的,坦率地说,这根本不起作用。我得到的伪代码是:Create an empty list to hold the ordered elements
While there are still elements in the unordered list
Set a variable, lowest, to the first element in the unordered list
For each element in the unordered list
If the element is lower than lowest
Assign the value of that element to lowest
Append lowest to the ordered list
Remove lowest from the unordered list
Print out the ordered list
到目前为止,我遇到的最大问题是,我的计数器不能可靠地让我从无序的列表中找出最低的数字。然后我在索引我的列表时遇到问题,即索引超出范围。有谁能给我一点反馈,告诉我哪里出错了吗?
另外,我还得到了一些我不太确定的信息:You can use an established method to sort the list called the Selection Sort.
我这次不应该使用Python内置的排序方法。一切都应该手动完成。谢谢你的帮助!