如果我的列表每次都有不同的长度,并且我想从最低到最高排序,我该怎么做?
如果我有:[-5, -23, 5, 0, 23, -6, 23, 67]
我想:[-23, -6, -5, 0, 5, 23, 23, 67]
我从这开始:
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)
但这只经过一次,我得到了:
new_list = [-23]
这是我被卡住的地方。
我如何保持循环直到len(new_list) = len(data_list)
(即所有数字都在新列表中),所有内容都排序,而不使用内置的max,min,sort函数?我不确定是否有必要创建一个新列表。
答案 0 :(得分:22)
我猜你正试图做这样的事情:
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
答案 1 :(得分:5)
这是我一直在尝试的东西。(插入排序 - 不是排序的最佳方式,但是做了工作)
def sort(list):
for index in range(1,len(list)):
value = list[index]
i = index-1
while i>=0:
if value < list[i]:
list[i+1] = list[i]
list[i] = value
i -= 1
else:
break
答案 2 :(得分:5)
l = [64, 25, 12, 22, 11, 1,2,44,3,122, 23, 34]
for i in range(len(l)):
for j in range(i + 1, len(l)):
if l[i] > l[j]:
l[i], l[j] = l[j], l[i]
print l
输出:
[1, 2, 3, 11, 12, 22, 23, 25, 34, 44, 64, 122]
答案 3 :(得分:4)
这严格遵循您的要求,不要使用sort()
,min()
,max()
,而是使用Python最佳做法,而不是重新发明轮子。
data_list = [-5, -23, 5, 0, 23, -6, 23, 67]
import heapq
heapq.heapify(data_list)
new_list = []
while data_list:
new_list.append(heapq.heappop(data_list)))
我建议您查看heapq.py
的Python库,看看它是如何工作的。 Heapsort是一种非常有趣的排序算法,因为它可以让您“排序”一个无限的流,即您可以快速获取当前最小的项目,但也可以有效地将新项目添加到要排序的数据中。
答案 4 :(得分:1)
def bubble_sort(seq):
"""Inefficiently sort the mutable sequence (list) in place.
seq MUST BE A MUTABLE SEQUENCE.
As with list.sort() and random.shuffle this does NOT return
"""
changed = True
while changed:
changed = False
for i in xrange(len(seq) - 1):
if seq[i] > seq[i+1]:
seq[i], seq[i+1] = seq[i+1], seq[i]
changed = True
return None
if __name__ == "__main__":
"""Sample usage and simple test suite"""
from random import shuffle
testset = range(100)
testcase = testset[:] # make a copy
shuffle(testcase)
assert testcase != testset # we've shuffled it
bubble_sort(testcase)
assert testcase == testset # we've unshuffled it back into a copy
答案 5 :(得分:1)
aw=[1,2,2,1,1,3,5,342,345,56,2,35,436,6,576,54,76,47,658,8758,87,878]
for i in range(aw.__len__()):
for j in range(aw.__len__()):
if aw[i] < aw[j] :aw[i],aw[j]=aw[j],aw[i]
答案 6 :(得分:1)
data = [3, 1, 5, 2, 4]
n = len(data)
for i in range(n):
for j in range(1,n):
if data[j-1] > data[j]:
(data[j-1], data[j]) = (data[j], data[j-1])
print(data)
答案 7 :(得分:0)
您可以使用迭代方法与递归函数进行一一比较。如果要降序,则结果按升序排列,然后将条件从 if l[i]>l[i+1]
更改为 if l[i]<l[i+1]
:
l=[2,1,3,4,3,2,1,2]
list_len=len(l)
def fun(l):
for i in range(list_len):
if i<list_len-1:
if l[i]>l[i+1]:
l[i],l[i+1]=l[i+1],l[i]
fun(l)
return l
print(fun(l))
答案 8 :(得分:0)
这是未排序的列表,我们想要的是 1234567
list = [3,1,2,5,4,7,6]
def sort(list):
for i in range(len(list)-1):
if list[i] > list[i+1]:
a = list[i]
list[i] = list[i+1]
list[i+1] = a
print(list)
sort(list)
对数组进行排序的最简单的方法。 我正在使用当前的冒泡排序:它检查前 2 个位置并将最小的数字向左移动,依此类推。 "-n"in 循环是为了避免缩进错误,这样做你会明白的。
答案 9 :(得分:0)
由于复杂性无关紧要,所以我向您介绍... BogoSort:
from random import shuffle
def is_sorted(ls):
for idx in range(len(ls)-1):
if x[idx] > x[idx + 1]:
return False
return True
def sort(ls):
while not is_sorted(ls):
shuffle(ls)
return ls
ls = list(range(5))
shuffle(ls)
print(
"Original: ",
ls
)
print(
"Sorted: ",
sorted(ls)
)
答案 10 :(得分:0)
这行得通!
def sorting(li):
for i in range(len(li)):
for j in range(len(li)):
if li[i] < li[j]:
li[j],li[i] = li[i],li[j]
return li
listToSort = [22,11,23,1,100,24,3,101,2,4]
print(sorting(listToSort))
答案 11 :(得分:0)
从第一位置交换值到列表末尾,此代码循环(n * n-1)/ 2次。每次它将更大的值从零索引开始推到更大的索引。
list2 = [40,-5,10,2,0,-4,-10]
for l in range(len(list2)):
for k in range(l+1,len(list2)):
if list2[k] < list2[l]:
list2[k] , list2[l] = list2[l], list2[k]
print(list2)
答案 12 :(得分:0)
n = int(input("Input list lenght: "))
lista = []
for i in range (1,n+1):
print ("A[",i,"]=")
ele = int(input())
lista.append(ele)
print("The list is: ",lista)
invers = True
while invers == True:
invers = False
for i in range (n-1):
if lista[i]>lista[i+1]:
c=lista[i+1]
lista[i+1]=lista[i]
lista[i]=c
invers = True
print("The sorted list is: ",lista)
答案 13 :(得分:0)
# Your current setup
data_list = [-5, -23, 5, 0, 23, -6, 23, 67]
new_list = []
# Sort function
for i in data_list:
new_list = [ x for x in new_list if i > x ] + [i] + [ x for x in new_list if i <= x ]
答案 14 :(得分:0)
通过使用min()函数,您可以轻松地做到这一点
`def asc(a):
b=[]
l=len(a)
for i in range(l):
x=min(a)
b.append(x)
a.remove(x)
return b
print asc([2,5,8,7,44,54,23])`
答案 15 :(得分:0)
以下是就地 Insertion sort算法的更具可读性的示例。
a = [3, 1, 5, 2, 4]
for i in a[1:]:
j = a.index(i)
while j > 0 and a[j-1] > a[j]:
a[j], a[j-1] = a[j-1], a[j]
j = j - 1
答案 16 :(得分:0)
解决方案
mylist = [1, 6, 7, 8, 1, 10, 15, 9]
print(mylist)
n = len(mylist)
for i in range(n):
for j in range(1, n-i):
if mylist[j-1] > mylist[j]:
(mylist[j-1], mylist[j]) = (mylist[j], mylist[j-1])
print(mylist)
答案 17 :(得分:0)
我的方法 -
s = [-5, -23, 5, 0, 23, -6, 23, 67]
nl = []
for i in range(len(s)):
a = min(s)
nl.append(a)
s.remove(a)
print nl
答案 18 :(得分:0)
这是一个not very efficient排序算法:)
>>> data_list = [-5, -23, 5, 0, 23, -6, 23, 67]
>>> from itertools import permutations
>>> for p in permutations(data_list):
... if all(i<=j for i,j in zip(p,p[1:])):
... print p
... break
...
(-23, -6, -5, 0, 5, 23, 23, 67)
答案 19 :(得分:-1)
如何在len(new_list)= len(data_list)
之前保持循环
while len(new_list) != len(data_list):
# ...
也许是这样的?
不,没有必要创建一个新列表;大多数排序算法都可以通过更改列表来实现。
您可能要做的是Selection sort并单独列出。有关该排序算法的更多信息,请参阅维基百科文章,您将看到它如何与单个列表一起使用,以及它的效率(扰码:它不是)。
答案 20 :(得分:-3)
def getIndexOfMaximum(list1):
index = 0
emptyList = []
value = list1[0]
c = 0
while (c == 0):
for cell in list1:
index += 1
if (cell >= value):
value = cell
hold = index -1
if (len(list1) == index):
emptyList += [value]
del list1[hold]
index = 0
value = 0
if (len(list1) == 1):
newList = emptyList + list1
del list1[index]
c = 1
return newList
print(getIndexOfMaximum([2,5,8,7,44,54,23]))
#TRY THIS!!!