我有一个作业,我可以从低到高对列表进行排序。但是我的程序给出了预期顺序的反转。这是我在任务中的徒劳尝试。请帮帮我,我真的被卡住了。希望这是一个noob错误。
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()
答案 0 :(得分:1)
from random import randint, randrange
def main():
# initializing variables
num_list_length = 25 # the number of integers in num_list array
max_shuffles = 1000 # the number of times num_list is to be shuffled
# populating num_list
num_list = [randint(-100,100) for i in range(num_list_length)]
print("List before shuffling: " )
print(num_list)
# shuffle the list multiple times
for shuffle_count in range(max_shuffles):
i = randrange(0, len(num_list))
j = randrange(0, len(num_list))
if i > j: # ensure i is always smaller than j
i, j = j, i
#compare the contents of those locations
if num_list[i] < num_list[j]:
num_list[i], num_list[j] = num_list[j], num_list[i]
#shuffling done, display results
print("List after shuffling: ")
print(num_list)
if __name__ == "__main__":
main()
答案 1 :(得分:0)
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] : #Inverted < sign
#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] : #Inverted < sign
#swap the contents
original_i = numList[i]
original_j = numList[j]
numList[i] = original_j
numList[j] = original_i
else: #Handling i==j case
continue
#increment shuffleCounter
shuffleCount = shuffleCount + 1
#shuffling done, display results
printNow("List after shuffling: ")
printNow(numList)
main()
答案 2 :(得分:0)
要打印程序打印的相反顺序,请将i < j
更改为i > j
,将j < i
更改为i < j
。对numList[i] > numList[j]
执行相同的操作。
您的代码现在将打印出如下内容:
List before shuffling:
[26, 52, -58, 48, -91, -53, -20, -7, 78, -74, 10, -8, 29, -57, 31, 80, -76, -48, 45, -59, -46, -23, 33, -64, -89]
List after shuffling:
[-91, -89, -76, -74, -64, -59, -58, -57, -53, -46, -48, -23, -20, -8, -7, 10, 26, 29, 31, 33, 45, 48, 52, 78, 80]