【发布时间】:2018-03-20 06:52:26
【问题描述】:
我有一个任务,我要对列表从低到高进行排序。但是我的程序给出了预期顺序的倒数。这是我徒劳的尝试。请帮助我,我真的被卡住了。希望这是一个菜鸟的错误。
from random import *
def main():
# initializing variables
numList = []; #the list of integers
numListLength = 25; #the number of integers in numList array
maxShuffles = 1000; #the number of times numList is to be shuffled
#populating numList
while len(numList) < numListLength :
randomElement = randint(-100, 100)
numList.append(randomElement)
printNow("List before shuffling: " )
printNow(numList)
#shuffle the list multiple times
shuffleCount = 0
while shuffleCount < maxShuffles :
i = randrange( 0, len(numList) )
j = randrange( 0, len(numList) )
if i < j :
#compare the contents of those locations
if numList[i] < numList[j] :
#swap the contents
original_i = numList[i]
original_j = numList[j]
numList[i] = original_j
numList[j] = original_i
elif j < i :
if numList[j] < numList[i] :
#swap the contents
original_i = numList[i]
original_j = numList[j]
numList[i] = original_j
numList[j] = original_i
#increment shuffleCounter
shuffleCount = shuffleCount + 1
#shuffling done, display results
printNow("List after shuffling: ")
printNow(numList)
main()
【问题讨论】:
-
请详细说明您的问题。阅读How do I ask a good question? 并编辑您的问题。
-
这实际上是非常糟糕的 Python 代码。你的老师写了多少?看起来作业是从 java 移植的
-
你有
printNow函数吗?您的代码似乎无法打印。
标签: python loops sorting nested-if