简体   繁体   English

如何修复 python 中超出范围的列表索引

[英]how to fix list index out of range in python

i have a function that append item to a list then i want to print the each using .format()我有一个 function 那个 append 项目到列表然后我想打印每个使用.format()

If i try to print the self.checked[0] it will print the selected item but if it try self.checked[0],self.checked[1] system crash.如果我尝试打印self.checked[0] ,它将打印所选项目,但如果它尝试self.checked[0],self.checked[1]系统崩溃。

the problem is when i am trying to print each item the system display this error:问题是当我尝试打印每个项目时系统显示此错误:

 self.display_event_mouhafaza.setText(" {} over {}".format(self.checked[0],self.checked[1]))
IndexError: list index out of range

where is the error in my code?我的代码中的错误在哪里?

def selectionChanged(self):
        checked = []
        for row in range(self.header_list.count()):
            item = self.header_list.item(row)
            if item.checkState():
                checked.append(item)
        print("Checked items: ", ", ".join(i.text() for i in checked))
        self.checked = [i.text() for i in checked]
        self.display_event_person.setText("total of {} ".format(self.checked[0]))
        self.display_event_mouhafaza.setText(" {} over {}".format(self.checked[0],self.checked[1]))

#part that display items in the qlistWidget
        self.header_list.clear()
        savelist = list(self.df)
        for item in savelist:
            qitem = QtWidgets.QListWidgetItem ( ) 
            qitem.setText ( item ) 
            qitem.setCheckState ( QtCore.Qt.Unchecked ) 
            self.header_list.addItem ( qitem )

here the self.checked includes 3 items.这里的self.checked包括 3 个项目。 在此处输入图像描述

self.display_event_mouhafaza.setText(" {} over {}".format(self.checked[0],self.checked[1]))

To get rid of index out of range, you would need a default value, let print empty space "" if index out of range要摆脱索引超出范围,您需要一个默认值,如果索引超出范围,请打印空格“”

if len(self.checked) < 2 :
    print( 'This is your self.checked does not have second value ', self.checked)
else:
    self.display_event_mouhafaza.setText(" {} over {}".format(
             self.checked[0]
             self.checked[1]
    ))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何修复python中的“IndexError:列表索引超出范围”? - How to fix 'IndexError: list index out of range' in python? 如何修复 Python 中的“IndexError: list index out of range” - How do you fix 'IndexError: list index out of range' in Python 如何在Python中修复动态列表索引超出范围 - How to fix dynamic list index out of range in Python 如何在Python中修复“ IndexError:列表索引超出范围” - How to fix “IndexError: list index out of range” in python 如何修复 python 中的字典 get() 方法“列表索引超出范围” - How to fix Dictionary get() Method in python 'list index out of range' 在python中如何修复列表索引超出范围的问题? - In python how can i fix list index out of range problem? 如何修复Python中的“ IndexError:列表分配索引超出范围”错误? - How to fix “IndexError: list assignment index out of range” error in Python? 如何修复 Python 上的“IndexError: list index out of range” - How to fix "IndexError: list index out of range" on Python 如何使用 CNN 代码修复 Python “IndexError: list index out of range” - How to fix Python “IndexError: list index out of range” with CNN code 如何修复Python中的“列表索引超出范围”错误? - How to fix 'list index out of range' error in python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM