首先,我在理解此问题时遇到了麻烦,因此在解释该问题时会感到困惑。
我目前正在制作一个健身房锻炼程序生成器,它将为某些肌肉群(也由我指定)随机抽取一定数量(由我指定)的运动。我有一个名为array
的数组,看起来像这样;
[['Bench Press', 'Dumbell Press', 'Chest Press Machine', 'Cable Flies', 'Cable Crossover', 'Decline Bench Press', 'Angled Dips', 'Smith Machine Bench Press', 'Inner Chest Push'],
['Incline Bench Press', 'Incline Dumbell Press', 'Incline Cable Flies', 'Incline Chest Press Machine', 'Incline Dumbell Flies', 'Raised Pushups', 'Smith Machine Incline Bench Press', 'Rotating Incline Dumbell Press', 'Inner Check Upwards Barbell Push'],
['Barbell Overhead Press', 'Dumbell Overhead Press', 'Dumbell Lateral Raise', 'Face Pulls', 'Dumbell Front Raise', 'Reverse Flies', 'Smith Shoulder Press', 'Cable Side Raise', 'Behind Head Overhead Press'],
['Dumbell Curls ', 'Hammer Curls', 'Preacher Curls', 'Machine Curls', 'Close Grip Chin Ups', 'Close Grip Pulldown', 'Half Rep Curls', 'Rotating Curls', 'Drop Set Curls'],
['Cable Pushdowns', 'Overhead Dumbell Extensions', 'Overhead Barbell Extensions', 'Upright Dips', 'Skullcrushers', 'Close Grip Benchpress', 'Tricep Kickbacks', 'Overhead Rope Extensions', 'Drop Set Pushdowns'],
['Lat Pulldowns', 'Pullups', 'Deadlifts', 'Bent Over Rows', 'Dumbell Rows', 'Rack Pulls', 'Upright Row', 'Low Rows', 'One Arm Cable Pull'],
['Squats', 'Leg Press', 'Calf Raisers', 'Leg Extensions', 'Leg Curls', 'Lunges', 'Rear Kicks', 'Abductor', 'Adductor']]
连同2d阵列的每个阵列都包含针对不同肌肉群的锻炼。
现在我创建的例程是4天的例程。第一天,胸部和肱三头肌产生(阵列1和5)。第2天是肩膀(阵列3)。第三天回来了,肱二头肌(数组4和6),最后第四天是腿(数组6)。
应该使用以下功能来设置该系统:
def FourDays():
global Day1
Day1 = [4,0,0,0,2,0,0]
global Day2
Day2 = [0,0,5,0,0,0,0]
global Day3
Day3 = [0,0,0,2,0,4,0]
global Day4
Day4 = [0,0,0,0,0,0,5]
Day1_routine = Routine_Maker3(array, Day1)
Day2_routine = Routine_Maker3(array, Day2)
Day3_routine = Routine_Maker3(array, Day3)
Day4_routine = Routine_Maker3(array, Day4)
例如-Day2
(是肩膀),在数组的第三个索引中具有数字5。这意味着从第三个数组开始的5个练习,该数组包含肩部练习。
这些变量(Day1
,Day2
,Day3
和Day4
)然后传递给以下函数,该函数负责从{ {1}}
array
请注意:def Routine_Maker3(array, exercises_per_day):
old_routine = []
pos = 0
for i in range(7):
if pos >= len(array):
pos = 0
temporal_array = [array[pos][j] for j in random.sample(range(9), exercises_per_day[pos])]
old_routine.append(temporal_array)
pos += 1
print (old_routine)
new_routine=[]
for e in old_routine:
new_routine += e
print (new_routine)
,Day1
,2
和3
应该以{{1}}的形式传递
以下是应输出的示例:
4
我得到的错误是列表索引超出范围,它说这是以下两行的问题:
exercises_per_day
答案 0 :(得分:1)
这是您的常规maker方法的一种更简单的选择。
def Routine_Maker3(array, exercises_per_day):
routine = []
for i in range(7):
routine.extend(random.sample(array[i], exercises_per_day[i]))
return routine
Routine_Maker3(array, Day1)
输出:
['Smith Machine Bench Press',
'Inner Chest Push',
'Bench Press',
'Angled Dips',
'Tricep Kickbacks',
'Overhead Barbell Extensions']
基本上,random.sample()函数采用一个数组和一个值K,并从该数组返回K个随机值。随您便。
您还使用了Routine_Maker4
而不是Routine_Maker3
。
每当您试图将列表添加到另一个列表时,如果您想要一个平面一维列表,则需要使用extend()
而不是append()
答案 1 :(得分:1)
让我们退后几步,以更深入地了解您的问题。
您想要做的是从列表列表(称为数组)中获得一个具有唯一元素的随机子集。内置的random
module提供了专用功能来实现这一目的。我正在谈论的功能称为random.sample()
。
常规语法(从文档中获取):
random.sample(population, k)
返回长度为k的唯一元素列表 从总体序列或集合中选择。用于随机抽样 无需更换。
返回一个新列表,其中包含人口中的元素 保持原始人口不变。 [...]
将这种方法应用于您的问题将如下所示:
import random
exercises_0 = ['Bench Press', 'Dumbell Press', 'Chest Press Machine', 'Cable Flies', 'Cable Crossover', 'Decline Bench Press', 'Angled Dips', 'Smith Machine Bench Press', 'Inner Chest Push']
exercises_1 = ['Incline Bench Press', 'Incline Dumbell Press', 'Incline Cable Flies', 'Incline Chest Press Machine', 'Incline Dumbell Flies', 'Raised Pushups', 'Smith Machine Incline Bench Press', 'Rotating Incline Dumbell Press', 'Inner Check Upwards Barbell Push']
exercises_2 = ['Barbell Overhead Press', 'Dumbell Overhead Press', 'Dumbell Lateral Raise', 'Face Pulls', 'Dumbell Front Raise', 'Reverse Flies', 'Smith Shoulder Press', 'Cable Side Raise', 'Behind Head Overhead Press']
exercises_3 = ['Dumbell Curls ', 'Hammer Curls', 'Preacher Curls', 'Machine Curls', 'Close Grip Chin Ups', 'Close Grip Pulldown', 'Half Rep Curls', 'Rotating Curls', 'Drop Set Curls']
exercises_4 = ['Cable Pushdowns', 'Overhead Dumbell Extensions', 'Overhead Barbell Extensions', 'Upright Dips', 'Skullcrushers', 'Close Grip Benchpress', 'Tricep Kickbacks', 'Overhead Rope Extensions', 'Drop Set Pushdowns']
exercises_5 = ['Lat Pulldowns', 'Pullups', 'Deadlifts', 'Bent Over Rows', 'Dumbell Rows', 'Rack Pulls', 'Upright Row', 'Low Rows', 'One Arm Cable Pull']
exercises_6 = ['Squats', 'Leg Press', 'Calf Raisers', 'Leg Extensions', 'Leg Curls', 'Lunges', 'Rear Kicks', 'Abductor', 'Adductor']
all_exercises = [exercises_0, exercises_1, exercises_2, exercises_3, exercises_4, exercises_5, exercises_6]
days = [
[4, 0, 0, 0, 2, 0, 0],
[0, 0, 5, 0, 0, 0, 0],
[0, 0, 0, 2, 0, 4, 0],
[0, 0, 0, 0, 0, 0, 5]
]
def get_exercises(day_number):
output = []
for idx, value in enumerate(days[day_number]):
if value:
# use `append` to get a list of lists
output.append(random.sample(all_exercises[day_number], value))
# use `extend` to get a single list
# output.extend(random.sample(all_exercises[day_number], value))
return output
exercises_to_do = get_exercises(0)
print(exercises_to_do)
为您提供以下输出:
[['Dumbell Press', 'Cable Flies', 'Decline Bench Press', 'Cable Crossover'], ['Cable Flies', 'Inner Chest Push']]
答案 2 :(得分:1)
请尝试使用以下简化代码来帮助您避免将来出现的复杂错误:
import random
exercises = {"chest": ['Bench Press', 'Dumbell Press', 'Chest Press Machine', 'Cable Flies', 'Cable Crossover', 'Decline Bench Press', 'Angled Dips', 'Smith Machine Bench Press', 'Inner Chest Push'],
"shoulders": ['Barbell Overhead Press', 'Dumbell Overhead Press', 'Dumbell Lateral Raise', 'Face Pulls', 'Dumbell Front Raise', 'Reverse Flies', 'Smith Shoulder Press', 'Cable Side Raise', 'Behind Head Overhead Press'],
"back": ['Dumbell Curls ', 'Hammer Curls', 'Preacher Curls', 'Machine Curls', 'Close Grip Chin Ups', 'Close Grip Pulldown', 'Half Rep Curls', 'Rotating Curls', 'Drop Set Curls'],
"triceps": ['Cable Pushdowns', 'Overhead Dumbell Extensions', 'Overhead Barbell Extensions', 'Upright Dips', 'Skullcrushers', 'Close Grip Benchpress', 'Tricep Kickbacks', 'Overhead Rope Extensions', 'Drop Set Pushdowns'],
"biceps": ['Lat Pulldowns', 'Pullups', 'Deadlifts', 'Bent Over Rows', 'Dumbell Rows', 'Rack Pulls', 'Upright Row', 'Low Rows', 'One Arm Cable Pull'],
}
def make_routine(*args, **kwargs):
routine = []
muscle_types = list(kwargs.keys())
for muscle_type in muscle_types:
muscle_exercises = exercises[muscle_type]
exercises_per_day = kwargs[muscle_type]
routine.extend(random.sample(muscle_exercises, exercises_per_day))
return routine
if __name__ == '__main__':
day1_routine = make_routine(chest=4, triceps=2)
day2_routine = make_routine(shoulders=5)
print("day1_routine:", day1_routine)
print("day2_routine:", day2_routine)
# You get the idea
# Just make you specifications as key word arguments in your calling function
#
答案 3 :(得分:0)
我为您的日常制作者提供一个版本:
def Routine_Maker5(array, exercises_per_day):
res = []
for exercise_type, volume in enumerate(exercises_per_day):
excercises = array[exercise_type][:]
for _ in range(volume)
select = random.randint(0, len(excercises)-1)
res.append(excercises.pop(select))
return res