【发布时间】:2019-05-31 21:47:52
【问题描述】:
首先,我无法理解这个问题,因此在解释时如有任何混淆,我深表歉意。
我目前正在制作一个健身房锻炼例程生成器,它将为某些肌肉组(也由我指定)随机提取一定数量(由我指定)的锻炼。我有一个名为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']]
带有二维数组的每个数组都包含针对不同肌肉群的练习。
现在我正在创建的例程是 4 天例程。第一天,正在生成胸部和三头肌(数组 1 和 5)。第 2 天是肩膀(阵列 3)。第 3 天是背部和二头肌(阵列 4 和 6),最后是第 4 天是腿(阵列 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)然后被传递给以下函数,该函数负责从 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,3and4 应传递为exercises_per_day
这是一个应该输出的例子:
['Cable Crossover', 'Cable Flies', 'Smith Machine Bench Press', 'Inner Chest Push', 'Upright Dips', 'Overhead Dumbell Extensions']
['Behind Head Overhead Press', 'Dumbell Overhead Press', 'Smith Shoulder Press', 'Face Pulls', 'Dumbell Lateral Raise']
['Machine Curls', 'Close Grip Pulldown', 'Lat Pulldowns', 'One Arm Cable Pull', 'Pullups', 'Upright Row']
['Abductor', 'Rear Kicks', 'Lunges', 'Leg Curls', 'Squats']
我得到的错误是列表索引超出范围,它说这是这两行的问题:
Day1_routine = Routine_Maker4(array, Day1)
temporal_array = [array[pos][j] for j in random.sample(range(9), exercises_per_day[pos])]
【问题讨论】:
-
我进行了编辑,使其更清晰
-
你能告诉我你是如何初始化你的
array变量的吗?它看起来是一个 3d 数组...Nvm 你编辑了它。 -
是的,很抱歉,当我复制数组时,我的复制和粘贴搞砸了 - 现在看起来就是数组的样子
-
为什么你在
for i in range(7)中设置range(7) -
这样它就可以遍历整个数组。 (确保它遍历数组中的每个值,即使它是 0)