如何从列表中随机提取并放置到Python中的另一个列表中?

时间:2019-05-01 19:35:48

标签: python

我正在尝试使用Python创建锻炼例程生成器。我列出了要锻炼的每个肌肉群的锻炼清单。我在制作不同的运动列表时遇到麻烦,这些运动列表从肌肉组列表中随机抽取了一个运动。我已将锻炼清单与一周中的特定日期配对。在摆脱分隔每个印刷练习的最后一个逗号时,也需要帮助。

#Muscle Group Exercises

Chest= ["Barbell Bench Press", "Flat Bench Dumbbell Press", "Pushups", "Machine Decline Press", "Seated Machine Chest Press", "Incline Dumbbell Press", "Chest Dips", "Incline Bench Cable Fly", "Incline Dumbell Pull-Over", "Pec-Deck Machine"]
Back= ["Barbell Deadlift", "Bent-Over Barbell Deadlift", "Wide-Grip Pull-Up", "Standing T-Bar Row", "Wide-Grip Seated Cable Row", "Reverse-Grip Smith Machine Row", "Close-Grip Pull-Down", "Single-Arm Dumbbell Row", "Decline Bench Dumbbell Pull-Over", "Single-Arm Smith Machine Row"]
Shoulders= ["Barbell Push Press", "Standing Military Press", "Dumbbell Incline Row", "Seated Overhead Dumbbell Press", "Seated Overhead Barbell Press", "Upright Row", "Arnold Press", "Machine Rear-Delt Fly", "Dumbbell Lateral Raise", "Front Dumbbell Raise"]
Legs= ["Squat", "Leg Curl", "Olympic Lift: Snatch and Power Clean", "Leg Extension", "Bulgarian Split Squat", "Hack Squat", "Dumbbell Lunge", "Leg Press", "Romanian Deadlift", "Machine Squat"]
Biceps= ["Standing Dumbbell Curl", "Hammer Curl", "Incline Dumbbell Curl", "Zottman Curl", "Barbell Bent-over Row", "Chin-up", "Regular EZ Bar Curl", "Underhand Seated Row", "Preacher Curl"]
Triceps= ["Close-grip Bench Press", "Rope Tricep Pushdown", "Tricep Dips", "Overhead Triceps Extension", "Skullcrushers", "Diamond Pushups", "Tricep Kickback", "Dumbbell Press", "Pushups", "One Arm Kettlebell Press"]
Aerobic= ["mile run", "jumping jacks", "sprints", "burpees", "cycling"]

#How I tried to pull randomly from lists

import random
a = Chest[random.randint(0, len(Chest)-1)]
b = Back[random.randint(0, len(Back)-1)]
c = Shoulders[random.randint(0, len(Shoulders)-1)]
d = Legs[random.randint(0, len(Legs)-1)]
e = Biceps[random.randint(0, len(Biceps)-1)]
f = Triceps[random.randint(0, len(Triceps)-1)]
g = Aerobic[random.randint(0, len(Aerobic)-1)]

exercise1= ["a", "b", "c", "d", "e", "f"]
exercise2= ["d", "f", "e", "a", "b", "c"]
exercise3= ["c", "b", "a", "d", "f", "e"]
exercise4= ["g"]
Rest= ["Recovery Day"]

#Workout Routine Organizer

w = [['Monday:',exercise1], ['Tuesday:',Rest], ['Wednesday:',exercise2], ['Thursday:',Rest], ['Friday:',exercise3], ['Saturday:',exercise4], ['Sunday:',Rest]]

y=0
for list in w:
    print(w[y][0])
    for x in w[y][1]:
        print (x, end= ", ")
    print("\n")
    y+=1

2 个答案:

答案 0 :(得分:0)

在我看来,您可能意味着以下意思:

exercise1 = [a, b, c, d, e, f]
exercise2 = [d, f, e, a, b, c]
exercise3 = [c, b, a, d, f, e]
exercise4 = [g]

