您还没有真正发布足够的代码,但据我所知:
import collections
import uuid
class Person():
def __init__(self, name="", age=69):
self.name = name
self.uid = str( uuid.uuid4( ) )
self.age = age
def __eq__(self, other):
return isinstance(other, Person) and self.uid == other.uid
def __ne__(self, other): return self != other # you need this
def __hash__(self):
return hash(self.uid)
# UI --------------------------------------------------------------------------
persons_count = collections.defaultdict(int) # belongs to your UI class
your_list_of_persons = [] # should be a set
def add_to_ui(person):
persons_count[person] += 1
# add it to the UI
def remove_from_ui(person):
persons_count[person] -= 1
if not persons_count[person]: your_list_of_persons.remove(person)
# remove from UI
所以基本上:
在执行删除操作(仅删除树节点项目)之前,我将收集用户界面中使用的所有人员。
否 - 您始终可以将此信息作为用户界面中的模块变量使用 -persons_count
多于。这样您就不必复制列表。
仍然是创建人员的代码 - 然后是您的列表(其中包含distinct人所以应该是set)应该更新。如果这是在add_to_ui
(有道理)你应该修改为:
def add_to_ui(name, age):
p = Person(name, age)
set_of_persons.add(p) # if already there won't be re-added and it's O(1)
persons_count[person] += 1
# add it to the UI
更进一步 - 你真的不需要你的原始列表 - 这只是persons_count.keys()
,你只需修改:
def add_to_ui(name, age):
p = Person(name, age)
persons_count[person] += 1
# add it to the UI
def remove_from_ui(person):
persons_count[person] -= 1
if not persons_count[person]: del persons_count[person]
# remove from UI
这样你就明白了
EDIT:这是从我的最新迭代中删除的:
def delete_tree_nodes_clicked(self):
root = self.treeWidget.invisibleRootItem()
# delete treewidget items from gui
for item in self.treeWidget.selectedItems():
(item.parent() or root).removeChild(item)
self.highlighted.discard(item)
persons_count[item.person] -= 1
if not persons_count[item.person]: del persons_count[item.person]
我已将我的解决方案(重写与第一个问题相关的代码)发布在:https://github.com/Utumno/so_34104763/commits/master。这是一个很好的重构练习 - 查看提交消息。我在这里特别介绍一下这个字典:https://github.com/Utumno/so_34104763/commit/074b7e659282a9896ea11bbef770464d07e865b7
可以做更多的工作,但我认为这是朝着正确方向迈出的一步 - 在大多数操作中也应该更快并节省内存