【发布时间】:2020-04-09 09:36:20
【问题描述】:
我正在尝试从两个不同的字典中选择一个表,但是在下面给出的 for 循环之后,我收到一个错误“IndexError: list index out of range”。
我的字典;
>>> file_dict
{'NODES': ['BSHTAS1', 'GB-vMTAS', 'GBHTAS1', 'GBMTAS1', 'GBZLRF', 'MBHTAS01', 'MNDHTAS', 'SGT-vMTAS', 'SOGZLRF'],
'KPIS': [['VoLTE MO Voice Connection Rate(%)', 'OK', 'OK', 'NOK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK'],
['VoLTE MO Voice Answer Rate(%)', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK'],
['VoLTE MO Voice Call Drop Times(times)', 'NOK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK'],
['VoLTE MO Video Connection Rate(%)', 'OK', 'OK', 'OK', 'OK', 'OK', 'NOK', 'NOK', 'OK', 'OK'],
['VoLTE MO Video Answer Rate(%)', 'OK', 'OK', 'NOK', 'OK', 'OK', 'NOK', 'NOK', 'OK', 'OK'],
['VoLTE MO Video Call Drop Times(times)', 'NOK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK'],
['VoLTE MT Voice Connection Rate(%)', 'NOK', 'OK', 'NOK', 'OK', 'OK', 'NOK', 'NOK', 'OK', 'OK'],
['VoLTE MT Voice Answer Rate(%)', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'NOK', 'OK', 'OK'],
['VoLTE MT Voice Call Drop Times(times)', 'OK', 'OK', 'OK', 'OK', 'OK', 'NOK', 'OK', 'OK', 'OK'],
['VoLTE MT Video Connection Rate(%)', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK'],
['VoLTE MT Video Answer Rate(%)', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK'],
['VoLTE MT Video Call Drop Times(times)', 'OK', 'OK', 'OK', 'OK', 'OK', 'NOK', 'OK', 'OK', 'OK'],
['VoLTE MO Request Times(times)', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK'],
['VoLTE MT Request Times(times)', 'OK', 'NOK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK', 'OK']
]}
我想让标题成为下面的键;
>>> file_dict["KPIS"][0][0]
'VoLTE MO Voice Connection Rate(%)'
这是我的循环。你能告诉我我的错误在哪里吗?非常感谢..
rows = []
headers = ["Kpis"]
for filename in file_list:
file_content = open('{}'.format(filename), 'r').read()
file_string = json.loads(file_content)
# convert string to dictionary
file_dict = eval(file_string)
if "KPIS" in file_dict:
ind = 0
for eachkpi in file_dict["KPIS"]:
t = [eachkpi]
for eachnode in file_dict["KPIS"]:
if eachnode[0][0] not in headers:
headers.append(eachnode[0][0])
t.append(eachnode[ind + 1])
rows.append(t)
ind = ind + 1
错误信息;
Traceback (most recent call last):
File "<stdin>", line 13, in <module>
IndexError: list index out of range
>>>
【问题讨论】:
-
请使用完整的错误回溯更新您的问题。
-
哦,不。full 错误回溯会列出产生错误的代码行的文本。
-
在使用它访问某些东西之前打印索引。您将了解问题
-
我认为这里
t.append(eachnode[ind + 1])的索引最终会大于len(eachnode)。 -
如何在这里控制索引大小?
标签: python loops dictionary transpose