使用 if 语句在 python 中从左到右对列表进行排序
Sorting a list from low to high from the left in python, using if statements
我有一项作业,我要将列表从低到高排序。但是我的程序给出了与预期顺序相反的顺序。这是我对作业的徒劳尝试。请帮助我,我真的被困住了。希望这是一个菜鸟错误。
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()
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()
为了打印与程序打印顺序相反的顺序,请将 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]
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()
我有一项作业,我要将列表从低到高排序。但是我的程序给出了与预期顺序相反的顺序。这是我对作业的徒劳尝试。请帮助我,我真的被困住了。希望这是一个菜鸟错误。
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()
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()
为了打印与程序打印顺序相反的顺序,请将 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]
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()