Python 类型错误:尝试更改数组列表的某些元素时,列表索引必须是整数,而不是元组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33925698/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 14:12:34  来源:igfitidea点击:

TypeError: list indices must be integers, not tuple when trying to alter certain elements of a list of arrays

pythonarrayslisttypeerror

提问by Orange Pukeko

I have a list of 2 by n arrays of x and y coordinates.

我有一个由 x 和 y 坐标组成的 2 x n 数组的列表。

old: [array([[1, 2, 3], [4, 5, 6]]), array([[10, 20, 30], [40, 50, 60]])]

I am trying to shift the y-coordinates, the second row of each array, by a certain value 'shift'. However, when I try to do this by the method below, I get the an error:

我试图将每个数组的第二行 y 坐标移动某个值“移位”。但是,当我尝试通过以下方法执行此操作时,出现错误:

"TypeError: list indices must be integers, not tuple when trying to alter certain elements of a list of arrays."

“类型错误:在尝试更改数组列表的某些元素时,列表索引必须是整数,而不是元组。”

import pylab


    def shiftY(old,shift):
        new = list([])

        for i in arange(len(old)):
            y = old[i][1,:] + shift
            newItem = array([old[:,0],y])
            new.append(newItem)

        return new

    old = list()
    old.append(arr

ay([[1, 2, 3], [4, 5, 6]]))
old.append(array([[10,20,30],[40,50,60]]))
shift =3 
new=shiftY(old,shift)
print(new)

Traceback:

追溯:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Python27_32bit\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 540, in runfile
    execfile(filename, namespace)
  File "C:/Users/tald574/testShifty.py", line 25, in <module>
    new=shiftY(old,shift)
  File "C:/Users/tald574/testShifty.py", line 15, in shiftY
    newItem = array([old[:,0],y])
TypeError: list indices must be integers, not tuple

I can't see what I am doing wrong as newItemis not even supposed to be a list, it should be a 2D array. Would appreciate it if someone can tell me what I do wrong an how to fix it.

我看不出我做错了什么,因为newItem它甚至不应该是一个列表,它应该是一个二维数组。如果有人能告诉我我做错了什么以及如何解决它,我将不胜感激。

Thanks.

谢谢。

Edit: The expected outcome of this test would be,

编辑:这个测试的预期结果是,

new:[array([[1, 2, 3], [7, 8, 9]]), array([[10, 20, 30], [43, 53, 63]])]

采纳答案by 6c1

You don't need the commas in the list slicing notation. array[:,i]is parsed as array[:t], where t = ,iis a tuple. See herefor a rundown of list slicing.

您不需要列表切片符号中的逗号。array[:,i]被解析为array[:t], wheret = ,i是一个元组。有关列表切片的概要,请参见此处

Tuples are defined by commas, not parentheses.

元组由逗号而不是括号定义

In your case, replace the lines

在您的情况下,替换行

y = old[i][1,:] + shift
newItem = array([old[:,0],y])

with

y = old[i][1:] + shift
newItem = array([old[:0],y])

回答by Orange Pukeko

Right, I feel stupid now.

没错,我现在觉得自己很傻。

I have been working on this for way too long and off course I figure out the answer within 15 minutes of posting this question.

我一直在研究这个问题太久了,当然我在发布这个问题的 15 分钟内就找到了答案。

I made two mistakes in the assignment of 'newitem'. instead of

我在分配“newitem”时犯了两个错误。代替

newItem = array([old[:,0],y])

I should have used:

我应该使用:

newItem = array([old[i][0,:],y])

That is, I need to access the appropriate item in the list, old[i] rather than old. And I need to access the appropriate item in the array old[i][0,:] rather than old[i][:,0]. Rookie mistake, I hope this helps someone else.

也就是说,我需要访问列表中的适当项目,old[i] 而不是 old。我需要访问数组 old[i][0,:] 而不是 old[i][:,0] 中的适当项目。菜鸟错误,我希望这对其他人有所帮助。

Edit, oh wow, only now see all the comments and answers on my question, thanks guys, I took so long because I incorporated this into my actual project, rather than just the test script. Cheers anyway.

编辑,哇哦,现在才看到我的问题的所有评论和答案,谢谢大家,我花了这么长时间,因为我将它合并到了我的实际项目中,而不仅仅是测试脚本。总之干杯。

回答by Neelu

It seems that ,is placed instead of :.

似乎,是放置而不是:.

Replace the comma with colon.

用冒号替换逗号。

Below is the correct syntax of the sublist creation from the list.

下面是从列表中创建子列表的正确语法。

list_name[start_index:end_index]