Python:类型错误:列表索引必须是整数,而不是列表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30890707/
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 09:07:51  来源:igfitidea点击:

Python: TypeError: list indices must be integers, not list

pythonlistloopsindices

提问by kotopanda

I've got two lists of coordinates, they look like this:

我有两个坐标列表,它们看起来像这样:

list_kp2_ok:

list_kp2_ok:

[[1185.60009765625, 933.6000366210938], [1310.4000244140625, 828.0000610351562], [1067.0, 979.0], [1310.0, 828.0], [1423.2000732421875, 814.800048828125], [1306.0, 828.0], [3634.0, 605.0], [1308.0960693359375, 827.7120971679688], [1422.7200927734375, 815.0400390625], [1185.1199951171875, 933.1200561523438], [1186.56005859375, 923.0400390625], [1306.3681640625, 829.4401245117188], [1194.393798828125, 839.80810546875], [1187.1361083984375, 922.7520751953125], [1082.8800048828125, 849.6000366210938]]

list_kp2_2_ok:

list_kp2_2_ok:

[[835.0, 1201.0], [1086.0, 850.0], [1187.0, 924.0], [1197.0, 839.0], [1310.0, 828.0], [3634.0, 605.0], [1195.2000732421875, 838.800048828125], [1308.0, 828.0000610351562], [1084.800048828125, 849.6000366210938], [1310.4000244140625, 828.0000610351562], [1186.800048828125, 924.0000610351562], [1296.0, 956.4000244140625], [1082.8800048828125, 849.6000366210938], [1072.800048828125, 944.6400146484375], [1083.4560546875, 850.1760864257812], [1187.1361083984375, 922.7520751953125], [3633.984375, 606.528076171875], [1082.4193115234375, 850.1761474609375], [1306.3681640625, 829.4401245117188], [1181.9521484375, 966.2977294921875], [1306.3682861328125, 828.6107788085938]]

Now I need to check if there are any same coordinates on both lists and create a new list of them. So I wrote:

现在我需要检查两个列表上是否有任何相同的坐标并创建一个新的列表。所以我写道:

list_wsp=[]
count=0
count1=0
print type(count)
print type(count1)
for count in list_kp2_ok:
    for count1 in list_kp2_2_ok:
        if list_kp2_ok[count]==list_kp2_2_ok[count1]:
            list_wsp.append(list_kp2_ok[count])
            count1=count1+1
            if count1==len(list_kp2_2_ok)-1:
                break
        count=count+1
        if count==len(list_kp2_ok)-1:
            break

and...

和...

TypeError: list indices must be integers, not list

I don't know what's wrong, couldn't find a solution...

我不知道怎么了,找不到解决方案...

Could anyone help me, please?

请问有人可以帮我吗?

Maybe there's a simplier way to do such a thing?

也许有更简单的方法来做这样的事情?

采纳答案by Iron Fist

You are indexing your lists with a non-int type index:

您正在使用非 int 类型索引为您的列表编制索引:

for count in list_kp2_ok:
    for count1 in list_kp2_2_ok:
        if list_kp2_ok[count]==list_kp2_2_ok[count1]:

So a quick fix for that is to do it this way:

因此,对此的快速解决方法是这样做:

for coord1 in list_kp2_ok:
    for coord2 in list_kp2_2_ok:
        if coord1==coord2:

You can even do the whole coding in one statement:

您甚至可以在一个语句中完成整个编码:

list_wsp=[coords for coords in list_kp2_ok if coords in list_kp2_2_ok]

This will directly output to you the common coordinates in both lists.

这将直接向您输出两个列表中的公共坐标。

回答by cezar

You don't have to declare the counter variables. You can iterate through the lists with for-in:

您不必声明计数器变量。您可以使用 for-in 遍历列表:

list_wsp = []
for elem in list_k2_ok:
    for elem1 in list_k2_2_ok:
        if elem == elem1:
            list_wsp.append(elem)

This will create the new list with the same coordinates.

这将创建具有相同坐标的新列表。

回答by bruno desthuilliers

Python's forloop is not indexed-based, it really iterates on the sequence (or any iterable). So in this code:

Python 的for循环不是基于索引的,它实际上是对序列(或任何可迭代的)进行迭代。所以在这段代码中:

for whatever in some_iterable:
    do_something_with(whatever)

whateveris successively bound to each item in some_iterable. As an example:

whatever依次绑定到 中的每个项目some_iterable。举个例子:

>>> mylist = ["A", "B", "C"]
>>> for item in mylist:
...     print "item is", item
... 
item is A
item is B
item is C

If you want the indexes to, you can use the builtin enumerate(iterable, start=0)function, which yields a (index, item)tuple for each item in iterable:

如果你想要索引,你可以使用内置enumerate(iterable, start=0)函数,它(index, item)为 中的每个项目生成一个元组iterable

>>> for index, item in enumerate(mylist):
...     print "item %s is %s" % (index, item)
... 
item 0 is A
item 1 is B
item 2 is C

回答by Joe T. Boka

You can use list comprehension:

您可以使用列表理解:

new_list = [i for i in list_kp2_ok if i in list_kp2_2_ok]

回答by Martin Evans

An alternative approach might be to try using sets:

另一种方法可能是尝试使用集合:

for x in set([tuple(l) for l in list_kp2_ok]).intersection(set([tuple(l) for l in list_kp2_2_ok])):
    print x

This first converts the inner list items to tuples as they are then hashable by the set function. The answer is then the intersection of the two sets. This would remove any duplicates which may or may not be desirable.

这首先将内部列表项转换为元组,因为它们然后可以被 set 函数散列。答案是两个集合的交集。这将删除可能需要也可能不需要的任何重复项。