从压缩列表中查找元素时的问题将自动清空压缩列表

Issue when finding element from a zipped list will automatic empty the zipped list

我正在尝试从 QTlistwidget 中搜索键入的项目,当 selected 时,它将再次搜索以查看是否存在于压缩列表中。

但是,第一次 运行 它将能够找到匹配项。第二次我 select,zip_cmsdata 将 return 我一个空列表 例如: 第一次搜索“Hello”,然后从 Qtlistwdiget selected“Hello” 输出:

item click
[('Hello', 1, 'US', None)]

如果我再次从QTlistwdiget 中单击(select) 你好,或搜索其他项目。我将得到以下输出

item click
[]

我猜 cmsdata 只添加了一次,所以第二次被清除了。如果是这样,我该如何解决?

请在下面找到我的代码:

from asyncio.windows_events import NULL
from re import A
import sys
from PyQt5.uic import loadUi
from PyQt5 import QtWidgets, QtCore
from PyQt5.QtWidgets import QDialog, QApplication, QStackedWidget, QWidget, QTabWidget, QTableView, QListView
from PyQt5.QtGui import QPixmap
from PyQt5.QtCore import QStringListModel
import requests
import images
import json

cmsdata = []
cmsdisplayname = []
cid = []
cmscountry = []
parentId = []
zip_cmsdata = []
class PerfectConsoleSearch(QDialog):
    def __init__(self):
        super(PerfectConsoleSearch, self).__init__()
        loadUi("pfsearch.ui",self)
        with open('cmsdata.json') as json_file:
            data = json.load(json_file)
            global cmsdata
            cmsdata = data
            #print(data)
        #print(cmsdata)
        #print(type(cmsdata))
        #self.login_button.clicked.connect(self.gotologin)
        #self.cms_display_list.setStringList(self.cmsdata)
        #self.cms_display_list.setModel(main.cmsdata)
        self.load_cmslist()
        print(self.customer_searchbar.text())
        self.customer_searchbar.textChanged.connect(lambda: self.Search(self.customer_searchbar.text()))
        self.cms_display_list.itemClicked.connect(self.show_select_item)

    def load_cmslist(self):
        n = 0
        a=[]
        #print(cmsdata['customerList'][0]['displayName'])
        while n < len(cmsdata['customerList']):         
            #print(cmsdata['customerList'][n]['displayName'])
            cmsdisplay_name = cmsdata['customerList'][n]['displayName']
            cms_cid = cmsdata['customerList'][n]['cid']
            cms_country = cmsdata['customerList'][n]['country']
            cms_parentId = cmsdata['customerList'][n]['parentId']
            a.append(cmsdisplay_name)
            global cmsdisplayname, cid, cmscountry, parentId
            cmsdisplayname.append(cmsdisplay_name)
            cid.append(cms_cid)
            cmscountry.append(cms_country)
            parentId.append(cms_parentId)
            self.cms_hidden_list.addItem(str(cmsdisplay_name))
            n=n+1
    global zip_cmsdata
    zip_cmsdata = zip(cmsdisplayname,cid,cmscountry,parentId)
    
    def Search(self, text):
        #print(text)
        #print(cmsdisplayname)
        #print(self.cms_display_list.count)
        self.cms_display_list.clear()
        items = self.cms_hidden_list.findItems(text, QtCore.Qt.MatchFlag.MatchContains)
        #print(items)
        #print(type(items))
        for i in items:
            #print(i.text())
            self.cms_display_list.addItem(i.text())

    
    def show_select_item(self):
        print('item click')
        item = self.cms_display_list.selectedItems()[0]
        print(item.text())
        #self.label.setText(text_0 + item.text())
        #Find account info
        print([i for i in zip_cmsdata if item.text() in i])

zip 对象是可迭代的,这意味着它只能被迭代一次。由于它是全球性的,因此永远不会更新并且 returns 什么都没有。

a = [i for i in range(0,10)]
b = [i for i in range(20,30)]
Z = zip(a,b)
print("first run")
for i in Z:
    print(i)
print("second run")
for i in Z:
    print(i)

输出:

first run
(0, 20)
(1, 21)
(2, 22)
(3, 23)
(4, 24)
(5, 25)
(6, 26)
(7, 27)
(8, 28)
(9, 29)
second run

您可以通过重新生成 zip 对象来解决此问题:

a = [i for i in range(0,10)]
b = [i for i in range(20,30)]

print("first run")
Z = zip(a,b)
for i in Z:
    print(i)

print("second run")
Z = zip(a,b)
for i in Z:
    print(i)

输出:

first run
(0, 20)
(1, 21)
(2, 22)
(3, 23)
(4, 24)
(5, 25)
(6, 26)
(7, 27)
(8, 28)
(9, 29)
second run
(0, 20)
(1, 21)
(2, 22)
(3, 23)
(4, 24)
(5, 25)
(6, 26)
(7, 27)
(8, 28)
(9, 29)