一段非常基本的代码`切片索引必须是整数或无,或者有一个索引方法`

2024-05-29 05:39:56 发布

您现在位置:Python中文网/ 问答频道 /正文

我被这个超级简单的代码惊呆了:

#!/usr/bin/python2.7
rp1 = ['id', 'key1', 'key2', 'key3', 'key4', 'key5']
rp2 = ['id', 'key1', 'key2']

rp1_keys = list(set(rp1).difference(set(rp2)))

print("%s, Type: %s/n" % (rp1_keys, type(rp1_keys)))

if 'id' not in rp1_keys:
    rp1_keys.index(0,'id')

这是出口:

===>  ['key3', 'key5', 'key4'] type : <type 'list'>

Traceback (most recent call last):
  File "/tmp/py.py", line 11, in <module>
    rp1_keys.index(0,'id')
TypeError: slice indices must be integers or None or have an __index__ method

Tags: inpyidindextypekeyslistkey2

热门问题