对列表排序后列表索引超出范围?

2024-04-26 14:57:32 发布

您现在位置:Python中文网/ 问答频道 /正文

def Routine_Maker(number_of_exercises,array):
    routine = []
    i=0
    j=0
    for i in range(number_of_exercises):
        for j in range(2):                
            x = random.randint(0,4)
            movement = array[i][x]
            routine.append(movement)
            j=j+1
        i=i+1
        print (routine)

我的代码搜索存储在名为array的二维数组中的练习列表,并将它们写入名为routine的数组中

它搜索整个文件并添加所有的训练,但是我在最后得到以下消息。你知道吗

line 20, in Routine_Maker
    movement = array[i][x]
IndexError: list index out of range

我已经尝试过做for i in range(number_of_exercises-1)以使它与索引保持一致,但是这不起作用。有什么想法吗?你知道吗

这就是array的样子:

[['Bench Press', 'Dumbell Press', 'Chest Press Machine', 'Cable Flies', 'Cable Crossover'], ['Incline Bench Press', 'Incline Dumbell Press', 'Incline Cable Flies', 'Incline Chest Press Machine', 'Incline Dumbell Flies'], ['Barbell Overhead Press', 'Dumbell Overhead Press', 'Dumbell Lateral Raise', 'Face Pulls', 'Dumbell Front Raise'], ['Dumbell Curls ', 'Hammer Curls', 'Preacher Curls', 'Machine Curls', 'Close Grip Chin Ups'], ['Cable Pushdowns', 'Overhead Dumbell Extensions', 'Overhead Barbell Extensions', 'Dips', 'Skullcrushers'], ['Lat Pulldowns', 'Pullups', 'Deadlifts', 'Bent Over Rows', 'Dumbell Rows'], ['Squats', 'Leg Press', 'Calf Raisers', 'Leg Extensions', 'Leg Curls']]

我试着从名为array的数组中的每个数组中随机选择2个练习。例如,第一个数组包括胸部练习。下一个阵法包括上胸部练习,然后肩部练习等。我想从每个阵法/肌肉群中随机选择2个。你知道吗


Tags: ofinnumberforrange数组arraypress
3条回答

for循环已经在每次迭代之后将1添加到变量“i”和“j”中。所以,不是从0到你的极限-1,而是从1到你的极限。只需删除j = j + 1i = i + 1行:)

编辑时间:

def Routine_Maker(number_of_exercises, array):
    routine = []
    for i in range(number_of_exercises):
        x = random.randint(0, len(array) - 1) 
        y = random.randint(0, 4)
        movement = array[x][y]
        routine.append(movement)
    print (routine)

编辑#2:

这应该是你所说的(至少是我所理解的):)

   def Routine_Maker(number_of_exercises, array):
      routine = []
      pos = 0
      for i in range(number_of_exercises):
            if pos >= len(array):
                pos = 0
            for j in range(2):
                x = random.randint(0,4)
                movement = array[pos][x]
                routine.append(movement)
            pos += 1
      print(routine)

这将返回一个长度为2*number_of_exercises的数组。如果您希望它的长度正好是number_of_exercises,pos索引应该每2次迭代移动一次,只需删除for j语句:)

编辑#3:

好吧,我想我明白了:

首先,你有你的数据:

array_of_exercises = [['Bench Press', 'Dumbell Press', 'Chest Press Machine', 'Cable Flies', 'Cable Crossover'], 
                      ['Incline Bench Press', 'Incline Dumbell Press', 'Incline Cable Flies', 'Incline Chest Press Machine', 'Incline Dumbell Flies'], 
                      ['Barbell Overhead Press', 'Dumbell Overhead Press', 'Dumbell Lateral Raise', 'Face Pulls', 'Dumbell Front Raise'], 
                      ['Dumbell Curls ', 'Hammer Curls', 'Preacher Curls', 'Machine Curls', 'Close Grip Chin Ups'], 
                      ['Cable Pushdowns', 'Overhead Dumbell Extensions', 'Overhead Barbell Extensions', 'Dips', 'Skullcrushers'],
                      ['Lat Pulldowns', 'Pullups', 'Deadlifts', 'Bent Over Rows', 'Dumbell Rows'], 
                      ['Squats', 'Leg Press', 'Calf Raisers', 'Leg Extensions', 'Leg Curls']]

然后,你说你每天要做多少运动:

exercises_per_day = [2, 4, 3, 1, 3, 5, 3, 2, 3, 4]

这就是函数:

