【问题标题】:Sorting a list from low to high from the left in python, using if statements使用if语句在python中从左到高对列表进行排序
【发布时间】: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


【解决方案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()

【讨论】:

    【解决方案2】:
    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()
    

    【讨论】:

      【解决方案3】:

      为了打印程序打印的相反顺序,请将您的 i &lt; j 更改为 i &gt; j 并将 j &lt; i 更改为 i &lt; j。对您的 numList[i] &gt; 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]
      

      【讨论】:

      • 我们被指示使用 JES 编辑器。可能 printNow() 是内置函数之一
      • @HowardMkongo 哦,好的。这是有道理的,那就别管它了:)
      • 这可以解释为什么他们在 python 中编写 java :(
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-10-23
      • 2021-05-03
      • 2015-01-24
      • 1970-01-01
      相关资源
      最近更新 更多