Python:TypeError:列表索引必须是整数,而不是列表 [英] Python: TypeError: list indices must be integers, not list

查看:79
本文介绍了Python:TypeError:列表索引必须是整数,而不是列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

和...

TypeError: list indices must be integers, not list

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

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

请问有人可以帮我吗?

也许做这种事情的方法比较简单?

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

推荐答案

您正在使用非整数类型索引为列表建立索引:

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

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.

这篇关于Python:TypeError:列表索引必须是整数,而不是列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