如何在 Python 中随机从一个列表中提取,并将其放入另一个列表中?

2024-06-02 06:36:21 发布

您现在位置: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

Tags: lenrandommachinecurlpullrowgrippress
2条回答

在我看来,你的意思可能是:

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

现在的代码是将字符串列表添加到exercise1到5,而不是存储在这些变量中的实际列表。你知道吗

一些错误和对代码的建议

  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



相关问题 更多 >