您所拥有的代码是在exercise 1至5中添加一个字符串列表,而不是您存储在这些变量中的实际列表。

答案 1 :(得分:0)

代码的一些错误和建议

  1. 您可以使用random.choice从列表中选择一个随机元素
  2. 我认为在exercise1, exercise2...变量中,您打算执行[a, b, c, d, e, f]
  3. 您可以从w[y][1]中创建一个字符串并进行打印以去除最后一个逗号
Chest= ["Barbell Bench Press", "Flat Bench Dumbbell Press", "Pushups", "Machine Decline Press", "Seated Machine Chest Press", "Incline Dumbbell Press", "Chest Dips", "Incline Bench Cable Fly", "Incline Dumbell Pull-Over", "Pec-Deck Machine"]
Back= ["Barbell Deadlift", "Bent-Over Barbell Deadlift", "Wide-Grip Pull-Up", "Standing T-Bar Row", "Wide-Grip Seated Cable Row", "Reverse-Grip Smith Machine Row", "Close-Grip Pull-Down", "Single-Arm Dumbbell Row", "Decline Bench Dumbbell Pull-Over", "Single-Arm Smith Machine Row"]
Shoulders= ["Barbell Push Press", "Standing Military Press", "Dumbbell Incline Row", "Seated Overhead Dumbbell Press", "Seated Overhead Barbell Press", "Upright Row", "Arnold Press", "Machine Rear-Delt Fly", "Dumbbell Lateral Raise", "Front Dumbbell Raise"]
Legs= ["Squat", "Leg Curl", "Olympic Lift: Snatch and Power Clean", "Leg Extension", "Bulgarian Split Squat", "Hack Squat", "Dumbbell Lunge", "Leg Press", "Romanian Deadlift", "Machine Squat"]
Biceps= ["Standing Dumbbell Curl", "Hammer Curl", "Incline Dumbbell Curl", "Zottman Curl", "Barbell Bent-over Row", "Chin-up", "Regular EZ Bar Curl", "Underhand Seated Row", "Preacher Curl"]
Triceps= ["Close-grip Bench Press", "Rope Tricep Pushdown", "Tricep Dips", "Overhead Triceps Extension", "Skullcrushers", "Diamond Pushups", "Tricep Kickback", "Dumbbell Press", "Pushups", "One Arm Kettlebell Press"]
Aerobic= ["mile run", "jumping jacks", "sprints", "burpees", "cycling"]

#How I tried to pull randomly from lists

import random

#Choose random exercises from all lists
a = random.choice(Chest)
b = random.choice(Back)
c = random.choice(Shoulders)
d = random.choice(Legs)
e = random.choice(Biceps)
f = random.choice(Triceps)
g = random.choice(Aerobic)

exercise1= [a, b, c, d, e, f]
exercise2= [d, f, e, a, b, c]
exercise3= [c, b, a, d, f, e]
exercise4= [g]
Rest= ["Recovery Day"]

#Workout Routine Organizer
w = [['Monday:',exercise1], ['Tuesday:',Rest], ['Wednesday:',exercise2], ['Thursday:',Rest], ['Friday:',exercise3], ['Saturday:',exercise4], ['Sunday:',Rest]]

y=0
for list in w:
    print(w[y][0])
    #Join whole list w[y][1] as a string, then print it
    print(", ".join(w[y][1]))
    print("\n")
    y+=1

输出将为

Monday:
Incline Dumbbell Press, Single-Arm Smith Machine Row, Seated Overhead Barbell Press, Hack Squat, Preacher Curl, Skullcrushers


Tuesday:
Recovery Day


Wednesday:
Hack Squat, Skullcrushers, Preacher Curl, Incline Dumbbell Press, Single-Arm Smith Machine Row, Seated Overhead Barbell Press


Thursday:
Recovery Day


Friday:
Seated Overhead Barbell Press, Single-Arm Smith Machine Row, Incline Dumbbell Press, Hack Squat, Skullcrushers, Preacher Curl


Saturday:
jumping jacks


Sunday:
Recovery Day