def Routine_Maker(number_of_days, array, exercises_per_day):
    routine = []
    pos = 0
    for i in range(number_of_days):
      if pos >= len(array):
        pos = 0
      temporal_array = [array[pos][j] for j in random.sample(range(5), exercises_per_day[pos])]
      routine.append(temporal_array)
      pos += 1
    print(routine)

所以你这样称呼它:

Routine_Maker(10, array_of_exercises, exercises_per_day) 

你会得到这样的结果:

[['Cable Flies', 'Cable Crossover'], 
 ['Incline Dumbell Flies', 'Incline Chest Press Machine', 'Incline Dumbell Press', 'Incline Cable Flies'], 
 ['Dumbell Overhead Press', 'Barbell Overhead Press', 'Dumbell Front Raise'], ['Dumbell Curls '], 
 ['Cable Pushdowns', 'Overhead Dumbell Extensions', 'Dips'], 
 ['Dumbell Rows', 'Bent Over Rows', 'Pullups', 'Lat Pulldowns', 'Deadlifts'], 
 ['Leg Press', 'Squats', 'Leg Curls'], 
 ['Dumbell Press', 'Cable Crossover'], 
 ['Incline Cable Flies', 'Incline Dumbell Flies', 'Incline Dumbell Press', 'Incline Chest Press Machine'], 
 ['Dumbell Lateral Raise', 'Face Pulls', 'Dumbell Front Raise']]

应该注意:

  • 如果你说你每天要做的运动比你阵列中的运动要多,那么这将失败,因为随机抽样你没有得到重复,但样本大小需要小于范围。如果你想要更多,你会有重复的,你可以通过修改randomInt来完成。

  • 当天数大于你的练习数组的大小时,它就会重新开始(这就是pos的用途)。

你不需要排队

i=0
j=0
j=j+1
i=i+1

当您使用for in循环时。你可以移除它们。你知道吗

您可能会得到一个索引器,因为number_of_exercises大于len(array)。你知道吗

My guesses based on your edits

#!/bin/python

array=[['Bench Press', 'Dumbell Press', 'Chest Press Machine', 'Cable Flies', 'Cable Crossover'], ['Incline Bench Press', 'Incline Dumbell Press', 'Incline Cable Flies', 'Incline Chest Press Machine', 'Incline Dumbell Flies'], ['Barbell Overhead Press', 'Dumbell Overhead Press', 'Dumbell Lateral Raise', 'Face Pulls', 'Dumbell Front Raise'], ['Dumbell Curls ', 'Hammer Curls', 'Preacher Curls', 'Machine Curls', 'Close Grip Chin Ups'], ['Cable Pushdowns', 'Overhead Dumbell Extensions', 'Overhead Barbell Extensions', 'Dips', 'Skullcrushers'], ['Lat Pulldowns', 'Pullups', 'Deadlifts', 'Bent Over Rows', 'Dumbell Rows'], ['Squats', 'Leg Press', 'Calf Raisers', 'Leg Extensions', 'Leg Curls']]

import random
def Routine_Maker(array):
    routine = []
    for i in array:
        routine.extend(random.sample(i,2))
    return routine

print(Routine_Maker(array))
# => ['Cable Crossover', 'Bench Press', 'Incline Dumbell Press', 'Incline Dumbell Flies', 'Face Pulls', 'Dumbell Front Raise', 'Preacher Curls', 'Machine Curls', 'Skullcrushers', 'Overhead Dumbell Extensions', 'Deadlifts', 'Pullups', 'Calf Raisers', 'Leg Extensions']

number_of_exercises大于len(array)*2时,您希望它做什么?你知道吗

您的问题可能是number_of_exercises大于len(array)。要解决这个问题,您需要验证输入,然后根据验证的结果做一些事情。例如

number_of_exercises = min(number_of_exercises, len(array))

我在这里做的是将number_of_exercises设置为array的长度,如果它大于len(array),这将使索引错误变得不可能。或者,您可以打印一些警告/错误消息,通知用户他们搞砸了,或者同时执行这两个操作。例如,可以通过以下方式进行验证:

number_of_exercises = int(input("How many? "))
while ((number_of_exercises < 0) or (number_of_exercises > len(array)):
    print ("Invalid input.")
    try:
        number_of_exercises = int(input(Please provide valid input! "))
    catch ValueError:
        number_of_exercises = -1

此代码禁止用户在提供有效输入之前继续前进。或者,您可以显示错误消息并退出程序:

import sys

number_of_exercises = int(input("How many? "))
if (number_of_exercises < 0 or number_of_exercises > len(array)):
    print ("Invalid data provided. This program will now exit")
    sys.exit()

相关问题 更多 >