[英]How to get multiple random values from a specified area in a dictionary
I am trying to create a workout simulator.我正在尝试创建一个锻炼模拟器。 If the user wants to target two areas, I want to be able to take 3 exercises from each section and then combine them into their own set.如果用户想要针对两个区域,我希望能够从每个部分进行 3 个练习,然后将它们组合成自己的集合。 How would I take 3 random exercises?我将如何进行 3 次随机练习? I have tried using random.sample with no luck我试过使用 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)
You can use shuffle
:您可以使用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))
This could also work, though sample
is being deprecated for sets so you have to do some more work:这也可以工作,虽然sample
已被弃用,所以你必须做更多的工作:
list(chain(*(sample(list(v), 3) for k, v in musclegroup_exercises.items() if k in selected_groups)))
You can use random.sample()
.您可以使用random.sample()
。 Pass as parameters the list from which to get the elements and the length of the new list.将要从中获取元素的列表和新列表的长度作为参数传递。
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
NOTE: Always import the packages you need at the beginning of the program, do not import them in the meantime inside an if or functions.注意:始终在程序开始时导入您需要的包,同时不要在 if 或函数中导入它们。
To take the exercises from only one area, you can simply change the key you use to access to musclegroup_exercises
:要仅从一个区域进行练习,您可以简单地更改用于访问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)
Maybe use random.choices
since sample
is deprecated:也许使用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]
Or in more understandable form:或者以更易于理解的形式:
exercices = []
for i in (musclegroup1, musclegroup2):
exercices.extend(random.choices(tuple(musclegroup_exercises[i]), k=3))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.