我正在嘗試創建一個鍛煉模擬器。如果用戶想要針對兩個領域,我希望能夠從每個部分中選取 3 個練習,然后將它們組合成自己的一組練習。我將如何進行 3 個隨機練習?我試過使用 random.sample 沒有運氣
musclegroup_exercises = {
'legs': {"squat", "calf raises", "hamstring curls", "deadlifts", "walking lunges"},
'chest': {"barbell bench", "pushups", "cable fly", "dumbbell fly", "dumbbell incline bench press"},
'arms': {"bicep curls", "kickbacks", "tricep pushdown", "reverse curls", "hammer curl"},
'shoulders':{"shoulder press", "lateral raise", "barbell shrug", "bent over reverse flys", "push press"},
'back':{"dumbbell rows", "back extension", "pull ups", "lat pull downs", "machine seated row"},
'core':{"sit ups", "crunches", "russian twists", "bicycles", "planks"},
}
print('Here are the possible muscle groups you can target: Legs, Chest, Arms, Shoulders, Back, Core')
print('Here are the possible intensity levels: Easy, Medium, Hard')
num = int(input('Would you like to target one or two muscle groups? '))
if num == 1:
musclegroup = input('What muscle group would you like to target? ')
if num == 2:
musclegroup1 = input('What is the first musclegroup you would like to target? ')
musclegroup2 = input('What is the second musclegroup you would like to target? ')
intensity = input('What intensity level would you like? ')
if intensity == 'Easy':
rate = '65%'
if intensity == 'Medium':
rate = '80%'
if intensity == 'Hard':
rate = '90%'
def createworkout1(y):
for exercise in musclegroup_exercises[musclegroup.lower()]:
print(exercise)
def createworkout2(j,k):
import random
half1 = random.sample(musclegroup_exercises[musclegroup1.lower()].items(),3)
uj5u.com熱心網友回復:
您可以使用shuffle:
from itertools import chain
from random import shuffle
musclegroup_exercises = {
'legs': {"squat", "calf raises", "hamstring curls", "deadlifts", "walking lunges"},
'chest': {"barbell bench", "pushups", "cable fly", "dumbbell fly", "dumbbell incline bench press"},
'arms': {"bicep curls", "kickbacks", "tricep pushdown", "reverse curls", "hammer curl"},
'shoulders':{"shoulder press", "lateral raise", "barbell shrug", "bent over reverse flys", "push press"},
'back':{"dumbbell rows", "back extension", "pull ups", "lat pull downs", "machine seated row"},
'core':{"sit ups", "crunches", "russian twists", "bicycles", "planks"},
}
def shuffled_group(group):
group = [i for i in group]
shuffle(group)
return group
selected_groups = ["back", "core"] # This should come from your input, though, not be hardcoded.
targets = list(chain(*(shuffled_group(v)[:3] for k, v in musclegroup_exercises.items()) if k in selected_groups))
這也可以作業,但sample不推薦用于集合,所以你必須做更多的作業:
list(chain(*(sample(list(v), 3) for k, v in musclegroup_exercises.items() if k in selected_groups)))
uj5u.com熱心網友回復:
您可以使用random.sample(). 將要從中獲取元素的串列和新串列的長度作為引數傳遞。
import random
musclegroup_exercises = {
"legs":["squat", "calf raises", "hamstring curls", "deadlifts", "walking lunges"],
"chest":["barbell bench", "pushups", "cable fly", "dumbbell fly", "dumbbell incline bench press"],
"arms":["bicep curls", "kickbacks", "tricep pushdown", "reverse curls", "hammer curl"],
"shoulders":["shoulder press", "lateral raise", "barbell shrug", "bent over reverse flys", "push press"],
"back":["dumbbell rows", "back extension", "pull ups", "lat pull downs", "machine seated row"],
"core":["sit ups", "crunches", "russian twists", "bicycles", "planks"],
}
random_exercises = {}
exercises_per_group = 3
for exercise_type in musclegroup_exercises:
exercises = exercise = random.sample(musclegroup_exercises[exercise_type], exercises_per_group)
random_exercises[exercise_type] = exercises
注意: 始終在程式開始時匯入您需要的包,不要在 if 或函式中同時匯入它們。
要僅從一個區域進行練習,您只需更改用于訪問的密鑰musclegroup_exercises:
import random
musclegroup_exercises = {
"legs":["squat", "calf raises", "hamstring curls", "deadlifts", "walking lunges"],
"chest":["barbell bench", "pushups", "cable fly", "dumbbell fly", "dumbbell incline bench press"],
"arms":["bicep curls", "kickbacks", "tricep pushdown", "reverse curls", "hammer curl"],
"shoulders":["shoulder press", "lateral raise", "barbell shrug", "bent over reverse flys", "push press"],
"back":["dumbbell rows", "back extension", "pull ups", "lat pull downs", "machine seated row"],
"core":["sit ups", "crunches", "russian twists", "bicycles", "planks"],
}
exercises_per_group = 3
exercise_type = "legs"
exercises = random.sample(musclegroup_exercises[exercise_type], exercises_per_group)
uj5u.com熱心網友回復:
也許使用random.choices因為sample已被棄用:
exercices = [j for z in [random.choices(tuple(musclegroup_exercises[i]),k=3) for i in (musclegroup1, musclegroup2)] for j in z]
或者以更容易理解的形式:
exercices = []
for i in (musclegroup1, musclegroup2):
exercices.extend(random.choices(tuple(musclegroup_exercises[i]), k=3))
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/375720.html
