本文整理汇总了Python中sortedcontainers.SortedList.pop方法的典型用法代码示例。如果您正苦于以下问题:Python SortedList.pop方法的具体用法?Python SortedList.pop怎么用?Python SortedList.pop使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类sortedcontainers.SortedList
的用法示例。
在下文中一共展示了SortedList.pop方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_pop
# 需要导入模块: from sortedcontainers import SortedList [as 别名]
# 或者: from sortedcontainers.SortedList import pop [as 别名]
def test_pop():
slt = SortedList(range(10), load=4)
slt._check()
assert slt.pop() == 9
slt._check()
assert slt.pop(0) == 0
slt._check()
assert slt.pop(-2) == 7
slt._check()
assert slt.pop(4) == 5
slt._check()
示例2: score_of_a_vacated_people
# 需要导入模块: from sortedcontainers import SortedList [as 别名]
# 或者: from sortedcontainers.SortedList import pop [as 别名]
def score_of_a_vacated_people(self, universo, work='translations'):
factor = math.sqrt(len(universo))
scores = SortedList(load=round(factor))
for (people, score) in self.__scores__().items():
if people in universo:
scores.add(TranslatorScore(people, score[work]))
return scores.pop(0)
示例3: __init__
# 需要导入模块: from sortedcontainers import SortedList [as 别名]
# 或者: from sortedcontainers.SortedList import pop [as 别名]
class DijkstraFixedPoint:
def __init__(self, automaton, initial_set, accepted_set):
self.automaton = automaton
self.set_to_visit = SortedList(initial_set,key= lambda d: -len(d))
self.accepted_set = accepted_set
def iter_fix_point_set(self,max_size=10):
if len(self.set_to_visit)==0:
raise StopIteration()
F = self.set_to_visit.pop()
nF = {k:[v] for k,v in F.items()}
new_size_of_fp = len(nF)
reach_accepted_set = False
for u,lu in F.items():
labelled_edges = self.automaton.get_labelled_successors(u)
succ = labelled_edges[lu]
for s in succ:
if s in self.accepted_set:
reach_accepted_set = True
if (s not in nF) and (s not in self.accepted_set):
nF[s] = list(self.automaton.get_successor_labels(s))
new_size_of_fp = len(nF)
if new_size_of_fp>max_size:
return False,F
newF = self.expand_successor_set(nF)
if F in newF:
newF.remove(F)
self.set_to_visit.update(newF)
accept_fix_point = (len(newF)==0) and reach_accepted_set
return accept_fix_point,F
def expand_successor_set(self,nF):
sF = []
# import operator
# size = reduce(operator.mul, [len(v) for v in nF.values()], 1)
for conf in itertools.product(*nF.values()):
sF.append({k:v for k,v in zip(nF.keys(),conf)})
return sF
def __iter__(self):
return self
def next(self):
return self.iter_fix_point_set()
def next_fixed_point(self,max_size):
fp_found = 0
try:
while fp_found==False:
fp_found,fp = self.iter_fix_point_set(max_size)
#print "#"*len(fp)
except StopIteration:
return False,None
return fp_found,fp
示例4: __init__
# 需要导入模块: from sortedcontainers import SortedList [as 别名]
# 或者: from sortedcontainers.SortedList import pop [as 别名]
class Store:
def __init__(self,N=10):
self.store = SortedList()
self.N = N
def add(self,item):
self.store.add(item)
if len(self.store) > self.N: self.store.pop(0)
def pop(self,i):
self.store.pop(i)
def __len__(self):
return len(self.store)
def __getitem__(self,i):
return self.store[i]
def __str__(self):
return str(self.store)
示例5: test_pop_indexerror2
# 需要导入模块: from sortedcontainers import SortedList [as 别名]
# 或者: from sortedcontainers.SortedList import pop [as 别名]
def test_pop_indexerror2():
slt = SortedList(range(10), load=4)
slt.pop(10)
示例6: PriorityDict
# 需要导入模块: from sortedcontainers import SortedList [as 别名]
# 或者: from sortedcontainers.SortedList import pop [as 别名]
class PriorityDict(MutableMapping):
"""
A PriorityDict provides the same methods as a dict. Additionally, a
PriorityDict efficiently maintains its keys in value sorted order.
Consequently, the keys method will return the keys in value sorted order,
the popitem method will remove the item with the highest value, etc.
"""
def __init__(self, *args, **kwargs):
"""
A PriorityDict provides the same methods as a dict. Additionally, a
PriorityDict efficiently maintains its keys in value sorted order.
Consequently, the keys method will return the keys in value sorted
order, the popitem method will remove the item with the highest value,
etc.
If the first argument is the boolean value False, then it indicates
that keys are not comparable. By default this setting is True and
duplicate values are tie-breaked on the key. Using comparable keys
improves the performance of the PriorityDict.
An optional *iterable* argument provides an initial series of items to
populate the PriorityDict. Each item in the sequence must itself
contain two items. The first is used as a key in the new dictionary,
and the second as the key's value. If a given key is seen more than
once, the last value associated with it is retained in the new
dictionary.
If keyword arguments are given, the keywords themselves with their
associated values are added as items to the dictionary. If a key is
specified both in the positional argument and as a keyword argument, the
value associated with the keyword is retained in the dictionary. For
example, these all return a dictionary equal to ``{"one": 2, "two":
3}``:
* ``SortedDict(one=2, two=3)``
* ``SortedDict({'one': 2, 'two': 3})``
* ``SortedDict(zip(('one', 'two'), (2, 3)))``
* ``SortedDict([['two', 3], ['one', 2]])``
The first example only works for keys that are valid Python
identifiers; the others work with any valid keys.
Note that this constructor mimics the Python dict constructor. If
you're looking for a constructor like collections.Counter(...), see
PriorityDict.count(...).
"""
self._dict = dict()
if len(args) > 0 and isinstance(args[0], bool):
if args[0]:
self._list = SortedList()
else:
self._list = SortedListWithKey(key=lambda tup: tup[0])
else:
self._list = SortedList()
self.iloc = _IlocWrapper(self)
self.update(*args, **kwargs)
def clear(self):
"""Remove all elements from the dictionary."""
self._dict.clear()
self._list.clear()
def clean(self, value=0):
"""
Remove all items with value less than or equal to `value`.
Default `value` is 0.
"""
_list, _dict = self._list, self._dict
pos = self.bisect_right(value)
for key in (key for value, key in _list[:pos]):
del _dict[key]
del _list[:pos]
def __contains__(self, key):
"""Return True if and only if *key* is in the dictionary."""
return key in self._dict
def __delitem__(self, key):
"""
Remove ``d[key]`` from *d*. Raises a KeyError if *key* is not in the
dictionary.
"""
value = self._dict[key]
self._list.remove((value, key))
del self._dict[key]
def __getitem__(self, key):
"""
Return the priority of *key* in *d*. Raises a KeyError if *key* is not
in the dictionary.
"""
return self._dict[key]
def __iter__(self):
"""
Create an iterator over the keys of the dictionary ordered by the value
sort order.
"""
return iter(key for value, key in self._list)
def __reversed__(self):
"""
Create an iterator over the keys of the dictionary ordered by the
reversed value sort order.
#.........这里部分代码省略.........
示例7: test_pop_indexerror3
# 需要导入模块: from sortedcontainers import SortedList [as 别名]
# 或者: from sortedcontainers.SortedList import pop [as 别名]
def test_pop_indexerror3():
slt = SortedList()
slt.pop()
示例8: test_pop_indexerror3
# 需要导入模块: from sortedcontainers import SortedList [as 别名]
# 或者: from sortedcontainers.SortedList import pop [as 别名]
def test_pop_indexerror3():
slt = SortedList()
with pytest.raises(IndexError):
slt.pop()
示例9: test_pop_indexerror2
# 需要导入模块: from sortedcontainers import SortedList [as 别名]
# 或者: from sortedcontainers.SortedList import pop [as 别名]
def test_pop_indexerror2():
slt = SortedList(range(10))
slt._reset(4)
with pytest.raises(IndexError):
slt.pop(10)
示例10: group_files_by_size_fast
# 需要导入模块: from sortedcontainers import SortedList [as 别名]
# 或者: from sortedcontainers.SortedList import pop [as 别名]
def group_files_by_size_fast(fileslist, nbgroups, mode=1): # pragma: no cover
'''Given a files list with sizes, output a list where the files are grouped in nbgroups per cluster.
Pseudo-code for algorithm in O(n log(g)) (thank's to insertion sort or binary search trees)
See for more infos: http://cs.stackexchange.com/questions/44406/fast-algorithm-for-clustering-groups-of-elements-given-their-size-time/44614#44614
For each file:
- If to-fill list is empty or file.size > first-key(to-fill):
* Create cluster c with file in first group g1
* Add to-fill[file.size].append([c, g2], [c, g3], ..., [c, gn])
- Else:
* ksize = first-key(to-fill)
* c, g = to-fill[ksize].popitem(0)
* Add file to cluster c in group g
* nsize = ksize - file.size
* if nsize > 0:
. to-fill[nsize].append([c, g])
. sort to-fill if not an automatic ordering structure
'''
ftofill = SortedList()
ftofill_pointer = {}
fgrouped = [] # [] or {}
ford = sorted(fileslist.iteritems(), key=lambda x: x[1])
last_cid = -1
while ford:
fname, fsize = ford.pop()
#print "----\n"+fname, fsize
#if ftofill: print "beforebranch", fsize, ftofill[-1]
#print ftofill
if not ftofill or fsize > ftofill[-1]:
last_cid += 1
#print "Branch A: create cluster %i" % last_cid
fgrouped.append([])
#fgrouped[last_cid] = []
fgrouped[last_cid].append([fname])
if mode==0:
for g in xrange(nbgroups-1, 0, -1):
fgrouped[last_cid].append([])
if not fsize in ftofill_pointer:
ftofill_pointer[fsize] = []
ftofill_pointer[fsize].append((last_cid, g))
ftofill.add(fsize)
else:
for g in xrange(1, nbgroups):
try:
fgname, fgsize = ford.pop()
#print "Added to group %i: %s %i" % (g, fgname, fgsize)
except IndexError:
break
fgrouped[last_cid].append([fgname])
diff_size = fsize - fgsize
if diff_size > 0:
if not diff_size in ftofill_pointer:
ftofill_pointer[diff_size] = []
ftofill_pointer[diff_size].append((last_cid, g))
ftofill.add(diff_size)
else:
#print "Branch B"
ksize = ftofill.pop()
c, g = ftofill_pointer[ksize].pop()
#print "Assign to cluster %i group %i" % (c, g)
fgrouped[c][g].append(fname)
nsize = ksize - fsize
if nsize > 0:
if not nsize in ftofill_pointer:
ftofill_pointer[nsize] = []
ftofill_pointer[nsize].append((c, g))
ftofill.add(nsize)
return fgrouped