【问题标题】:Python Generator: How do I generate pairs from two different lists based on user input (of how many pairs to print)Python 生成器:如何根据用户输入从两个不同的列表中生成对(要打印多少对)
【发布时间】:2026-02-15 18:45:02
【问题描述】:

鉴于有两个列表:

colorList1 = ['red', 'orange', 'pink', 'brown', 'green', 'yellow']
colorList2 = ['purple', 'blue', 'violet', 'black, 'cyan', 'white']

如何根据用户输入的数字从每个列表(每行)生成和打印一种颜色,从列表的开头开始。而且,在用户按下 Enter 键之前,如何每次最多只生成 2 对颜色。

例子:

userinput = int(input("Enter a limit: "))  # user enters 5

输出:

red     purple
orange  blue       
press Enter key for next 2 pairs of colors     # user presses Enter key to generate the next pair
pink    violet
brown   black      
press Enter key for next 2 pairs of colors     # user presses Enter key to generate the next pair
green   cyan              # last pair since user entered 5
End of list of colors

【问题讨论】:

    标签: python python-3.x list generator


    【解决方案1】:

    你可以试试这样的。

    colorList1 = ['red', 'orange', 'pink', 'brown', 'green', 'yellow']
    colorList2 = ['purple', 'blue', 'violet', 'black', 'cyan', 'white']
    
    def print_color():
        userInput = input("Enter amount to print: ")
    
        try:
           userInput = int(userInput)
        except ValueError:
            print("User Input must be a positive integer")
            return
    
        if userInput < 0:
            print("User Input must be a positive integer")
            return
    
        for i in range(0, int(userInput)):
    
            if i >= len(colorList1) or i >= len(colorList2):
                print("End of list of colors")
                return
    
            if i % 2 == 0 and i != 0:
                input("press Enter key for next 2 pairs of colors")
    
            print(colorList1[i], colorList2[i])
    
    print_color()
    
    

    编辑:我已更新代码以包含基本错误检查

    【讨论】:

      【解决方案2】:

      你可以这样做:

      colorList1 = ['red', 'orange', 'pink', 'brown', 'green', 'yellow']
      colorList2 = ['purple', 'blue', 'violet', 'black', 'cyan', 'white']
      color_pairs = iter(zip(colorList1,colorList2))
      userinput = int(input("Enter a limit: "))  # user enters 5
      i = 0
      while i<userinput:
          if i%2 == 0: input(f"press Enter key for next 2 pairs of colors")
          print(*next(color_pairs))
          if i+1 == len(colorList1):
              break
          i += 1
      print("\nEnd of list of colors")
      

      输出:

      Enter a limit: 5
      
      press Enter key for next 2 pairs of colors
      red purple
      orange blue
      
      press Enter key for next 2 pairs of colors
      pink violet
      brown black
      
      press Enter key for next 2 pairs of colors
      green cyan
      
      End of list of colors
      

      限制 = 7:

      Enter a limit: 7
      
      press Enter key for next 2 pairs of colors
      red purple
      orange blue
      
      press Enter key for next 2 pairs of colors
      pink violet
      brown black
      
      press Enter key for next 2 pairs of colors
      green cyan
      yellow white
      
      End of list of colors
      

      【讨论】: