尝试从python中的列表列表中调用索引时出错

时间:2017-09-10 07:49:27

标签: python list indexing

我有一个巨大的列表,我正在尝试创建一个将打印data_sets [51]的函数,这是:

['X', ['Sheet A', 'Location 1', 'Upright'],
          ['Sheet B', 'Location 2', 'Upright'],
          ['Sheet C', 'Location 3', 'Upright'],
          ['Sheet D', 'Location 4', 'Upright']],

此行如果第一批列表中的第51个列表

这是我的代码,其上面有绘制工作表的所有功能

def paste_up(position):

    for position in data_sets:
        #start location
        if position[1] == "Location 1":
            start_position = (-300, 0)
        elif position[1] == "Location 2":
            start_position = (-100, 0)
        elif position[1] == "Location 3":
            start_position = (100, 0)
        elif position[1] == "Location 4":
            start_position = (300, 0)

        #start direction
        if position[2] == "Upright":
            start_direction = (90)
        elif position[2] == "Upside down":
            start_direction = (270)

        #draw sheet
        if position[0] == "Sheet A":
            draw_sheetA(start_position, start_direction)
        if position[0] == "Sheet B":
            draw_sheetB(start_position, start_direction)
        if position[0] == "Sheet C":
            draw_sheetC(start_position, start_direction)
        if position[0] == "Sheet D":
            draw_sheetD(start_position, start_direction)

paste_up(data_sets[51])

但是我收到了这个错误,我不知道为什么,我试图改变索引或使用[51] [1],但它没有改变结果

Traceback (most recent call last):
  File "/Users/chalysefoster/Documents/2017/QUT Semester 2/IFB104 Building IT Systems/Assigment 1/billboard.py", line 911, in <module>
    paste_up(data_sets[51])
  File "/Users/chalysefoster/Documents/2017/QUT Semester 2/IFB104 Building IT Systems/Assigment 1/billboard.py", line 883, in paste_up
    if position[1] == "Location 1":
IndexError: list index out of range

三江源

1 个答案:

答案 0 :(得分:0)

据我所知 - 你收到了IndexError,因为提供列表中的第一个元素只包含一个索引为0的元素(obviosuly),所以当你尝试访问el [1]时 - 引发了IndexError。

>>>for i, element in enumerate(data_sets):
    print(i, element)

0 X
1 ['Sheet A', 'Location 1', 'Upright']
2 ['Sheet B', 'Location 2', 'Upright']
3 ['Sheet C', 'Location 3', 'Upright']
4 ['Sheet D', 'Location 4', 'Upright']

我认为明白为什么会得到IndexError。

只需在开始修改数据集后立即添加类似内容:

if len(item) != 3 and not isinstance(item, list):
    # continue or raise ValueError ( for example )
    continue
BTW,使用词典比多个if-elif语句更清晰:

def paste_up(data):

    sheets = {
        "Sheet A": draw_sheetA,
        "Sheet B": draw_sheetB,
        "Sheet C": draw_sheetC,
        "Sheet D": draw_sheetD
    }

    locations = {
        "Location 1": (-300, 0),
        "Location 2": (-100, 0),
        "Location 3": (100, 0),
        "Location 4": (300, 0)
    }

    directions = {
        "Upright": (90),
        "Upside down": (270)
    }

    for item in data:

        if len(item) != 3 and not isinstance(item, list):
            # continue or raise ValueError ( for example )
            continue

        # retrieve data
        sheet, location, direction = item

        # get link to fucntion from dictionary
        draw_action = sheets[sheet]

        # get start position from dictionary
        start_position = locations[location]

        # get start direction from dictionary
        start_direction = directions[direction]

        # exec function with found args
        draw_action(start_position, start_direction)