Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/328.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
python错误:";索引错误:字符串索引超出范围;_Python_Python 2.7 - Fatal编程技术网

python错误:";索引错误:字符串索引超出范围;

python错误:";索引错误:字符串索引超出范围;,python,python-2.7,Python,Python 2.7,我的代码有问题。试图从数组中查找progId元素中的索引时出错 当我尝试这个: for index in range(0, self.channel_count): test = progId[index] test_index = index print test 错误正在该行上跳转: test = progId[index] 错误为:索引器错误:字符串索引超出范围 以下是完整的代码: self.channel_count = 0 if start_time &l

我的代码有问题。试图从数组中查找
progId
元素中的索引时出错

当我尝试这个:

for index in range(0, self.channel_count):
    test = progId[index]
    test_index = index
    print test
错误正在该行上跳转:

test = progId[index]
错误为:索引器错误:字符串索引超出范围

以下是完整的代码:

self.channel_count = 0

if start_time < current_time < stop_time:
    print "program is half way"
    progId = list()

    for index in range(0, self.channel_count):
        test = progId[index]
        test_index = index
        print test_index
self.channel_count += 1
以下是
progId
列表中的元素列表:

19:16:40 T:2112  NOTICE: 3003
19:16:40 T:2112  NOTICE: 3131
19:16:40 T:2112  NOTICE: 3259
19:16:40 T:2112  NOTICE: 3387
19:16:40 T:2112  NOTICE: 3515
19:16:40 T:2112  NOTICE: 3643
19:16:40 T:2112  NOTICE: 3771
19:16:40 T:2112  NOTICE: 3003
19:16:40 T:2112  NOTICE: 3131
19:16:40 T:2112  NOTICE: 3259
19:16:40 T:2112  NOTICE: 3387
19:16:40 T:2112  NOTICE: 3515
19:16:40 T:2112  NOTICE: 3643
19:16:40 T:2112  NOTICE: 3771
你能告诉我如何修正这个错误吗

编辑:当我尝试此操作时:

program_index = str(self.program_index)

for index in program_index:
    print index
我通过使用
program\u index
将其作为值:

19:51:01 T:2888  NOTICE: 1
19:51:01 T:2888  NOTICE: 2
19:51:01 T:2888  NOTICE: 3
19:51:01 T:2888  NOTICE: 4
以下是
progId
列表中的元素列表:

19:16:40 T:2112  NOTICE: 3003
19:16:40 T:2112  NOTICE: 3131
19:16:40 T:2112  NOTICE: 3259
19:16:40 T:2112  NOTICE: 3387
19:16:40 T:2112  NOTICE: 3515
19:16:40 T:2112  NOTICE: 3643
19:16:40 T:2112  NOTICE: 3771
19:16:40 T:2112  NOTICE: 3003
19:16:40 T:2112  NOTICE: 3131
19:16:40 T:2112  NOTICE: 3259
19:16:40 T:2112  NOTICE: 3387
19:16:40 T:2112  NOTICE: 3515
19:16:40 T:2112  NOTICE: 3643
19:16:40 T:2112  NOTICE: 3771
我想得到这样的结果:

19:16:40 T:2112  NOTICE: 3131
19:16:40 T:2112  NOTICE: 3259
19:16:40 T:2112  NOTICE: 3387
19:16:40 T:2112  NOTICE: 3515

那么如何使用索引从数组中查找元素呢?

只要将
范围(0,self.channel\u count)
更改为
范围(len(progId))
。尚不清楚变量
self.channel\u count
progId
包含哪些内容。

在for循环的第一次运行中,
self.channel\u count
为0。因此,
range(0,self.channel\u count)
将是一个空列表,您将得到“索引超出范围”。

当我在此网站上搜索确切的错误消息时,有将近2000个结果。您看过其中任何一个了吗?非常感谢,变量
self.channel\u count
用于从
0
7
的值列表,而
progId
contain用于程序ID列表。您知道如何使用
progId
的索引从数组中搜索元素吗?