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
的索引从数组中搜索元素吗?