python:订购一个没有内置排序,最小,最大功能的数字列表 [英] python: order a list of numbers without built-in sort, min, max function

查看:215
本文介绍了python:订购一个没有内置排序,最小,最大功能的数字列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我每次都有一个长度不一的列表,而且我想从最低到最高排序,我该怎么做?



如果我有: [ - 5,-23,5,0,23,-6,23,67]

我想: [ - 23,-6,-5,0,5,23,23,67]

$ 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