处理列表会出现错误“列表索引必须是整数或切片,而不是元组”

时间:2019-04-02 13:32:22

标签: python list tuples

此代码用于将字符串作为输入,然后产生一个字母频率出现的二维数组。因此,对于每次迭代,它都会增加数组中整数的值(例如HI将是[h][i] =+ 1)。

我的问题是错误消息

list indices must be integers or slices, not tuple

只要我运行以下代码,就会出现。我已经更改了数组的创建方式,因此现在将其创建为列表,当我进入调试模式时可以确认该列表(letterArray成为列表的列表。)

我将字母转换为ASCII的原因是,我可以将其与其他ASCII值进行比较以同时清除字符串。我打算稍后将字符串转换为小写。

我已尝试将letterArray转换为列表,尽管这不会改变任何内容,因为letterArray是列表。我在具有相同问题的2d阵列上找不到任何relavent信息。

inputString = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat'  #just a test string
inputStringArray = list(inputString)        #break into char arrray
letterArray = []
posY = 28
x=0

for i in range(26):             #create balnk list of 0s of size 26x26
    letterArray.append([])
    for j in range(26):
        letterArray[i].append(0)

for letter in inputStringArray:
    posX = ord(letter)-97                   #-97 so that a = 0, z = 25
    if posY is 28 and posX <= 26 and posX >= 0:                     #instantiate first instance of past letter
        posY = posX
        continue
    if posX <= 26 and posX >= 0:                #if the char is a letter
        x = letterArray[posY,posX]
        x +=1
        letterArray[posY,posX] = x             #increment the space in the array by 1
        posY = posX
    else:
        continue

确切的错误是:

Message=list indices must be integers or slices, not tuple
  Source=C:\Users\me\source\repos\InputRecorder\InputRecorder\Recording.py
StackTrace:
  File "C:\Users\me\source\repos\InputRecorder\InputRecorder\Recording.py", line 103, in KeysToArray
    x = letterArray[posY,posX]

有什么建议吗?

2 个答案:

答案 0 :(得分:2)

您制作了可通过以下语法访问的嵌套列表:

x = letterArray[posY][posX]

答案 1 :(得分:0)

我看到有一个可以解决您的错误消息的答案,但是我想提出一些建议来改善您的代码。

这里f1()是您的代码,f2()是我的改进建议;它具有更少的代码,在我看来更易于理解,并且运行时间更少。

def f1(inputString):
    inputStringArray = list(inputString)            # break into char arrray
    letterArray = []
    posY = 28

    for i in range(26):                             # create balnk list of 0s of size 26x26
        letterArray.append([])
        for j in range(26):
            letterArray[i].append(0)

    for letter in inputStringArray:
        posX = ord(letter)-97                       # -97 so that a = 0, z = 25
        if posY is 28 and posX <= 26 and posX >= 0: # instantiate first instance of past letter
            posY = posX
            continue

        if posX <= 26 and posX >= 0:                # if the char is a letter
            x = letterArray[posY][posX]
            x +=1
            letterArray[posY][posX] = x             # increment the space in the array by 1
            posY = posX
        else:
            continue

    return letterArray

def f2(input_str):
    max_n = 26

    # build nested lists with zeros
    letter_list = [
        [0, ] * max_n
        for _ in range(max_n)]

    # 'None' is usually used to indicate an invalid value
    prev_i = None

    for letter in input_str:
        curr_i = ord(letter) - 97

        if 0 <= curr_i <= max_n:
            if prev_i is not None:
                letter_list[prev_i][curr_i] += 1

            prev_i = curr_i

    return letter_list

断言结果是相同的:

>>> s = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat'
>>> r1 = f1(s)
>>> r2 = f2(s)
>>> r1 == r2
True

比较运行时(此示例输入的时间为f2的一半,f1

>>> import timeit
>>> timeit.timeit('f1(s)', 'from __main__ import f1, s', number=10000)
1.0122240540222265
>>> timeit.timeit('f2(s)', 'from __main__ import f2, s', number=10000)
0.4270052219508216