我有一个python程序,我使用优先级队列来跟踪要处理的对象。目前,队列是使用SortedList实现的,而SortedList工作得非常好。
但是,我需要扩展此代码,以便列表在多个键上保持排序。有点像在多列上有索引的SQL数据库,因此我可以有效地访问,添加和删除所有键上的对象。我的工作量是添加/删除重。为了了解我想要做什么,这里有一些伪代码:
ml = MultiSortedList()
ml.append((1, "z", 1.5), a)
ml.append((2, "a", 40.0), b)
ml.append((3, "f", 0.5), c)
print(ml.sorted(0))
[((1, "z", 1.5), a),
((2, "a", 40.0), b),
((3, "f", 0.5), c),]
print(ml.sorted(2))
[((3, "f", 0.5), c),
((1, "z", 1.5), a),
((2, "a", 40.0), b)]
print(ml.sorted(2).pop(1)
(1, "z", 1.5), a)
print(ml.sorted(0))
[((2, "a", 40.0), b),
((3, "f", 0.5), c)]
我无法弄清楚如何有效地做到这一点。当然,我可以再次为每个访问不同列的列表排序,但这太贵了。此外,python列表上的O(n)
删除操作变得很痛苦,因为列表可能包含数千个对象。
是否存在解决此问题的现有数据结构(最好已在python中实现)?如果没有,你能帮我概述一下如何有效地实现这个目标吗?
答案 0 :(得分:5)
您应该使用heap,而不是使用排序列表作为优先级队列的实现。例如,二进制堆具有log(n)
插入和删除。 Fibonacci堆将O(1)
插入。
您在标准库中有一个实现:heapq
模块。
对于您的用例,您需要保留多个堆:每个排序顺序一个堆。为了清楚实现,您可能希望将原始数据保存在字典上(可能使用随机键或递增键),并且只将其键保留在堆上。
使用此技术,您可以轻松获得O(1)
插入和O(log(n))
删除。您没有真正指定您将拥有的访问类型,但如果您需要随机访问,则可以在相应的堆上使用binary search来获取O(log(n))
答案 1 :(得分:1)
一种方法是在内部维护n
列表,每个排序顺序一个,并为每个列表添加/删除项目。
这样,您“仅”将数据结构的操作时间乘以常数值(即,如果使用3个键而不是1个键,则使用3 log(n)
代替log(n)
来删除/插入元素)
我想象这个实现背后的概念是java的Comparator one。
为每个键创建一个比较器方法,用于对它们进行排序,然后在插入和删除时使用它。
它可以如下工作:
class SortedList(list):
def __init__(self, comparator = None):
list.__init__(self)
self.comparator = comparator
def add(self, element):
""" Adds the element into the sorted list.
If no comparator where provided at initialisation, then try to compare
the element to item already stored in the list using standard __gt__
and __eq__.
Argument :
element : the element to insert in the list
/!\ NB : A more inteligent implementation should be used, such as binary search... /!\
"""
index = 0
while (index < len(self)):
if self.isGreatterThan(element, self[index]):
index += 1
else:
break
self.insert(index, element)
def remove(self, element):
""" Same as previous, a better approach is possible that use binary search """
list.remove(self, element)
def isGreatterThan(self, element, otherElement):
""" Compare if element is greater than other element. """
if self.comparator != None:
return self.comparator(element, otherElement)
else:
return element.__gt__(otherElement)
class MultipleKeysObjectContainer():
def __init__(self, comparators):
#register the comparators
self.comparators = comparators
#create a List for each comparator
self.data = {}
for key in self.comparators.keys():
self.data[key] = SortedList(comparator = self.comparators[key])
def add(self, element):
for key in self.data.keys():
self.data[key].add(element)
def __repr__(self):
return "<MultipleKeysObjectContainer :"+self.data.__repr__()+">"
def __str__(self):
result = "MultipleKeysObjectContainer{\n"
for key in self.data.keys():
result += "\tOrder by : "+key+"{\n"
for element in self.data[key]:
result += "\t\t" + str(element) + "\n"
result += "\t}\n"
result += "}"
return result
def popOrderedBy(self, key, position):
""" pop the item a the position in the list of item ordered by the key.
Remove also from other data containers. """
item = self.data[key].pop(position)
for dataKey in self.data.keys():
if dataKey != key:
self.data[dataKey].remove(item)
return item
if __name__ == "__main__":
a = SortedList(lambda x,y : x[0][0] > y[0][0])
item1 = ((1, "z", 1.5),"foo")
item2 = ((2, "a", 40.0), "bar")
item3 = ((3, "f", 0.5), "barfoo")
a.add(item1)
a.add(item3)
a.add(item2)
print("Example of sorted list")
print(a)
a.remove(item3)
print("The same list without the barfoo")
print(a)
b = MultipleKeysObjectContainer({"id": (lambda x,y : x[0][0] > y[0][0]), "letter": (lambda x,y : x[0][1] > y[0][1] ), "value":(lambda x,y : x[0][2] > y[0][2])})
b.add(item1)
b.add(item3)
b.add(item2)
print("A multiple key container, object are ordered according three criterion.")
print(b)
print("Remove the first item if items are ordered by letter", b.popOrderedBy("letter", 0))
print("After this removing the container contains :")
print(b)
这导致:
Example of sorted list
[((1, 'z', 1.5), 'foo'), ((2, 'a', 40.0), 'bar'), ((3, 'f', 0.5), 'barfoo')]
The same list without the barfoo
[((1, 'z', 1.5), 'foo'), ((2, 'a', 40.0), 'bar')]
A multiple key container, object are ordered according three criterion.
MultipleKeysObjectContainer{
Order by : id{
((1, 'z', 1.5), 'foo')
((2, 'a', 40.0), 'bar')
((3, 'f', 0.5), 'barfoo')
}
Order by : value{
((3, 'f', 0.5), 'barfoo')
((1, 'z', 1.5), 'foo')
((2, 'a', 40.0), 'bar')
}
Order by : letter{
((2, 'a', 40.0), 'bar')
((3, 'f', 0.5), 'barfoo')
((1, 'z', 1.5), 'foo')
}
}
Remove the first item if items are ordered by letter ((2, 'a', 40.0), 'bar')
After this removing the container contains :
MultipleKeysObjectContainer{
Order by : id{
((1, 'z', 1.5), 'foo')
((3, 'f', 0.5), 'barfoo')
}
Order by : value{
((3, 'f', 0.5), 'barfoo')
((1, 'z', 1.5), 'foo')
}
Order by : letter{
((3, 'f', 0.5), 'barfoo')
((1, 'z', 1.5), 'foo')
}
}
看起来你正在寻找(差不多,只需添加二进制搜索:p) 祝你好运!