本文整理汇总了Python中sortedcontainers.SortedList.bisect方法的典型用法代码示例。如果您正苦于以下问题:Python SortedList.bisect方法的具体用法?Python SortedList.bisect怎么用?Python SortedList.bisect使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类sortedcontainers.SortedList
的用法示例。
在下文中一共展示了SortedList.bisect方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_bisect
# 需要导入模块: from sortedcontainers import SortedList [as 别名]
# 或者: from sortedcontainers.SortedList import bisect [as 别名]
def test_bisect():
slt = SortedList()
assert slt.bisect(0) == 0
slt = SortedList(range(100), load=17)
slt.update(range(100))
slt._check()
assert slt.bisect(50) == 100
assert slt.bisect(200) == 200
示例2: test_bisect
# 需要导入模块: from sortedcontainers import SortedList [as 别名]
# 或者: from sortedcontainers.SortedList import bisect [as 别名]
def test_bisect():
slt = SortedList()
assert slt.bisect(10) == 0
slt = SortedList(range(100))
slt._reset(17)
slt.update(range(100))
slt._check()
assert slt.bisect(10) == 22
assert slt.bisect(200) == 200
示例3: PriorityDict
# 需要导入模块: from sortedcontainers import SortedList [as 别名]
# 或者: from sortedcontainers.SortedList import bisect [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.
#.........这里部分代码省略.........
示例4: xrange
# 需要导入模块: from sortedcontainers import SortedList [as 别名]
# 或者: from sortedcontainers.SortedList import bisect [as 别名]
from random import uniform
from sortedcontainers import SortedList
x = [uniform(0,1) for i in xrange(20000)]
val = x[4500]
%time x.index(val)
rev_dict = dict(zip(x, range(20000)))
%time rev_dict[val]
rev_bis = SortedList((v,i) for (i,v) in enumerate(x))
%time rev_bis.bisect((val,))