Python:TypeError:list indices必须是整数,而不是list

时间:2015-06-17 11:48:44

标签: python list loops indices

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

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:

[[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]]

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

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

我不知道出了什么问题,找不到解决办法......

有人可以帮助我吗?

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

5 个答案:

答案 0 :(得分:3)

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

for whatever in some_iterable:
    do_something_with(whatever)

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

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

如果您想要索引,可以使用内置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

答案 1 :(得分:3)

您使用非int类型索引索引列表:

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

所以快速解决这个问题就是这样做:

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

您甚至可以在一个声明中完成整个编码:

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

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

答案 2 :(得分:1)

您可以使用列表理解:

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

答案 3 :(得分:0)

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

<script>
function validateForm() {


for (var x=0; x<82; x++) {
    for (var y=x+1; y<83; y++) {

      if (document.forms["myForm"]['pk'+x].value==document.forms["myForm"]['pk'+y].value) {
        alert('There was a match');
        return false;
      }


    }


  }


alert('No matches');
  return true;
}



</script>

<form action="test.php" name="myForm" method="get" onsubmit="return validateForm()">

<?php 


for ($i=0;  $i<=81; $i++)

{

?>

<input type="text" name='pk<?php echo $i; ?>' value='<?php echo $i; ?>'>



<?php





}

?>
<input type='submit' name='submit'>
</form>

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

答案 4 :(得分:0)

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

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

这首先将内部列表项转换为元组,因为它们可以通过set函数进行处理。答案就是两组的交集。这将删除任何可能需要或可能不需要的重复。