【发布时间】: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"]]
自学一个月后,我可能已经咬得比我能咀嚼的更多了哈哈
【问题讨论】: