【问题标题】:How to specify number of inputs and how to get each input in a new line?如何指定输入数量以及如何在新行中获取每个输入?
【发布时间】:2014-04-20 11:46:41
【问题描述】:

我是 Python 新手,我试图向用户询问所需的一些元素,然后要求在单独的行中输入每个元素,然后对该输入进行冒泡排序。

import readline
def bubbleSort(alist):
    for passnum in range(len(alist)-1,0,-1):
        for i in range(passnum):
            if alist[i]>alist[i+1]:
                temp = alist[i]
                alist[i] = alist[i+1]
                alist[i+1] = temp

alist = readlines('Enter the list to sort \n', 'r').rstrip()
alist = alist.split(',')

bubbleSort(alist)
print alist.readlines()

如果我将 readlines 更改为 raw_input ,则代码可以正常工作,但输入只有一行。有人可以帮我解决如何指定元素的编号以及如何在新行中获取每个输入吗?

【问题讨论】:

    标签: python bubble-sort


    【解决方案1】:

    试试这个:

    bubbleSort([raw_input('Enter element') for _ in range(input('Enter the number of elements needed'))])
    

    一个班轮就可以解决问题

    解释:::

    一旦您撤消列表推导和 Python 格式,我们在这里所做的基本上就是三件事。

    #Asking for the number of elements needed and making a loop that will repeat that many times
    for _ in range(input('Enter the number of elements needed')):
    
        #in each loop, retrieve an element from the user and put it into a list for later
        listvariable.append(raw_input('enter element'))
    
    #Then at the end we're taking that list and putting it through the bubbleSort
    bubbleSort(listvariable)
    

    在上面的单行解决方案中使用列表理解简化了代码。

    【讨论】:

      【解决方案2】:

      我相信这是您正在寻找的基础知识。

      ntimes = raw_input("Enter the number of lines")
      
      ntimes = int(ntimes)
      
      alist = []
      
      while (ntimes > 0):
          ntimes -= 1
          alist.append(raw_input('Enter the list to sort \n').split(','))
      
      print alist
      

      【讨论】:

      • 嗨,您的建议对于将每个输入放在一行中效果很好,但是它如何根据输入顺序开始打印元素而不是冒泡
      • 将打印列表更改为打印bubbleSort(alist)
      • 我已经更改了它,现在它给出了“无”输出
      【解决方案3】:

      Python 3:

      def bubbleSort(alist):
          for passnum in range(len(alist)-1, 0, -1):
              for i in range(passnum):
                  if alist[i] > alist[i+1]:
                      temp = alist[i]
                      alist[i] = alist[i+1]
                      alist[i+1] = temp
          return alist
      
      def main():
          lines = []
          print("Enter names (hit enter twice to bubble-sort):")
          while True:
              line = input("%3i: " % (len(lines)+1))
              if not line:
                  break
              lines.append(line)
          print("Sorted names:")
          for i, name in enumerate(bubbleSort(lines), 1):
              print("%3i. %s" % (i, name))
      
      main()
      

      输入输出:

      Enter names (hit enter twice to bubble-sort):
        1: Mark
        2: Jim
        3: Kayne
        4: Foobar
        5: Zulu
        6: Anna
        7: Yeti
        8: 
      Sorted names:
        1. Anna
        2. Foobar
        3. Jim
        4. Kayne
        5. Mark
        6. Yeti
        7. Zulu
      

      【讨论】:

      • 当我尝试运行您的代码时,它运行但不提供任何输出,甚至不要求输入
      • 你是在python 3k环境下运行的吗?对于 py 2.x,将 input 替换为 raw_input
      • 我已经换了,还是什么都没给
      猜你喜欢
      • 1970-01-01
      • 2014-06-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-01-17
      • 1970-01-01
      • 1970-01-01
      • 2014-08-27
      相关资源
      最近更新 更多