python:订购一个没有内置排序,最小,最大功能的数字列表 [英] python: order a list of numbers without built-in sort, min, max function
问题描述
如果我有: 我想: 我从这开始: 只有经过一次,我得到: 这是我陷入困境的地方。 如何循环直到 [ - 5,-23,5,0,23,-6,23,67] $ c $
[ - 23,-6,-5,0,5,23,23,67] $ c
$ b
$ $ $ $ $ $ $ $ $ data_list = -5,-23,5,0,23,-6,23,67]
new_list = []
最小值= data_list [0]#列表中的任意数字data_list中x的
:
如果x <最低:
最低=价值
new_list.append(i)
new_list = [-23]
len(new_list)= len(data_list)
(也就是说所有的数字都在新列表中),所有的东西都不用内置的max,min,sort函数排序?我不确定是否有必要创建一个新的列表。
data_list = [-5,-23,5,0,23,-6,23,67]
new_list = []
while data_list :
minimum = data_list [0]#列表中的任意数字
数据列表中的x:
如果x <最小值:
最小值= x
new_list.append(最小值)
data_list.remove(最小值)
print new_list
If I have a list that varies in length each time and I want to sort it from lowest to highest, how would I do that?
If I have: [-5, -23, 5, 0, 23, -6, 23, 67]
I want: [-23, -6, -5, 0, 5, 23, 23, 67]
I start with this:
data_list = [-5, -23, 5, 0, 23, -6, 23, 67]
new_list = []
minimum = data_list[0] # arbitrary number in list
for x in data_list:
if x < minimum:
minimum = value
new_list.append(i)
BUT this only goes through once and I get:
new_list = [-23]
This is where I get stuck.
How do I keep looping through until the len(new_list) = len(data_list)
(i.e. all the numbers are in the new list) with everything sorted without using the built in max, min, sort functions? I'm not sure if it's necessary to create a new list either.
I guess you are trying to do something like this:
data_list = [-5, -23, 5, 0, 23, -6, 23, 67]
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
这篇关于python:订购一个没有内置排序,最小,最大功能的数字列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!