Home > Blockchain >  How to get multiple random values from a specified area in a dictionary
How to get multiple random values from a specified area in a dictionary

Time:12-03

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. How would I take 3 random exercises? I have tried using random.sample with no luck

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)

CodePudding user response:

You can use 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:

list(chain(*(sample(list(v), 3) for k, v in musclegroup_exercises.items() if k in selected_groups)))

CodePudding user response:

You can use 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.

To take the exercises from only one area, you can simply change the key you use to access to 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)

CodePudding user response:

Maybe use random.choices since sample is deprecated:

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))
  • Related