Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.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_List_For Loop - Fatal编程技术网

如何使用python根据选中项的顺序从另一个列表创建列表

如何使用python根据选中项的顺序从另一个列表创建列表,python,list,for-loop,Python,List,For Loop,如何根据用户选择示例从现有列表创建新列表: l1=[1,2,3,4,5] 如果用户先检查项目3,然后检查项目2 l2=[3,2] 直到现在,如果用户先检查项目3,然后检查项目2 l2=[2,3] 我的问题是如何基于选中项创建列表 代码: 假设项目是复选框,则可以使用列表检查项目是否已添加,然后根据需要添加。这将为您提供选择顺序 checked = [] def selectionChanged(self): global checked for row in range(self

如何根据用户选择示例从现有列表创建新列表:

l1=[1,2,3,4,5]

如果用户先检查项目3,然后检查项目2 l2=[3,2]

直到现在,如果用户先检查项目3,然后检查项目2 l2=[2,3]

我的问题是如何基于选中项创建列表

代码:
假设项目是复选框,则可以使用列表检查项目是否已添加,然后根据需要添加。这将为您提供选择顺序

checked = []
def selectionChanged(self):
    global checked
    for row in range(self.header_list.count()):
        item = self.header_list.item(row)
        if item.checkState():
            if not item in checked: checked.append(item) # add item if not already added
        else:
            if item in checked: del checked[item] # remove unchecked items
    print("Checked items: ", ", ".join(i.text() for i in checked))
    self.checked = [i.text() for i in checked]

否项目是qwidgetItem,我尝试了您的答案,但它没有回答我的问题…我需要的是将用户选中的项目附加到选中的[]列表中,而不是列表中最初存在的项目。
checked = []
def selectionChanged(self):
    global checked
    for row in range(self.header_list.count()):
        item = self.header_list.item(row)
        if item.checkState():
            if not item in checked: checked.append(item) # add item if not already added
        else:
            if item in checked: del checked[item] # remove unchecked items
    print("Checked items: ", ", ".join(i.text() for i in checked))
    self.checked = [i.text() for i in checked]