【问题标题】:Building a randomized list in Python在 Python 中构建随机列表
【发布时间】:2021-05-24 04:40:35
【问题描述】:

我的代码有点问题。我正在尝试构建一个程序,每天生成一个新的锻炼程序。它应该从相同的锻炼列表中提取并创建用户当天将使用的新列表。当我运行程序时,它返回一个重复 4 次的相同项目的列表,而它应该是一个包含 4 个唯一项目的列表。我对 Python 和编程很陌生,但我似乎无法在任何地方找到解决方案。我将不胜感激任何帮助或建议。如何从列表中获取 4 个随机项目,将它们附加到新列表中,并且没有重复?

import random

chest_day_heavy = [["benchpress"], ["incline benchpress"], ["DB benchpress"], ["inclince DB press"],["seated machine press"]]
def workout_generator(muscle_group):
  workout_otd = []
  random_generator = random.randint(0,len(muscle_group))
  while len(workout_otd) < 4:
    workout_otd.append(muscle_group[random_generator])
    for i in range(len(workout_otd)):
      if muscle_group[random_generator] == workout_otd[i]:
        continue
  return workout_otd

print(workout_generator(chest_day_heavy))

结果,例如,我想是这样的:[["inclince DB press"],["seated machine press"],["benchpress"],["DB benchpress"]]。 没有重复的项目。 相反,我得到的是这样的:[["seated machine press"],["seated machine press"],["seated machine press"],["seated machine press"]]

自学一个月后,我可能已经咬得比我能咀嚼的更多了哈哈

【问题讨论】:

    标签: python list random


    【解决方案1】:

    这可能是最简单的方法:

    import random
    
    chest_day_heavy = [["benchpress"], ["incline benchpress"], ["DB benchpress"], ["inclince DB press"],["seated machine press"]]
    workout_otd=[]
    def workout_generator(workout_otd):
        workout_otd=chest_day_heavy.copy()
        random.shuffle(workout_otd)
        return workout_otd
    
    workout_otd= workout_generator(workout_otd)
    print(workout_otd)
    

    使用 random.shuffle() 函数,它会随机播放所有项目

    要仅打印前 4 个元素,您可以这样做:print(workout_otd[0:4])

    【讨论】:

    • 这帮助解决了我所有的问题!我感谢大家的帮助!我使用了 random.shuffle(),像 Leemosh 所说的那样打印了前 4 个项目,我有了我的解决方案。我终于可以继续下一步了。非常感谢!
    【解决方案2】:

    您可以使用random module 的其他功能大大减少您的代码,

    import random
    
    chest_day_heavy = [["benchpress"], ["incline benchpress"],
                            ["DB benchpress"], ["inclince DB press"],
                            ["seated machine press"]]
    
    def workout_generator(muscle_group, n_wk):
        random.shuffle(muscle_group)
        return muscle_group[:min(n_wk,len(muscle_group))]
    
    print(workout_generator(chest_day_heavy, 4))
    

    现在列表被随机重新排列,函数根据请求从列表中返回前 N 个元素。为了灵活性,元素的数量 - 锻炼例程 - 是函数参数列表的一部分。

    【讨论】:

      【解决方案3】:

      OP 的random.randint 用法的问题在于它仅固定到 1 个索引(因此重复相同的单个项目)。

      random模块自带了一些优秀的功能,比如shuffle(),可以这样使用:

      def workout_generator(muscle_group):
          r = [*muscle_group]  # shallow copy the input list
          random.shuffle(r)
          return r
      

      【讨论】:

        【解决方案4】:

        正如 Gabriele 所说,.choise() 是这样做的好方法,或者您可以使用

        random.shuffle(lst)
        

        然后打印前 4 个项目。

        【讨论】:

          【解决方案5】:
          random_generator = random.randint(0,len(muscle_group))
          

          应该在while内:

          ....
          while len(workout_otd) < 4:
              random_generator = random.randint(0,len(muscle_group))
              workout_otd.append(muscle_group[random_generator])
              ....
          

          【讨论】:

          • 成功了!谢谢!现在要弄清楚如何确保我没有重复的项目。非常感谢您的帮助!
          猜你喜欢
          • 2014-01-11
          • 2011-05-09
          • 1970-01-01
          • 1970-01-01
          • 2016-03-01
          • 1970-01-01
          • 2014-12-31
          • 1970-01-01
          相关资源
          最近更新 更多