追加到列表python时出现“索引错误-列表索引超出范围”错误

时间:2019-01-17 10:00:05

标签: python list

使用list.append函数的索引超出范围错误

我尝试添加一个额外的索引以查看是否存在问题-

cardinplay = [[数据],[]] cardinplay.pop(1)


def card_Maker(n):
    data = [line.strip() for line in open("dogs (1).txt", 'r')]
    i = randint(0, len(data))
    cardinplay = [[data[i], randint(1, 5), randint(1, 100), randint(1, 100), randint(1, 10)],[]] 
    cardinplay.pop(1)


    for f in range(1, n):
        i = randint(0, len(data))
        cardinplay.append(list([data[i], randint(1, 5), randint(1, 100), randint(1, 100), randint(1, 10)])) #where the problem is 

    repeat = True
    while repeat:
        for n in range(0,n):
            for m in range(0, n):
                if cardinplay[n] == cardinplay[m]:
                    cardinplay.pop(n)
                    i = randint(0, len(data))
                    cardinplay.append([data[i], randint(1, 5), randint(1, 100), randint(1, 100), randint(1, 10)])
                else:
                    repeat = False
    return cardinplay

预期结果是唯一列表的列表,例如[[data1],[data2] ... [dataN]]

0 个答案:

没有答案