用Python构建随机列表

用Python构建随机列表,python,list,random,Python,List,Random,我这里的代码有点问题。我正在尝试建立一个计划,每天生成一个新的锻炼计划。它应该从相同的训练列表中提取,并创建用户当天将使用的新列表。当我运行程序时,它返回一个重复4次的相同项目列表,而它应该是一个包含4个唯一项目的列表。我对Python和编程相当陌生,但似乎在任何地方都找不到解决方案。我将非常感谢任何帮助或建议。如何从一个列表中获取4个随机项,将它们附加到一个新列表中,并且没有重复项 import random chest_day_heavy = [["benchpress"

我这里的代码有点问题。我正在尝试建立一个计划,每天生成一个新的锻炼计划。它应该从相同的训练列表中提取,并创建用户当天将使用的新列表。当我运行程序时,它返回一个重复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))
随机导入
胸部日重=[“台式压力机”]、[“倾斜台式压力机”]、[“DB台式压力机”]、[“倾斜DB压力机”]、[“坐式压力机”]
def训练_发生器(肌肉组):
训练时间=[]
random_生成器=random.randint(0,len(肌肉组))
而len(训练时间)<4:
训练附加(肌肉组[随机生成器])
对于射程内的i(len(训练时间)):
如果肌肉组[random\u generator]==训练\u otd[i]:
持续
返回训练
打印(训练\生成器(胸部\白天\沉重))
例如,结果是:
[[“倾斜DB压力机”],[“坐式机器压力机”],[“台式压力机”],[“DB台式压力机”]
。 没有重复的项目。 相反,我得到的是这样的东西:
[[“坐着的机器按”],[“坐着的机器按”],[“坐着的机器按”],[“坐着的机器按”]

经过一个月的自学,我可能已经吃得太多了,哈哈

random_generator = random.randint(0,len(muscle_group))
应在内部,而:

....
while len(workout_otd) < 4:
    random_generator = random.randint(0,len(muscle_group))
    workout_otd.append(muscle_group[random_generator])
    ....
。。。。
而len(训练时间)<4:
random_生成器=random.randint(0,len(肌肉组))
训练附加(肌肉组[随机生成器])
....

正如Gabriele所说的
。choise()
是一种很好的方法,或者您可以使用

random.shuffle(lst)

然后打印前4个项目。

OP的
random.ranndint
用法是它只锁定到1个索引(因此重复同一个项目)

random
模块附带了一些优秀的功能,如
shuffle()
,可用于:

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

这可能是最简单的方法:

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\u otd[0:4])
您可以使用


现在,列表被随机地重新排列到位,函数将根据请求返回列表中的前N个元素。为了灵活性,函数参数列表中的元素数(训练例程)是有效的。

!非常感谢。现在我想知道如何确保我没有重复的项目。我真的很感谢你的帮助!这帮我解决了所有的问题!我感谢大家的帮助!我使用了random.shuffle(),像Leemosh说的那样打印了前4项,我有了我的解决方案。我终于可以进入下一步了。非常感谢你!
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))