本文整理汇总了Python中sortedcontainers.SortedList.remove方法的典型用法代码示例。如果您正苦于以下问题:Python SortedList.remove方法的具体用法?Python SortedList.remove怎么用?Python SortedList.remove使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类sortedcontainers.SortedList
的用法示例。
在下文中一共展示了SortedList.remove方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: collect_matches
# 需要导入模块: from sortedcontainers import SortedList [as 别名]
# 或者: from sortedcontainers.SortedList import remove [as 别名]
def collect_matches():
initial_summoner_name = "GustavEnk"
region = "EUW"
summoner = Summoner(name=initial_summoner_name, region=region)
patch = Patch.from_str("8.9", region=region)
unpulled_summoner_ids = SortedList([summoner.id])
pulled_summoner_ids = SortedList()
unpulled_match_ids = SortedList()
pulled_match_ids = SortedList()
while unpulled_summoner_ids:
# Get a random summoner from our list of unpulled summoners and pull their match history
new_summoner_id = random.choice(unpulled_summoner_ids)
new_summoner = Summoner(id=new_summoner_id, region=region)
matches = filter_match_history(new_summoner, patch)
unpulled_match_ids.update([match.id for match in matches])
unpulled_summoner_ids.remove(new_summoner_id)
pulled_summoner_ids.add(new_summoner_id)
while unpulled_match_ids:
# Get a random match from our list of matches
new_match_id = random.choice(unpulled_match_ids)
new_match = Match(id=new_match_id, region=region)
for participant in new_match.participants:
if participant.summoner.id not in pulled_summoner_ids and participant.summoner.id not in unpulled_summoner_ids:
unpulled_summoner_ids.add(participant.summoner.id)
# The above lines will trigger the match to load its data by iterating over all the participants.
# If you have a database in your datapipeline, the match will automatically be stored in it.
unpulled_match_ids.remove(new_match_id)
pulled_match_ids.add(new_match_id)
示例2: Episode_scores
# 需要导入模块: from sortedcontainers import SortedList [as 别名]
# 或者: from sortedcontainers.SortedList import remove [as 别名]
class Episode_scores(object):
def __init__(self, options):
self.maxlen = options.score_averaging_length
self.threshold = options.score_highest_ratio
self.episode_scores = deque()
self.episode_scores.append(0) # to avoid 0-div in first averaging
self.episode_scores_sum = 0
self.sorted_scores = SortedList()
self.sorted_scores.add(0) # align to episode_scores
self.num_episode = 0
self.options = options
def add(self, n, global_t, thread_index):
self.episode_scores_sum += n
self.episode_scores.append(n)
self.sorted_scores.add(-n) # trick to use SortedList in reverse order
if len(self.episode_scores) > self.maxlen:
oldest = self.episode_scores.popleft()
self.sorted_scores.remove(-oldest)
self.episode_scores_sum -= oldest
self.num_episode += 1
if self.num_episode % self.options.average_score_log_interval == 0:
print("@@@ Average Episode score = {:.6f}, s={:9d},th={}".format(self.average(), global_t, thread_index))
def average(self):
return self.episode_scores_sum / len(self.episode_scores)
def is_highscore(self, n):
sorted_scores = self.sorted_scores
num_scores = len(sorted_scores)
sorted_scores.add(-n)
index = sorted_scores.index(-n)
highest_ratio = (index + 1) / num_scores
sorted_scores.remove(-n)
return highest_ratio <= self.threshold
示例3: InMemoryBackend
# 需要导入模块: from sortedcontainers import SortedList [as 别名]
# 或者: from sortedcontainers.SortedList import remove [as 别名]
class InMemoryBackend(object):
"""
The backend that keeps the results in the memory.
"""
def __init__(self, *args, **kwargs):
def get_timestamp(result):
return timestamp_parser.parse(result['timestamp'])
self._results = dict()
self._sorted = SortedList(key=get_timestamp)
def disconnect(self):
return succeed(None)
def store(self, result):
"""
Store a single benchmarking result and return its identifier.
:param dict result: The result in the JSON compatible format.
:return: A Deferred that produces an identifier for the stored
result.
"""
id = uuid4().hex
self._results[id] = result
self._sorted.add(result)
return succeed(id)
def retrieve(self, id):
"""
Retrive a result by the given identifier.
"""
try:
return succeed(self._results[id])
except KeyError:
return fail(ResultNotFound(id))
def query(self, filter, limit=None):
"""
Return matching results.
"""
matching = []
for result in reversed(self._sorted):
if len(matching) == limit:
break
if filter.viewitems() <= result.viewitems():
matching.append(result)
return succeed(matching)
def delete(self, id):
"""
Delete a result by the given identifier.
"""
try:
result = self._results.pop(id)
self._sorted.remove(result)
return succeed(None)
except KeyError:
return fail(ResultNotFound(id))
示例4: test_delete
# 需要导入模块: from sortedcontainers import SortedList [as 别名]
# 或者: from sortedcontainers.SortedList import remove [as 别名]
def test_delete():
slt = SortedList(range(20), load=4)
slt._check()
for val in range(20):
slt.remove(val)
slt._check()
assert len(slt) == 0
assert slt._maxes == []
assert slt._lists == []
示例5: test_remove
# 需要导入模块: from sortedcontainers import SortedList [as 别名]
# 或者: from sortedcontainers.SortedList import remove [as 别名]
def test_remove():
slt = SortedList()
assert slt.discard(0) == None
assert len(slt) == 0
slt._check()
slt = SortedList([1, 2, 2, 2, 3, 3, 5], load=4)
slt.remove(2)
slt._check()
assert all(tup[0] == tup[1] for tup in zip(slt, [1, 2, 2, 3, 3, 5]))
示例6: test_remove_valueerror1
# 需要导入模块: from sortedcontainers import SortedList [as 别名]
# 或者: from sortedcontainers.SortedList import remove [as 别名]
def test_remove_valueerror1():
slt = SortedList()
slt.remove(0)
示例7: PriorityDict
# 需要导入模块: from sortedcontainers import SortedList [as 别名]
# 或者: from sortedcontainers.SortedList import remove [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.
#.........这里部分代码省略.........
示例8: TxGraph
# 需要导入模块: from sortedcontainers import SortedList [as 别名]
# 或者: from sortedcontainers.SortedList import remove [as 别名]
#.........这里部分代码省略.........
"""
node = self.nodeMap.get(name, None)
if node is None and create is True:
node = Node(name)
self.nodeMap[name] = node
return node
def __incr_degree_of_edge_nodes(self, edge):
"""increments the degree of the two nodes
of an edge
"""
src = self.__getnode_with_name(edge.source)
src.incr_degree()
tar = self.__getnode_with_name(edge.target)
tar.incr_degree()
return (src.degree, tar.degree)
def __decr_degree_of_edge_nodes(self, edge):
"""decrements the degree of the two nodes
of an edge
"""
self.__decr_degree_of_node(edge.source)
self.__decr_degree_of_node(edge.target)
def __decr_degree_of_node(self, name):
"""decrements the degree of a node
and removes it from the nodeMap if degree is 0
"""
node = self.__getnode_with_name(name, create=False)
node.decr_degree()
if node.degree == 0:
del self.nodeMap[node.name]
def __remove_edge(self, edge):
"""removes an edge from the graph and updates the
degree of a node. If degree of a node goes to 0, then
remove the node as well
Args:
egde(Edge) : An instance of Edge class
"""
self.__decr_degree_of_edge_nodes(edge)
del self.edgeMap[edge.name]
def __update_tstamp_for_existing_edge(self, edgeName, tstamp):
"""updates the timestamp for an existing edge and moves
the edge to an appropriate EdgeList
Args:
edgeName(str) : name of the edge to be updated
tstamp(int) : unix epoch of the timstamp
"""
currEdge = self.edgeMap[edgeName]
示例9: TTLCache
# 需要导入模块: from sortedcontainers import SortedList [as 别名]
# 或者: from sortedcontainers.SortedList import remove [as 别名]
class TTLCache(object):
"""A key/value cache implementation where each entry has its own TTL"""
def __init__(self, cache_name, timer=time.time):
# map from key to _CacheEntry
self._data = {}
# the _CacheEntries, sorted by expiry time
self._expiry_list = SortedList()
self._timer = timer
self._metrics = register_cache("ttl", cache_name, self)
def set(self, key, value, ttl):
"""Add/update an entry in the cache
Args:
key: key for this entry
value: value for this entry
ttl (float): TTL for this entry, in seconds
"""
expiry = self._timer() + ttl
self.expire()
e = self._data.pop(key, SENTINEL)
if e != SENTINEL:
self._expiry_list.remove(e)
entry = _CacheEntry(expiry_time=expiry, key=key, value=value)
self._data[key] = entry
self._expiry_list.add(entry)
def get(self, key, default=SENTINEL):
"""Get a value from the cache
Args:
key: key to look up
default: default value to return, if key is not found. If not set, and the
key is not found, a KeyError will be raised
Returns:
value from the cache, or the default
"""
self.expire()
e = self._data.get(key, SENTINEL)
if e == SENTINEL:
self._metrics.inc_misses()
if default == SENTINEL:
raise KeyError(key)
return default
self._metrics.inc_hits()
return e.value
def get_with_expiry(self, key):
"""Get a value, and its expiry time, from the cache
Args:
key: key to look up
Returns:
Tuple[Any, float]: the value from the cache, and the expiry time
Raises:
KeyError if the entry is not found
"""
self.expire()
try:
e = self._data[key]
except KeyError:
self._metrics.inc_misses()
raise
self._metrics.inc_hits()
return e.value, e.expiry_time
def pop(self, key, default=SENTINEL):
"""Remove a value from the cache
If key is in the cache, remove it and return its value, else return default.
If default is not given and key is not in the cache, a KeyError is raised.
Args:
key: key to look up
default: default value to return, if key is not found. If not set, and the
key is not found, a KeyError will be raised
Returns:
value from the cache, or the default
"""
self.expire()
e = self._data.pop(key, SENTINEL)
if e == SENTINEL:
self._metrics.inc_misses()
if default == SENTINEL:
raise KeyError(key)
return default
self._expiry_list.remove(e)
self._metrics.inc_hits()
return e.value
#.........这里部分代码省略.........
示例10: bool
# 需要导入模块: from sortedcontainers import SortedList [as 别名]
# 或者: from sortedcontainers.SortedList import remove [as 别名]
class SCEngine:
'''
Fast tree-based implementation for indexing, using the
``sortedcontainers`` package.
Parameters
----------
data : Table
Sorted columns of the original table
row_index : Column object
Row numbers corresponding to data columns
unique : bool (defaults to False)
Whether the values of the index must be unique
'''
def __init__(self, data, row_index, unique=False):
node_keys = map(tuple, data)
self._nodes = SortedList(starmap(Node, zip(node_keys, row_index)))
self._unique = unique
def add(self, key, value):
'''
Add a key, value pair.
'''
if self._unique and (key in self._nodes):
message = 'duplicate {0:!r} in unique index'.format(key)
raise ValueError(message)
self._nodes.add(Node(key, value))
def find(self, key):
'''
Find rows corresponding to the given key.
'''
return [node.value for node in self._nodes.irange(key, key)]
def remove(self, key, data=None):
'''
Remove data from the given key.
'''
if data is not None:
item = Node(key, data)
try:
self._nodes.remove(item)
except ValueError:
return False
return True
items = list(self._nodes.irange(key, key))
for item in items:
self._nodes.remove(item)
return bool(items)
def shift_left(self, row):
'''
Decrement rows larger than the given row.
'''
for node in self._nodes:
if node.value > row:
node.value -= 1
def shift_right(self, row):
'''
Increment rows greater than or equal to the given row.
'''
for node in self._nodes:
if node.value >= row:
node.value += 1
def items(self):
'''
Return a list of key, data tuples.
'''
result = OrderedDict()
for node in self._nodes:
if node.key in result:
result[node.key].append(node.value)
else:
result[node.key] = [node.value]
return result.items()
def sort(self):
'''
Make row order align with key order.
'''
for index, node in enumerate(self._nodes):
node.value = index
def sorted_data(self):
'''
Return a list of rows in order sorted by key.
'''
return [node.value for node in self._nodes]
def range(self, lower, upper, bounds=(True, True)):
'''
Return row values in the given range.
'''
iterator = self._nodes.irange(lower, upper, bounds)
return [node.value for node in iterator]
def replace_rows(self, row_map):
'''
#.........这里部分代码省略.........
示例11: test_remove_valueerror1
# 需要导入模块: from sortedcontainers import SortedList [as 别名]
# 或者: from sortedcontainers.SortedList import remove [as 别名]
def test_remove_valueerror1():
slt = SortedList()
with pytest.raises(ValueError):
slt.remove(0)
示例12: Fuzzer
# 需要导入模块: from sortedcontainers import SortedList [as 别名]
# 或者: from sortedcontainers.SortedList import remove [as 别名]
#.........这里部分代码省略.........
assert len(self.__corpus) == len(self.__refcounts) == len(self.__seen) \
== 1
def incorporate(self, string):
key = cache_key(string)
if key in self.__seen:
return
self.__seen.add(key)
labels = self.__classifier(string)
item = CorpusItem(string)
new_labels = set()
improved_labels = set()
for l in labels:
if (
l not in self.__strings_by_tag or
item < CorpusItem(self.__strings_by_tag[l])
):
self.__incref(string)
if l in self.__strings_by_tag:
self.__decref(self.__strings_by_tag[l])
improved_labels.add(l)
else:
new_labels.add(l)
self.__strings_by_tag[l] = string
if new_labels:
self.__lifecycle.new_labels(new_labels)
if improved_labels:
self.__lifecycle.labels_improved(improved_labels)
def fuzz(self):
while True:
for target in reversed(self.__corpus):
key = cache_key(target.string)
if key not in self.__fully_shrunk:
for string in self.__shrinks(target.string):
self.incorporate(string)
if target not in self.__corpus:
break
else:
self.__fully_shrunk.add(key)
break
def __shrinkers(self):
n = len(self.__corpus[-1].string)
while n > 1:
yield self.__cutter(n, n)
n //= 2
yield self.__byte_clearing
n = len(self.__corpus[-1].string)
while n > 1:
i = n
while i > 0:
yield self.__cutter(i, n)
i //= 2
n -= 1
def __shrinks(self, string):
for shrinker in self.__shrinkers():
for s in shrinker(string):
yield s
def __byte_clearing(self, string):
counter = Counter(string)
for c in sorted(counter, key=lambda x: (-counter[x], x)):
yield string.replace(bytes([c]), b'')
def __cutter(self, step, size):
assert step > 0
assert size > 0
def accept(string):
if size >= len(string):
return
i = 0
while i + size <= len(string):
yield string[:i] + string[i+size:]
i += step
accept.__name__ = '__cutter(%d, %d)' % (step, size)
return accept
def __incref(self, string):
c = self.__refcounts.get(string, 0)
assert c >= 0
if c == 0:
self.__counter += 1
self.__corpus.add(CorpusItem(string))
self.__lifecycle.item_added(string)
self.__refcounts[string] = c + 1
def __decref(self, string):
assert self.__refcounts[string] > 0
self.__refcounts[string] -= 1
if self.__refcounts[string] <= 0:
self.__counter += 1
self.__corpus.remove(CorpusItem(string))
del self.__refcounts[string]
self.__lifecycle.item_removed(string)
示例13: Gauge
# 需要导入模块: from sortedcontainers import SortedList [as 别名]
# 或者: from sortedcontainers.SortedList import remove [as 别名]
#.........这里部分代码省略.........
:raises ValueError: `since` later than or same with `until`.
:raises TypeError: the first argument is a momentum, but other
arguments passed.
"""
if isinstance(velocity_or_momentum, Momentum):
if not (since is until is None):
raise TypeError('Arguments behind the first argument as a '
'momentum should be None')
momentum = velocity_or_momentum
else:
velocity = velocity_or_momentum
if since is None:
since = -inf
if until is None:
until = +inf
momentum = Momentum(velocity, since, until)
since, until = momentum.since, momentum.until
if since == -inf or until == +inf or since < until:
pass
else:
raise ValueError('\'since\' should be earlier than \'until\'')
return momentum
def add_momenta(self, momenta):
"""Adds multiple momenta."""
for momentum in momenta:
self.momenta.add(momentum)
self._events.add((momentum.since, ADD, momentum))
if momentum.until != +inf:
self._events.add((momentum.until, REMOVE, momentum))
self.invalidate()
def remove_momenta(self, momenta):
"""Removes multiple momenta."""
for momentum in momenta:
try:
self.momenta.remove(momentum)
except ValueError:
raise ValueError('{0} not in the gauge'.format(momentum))
self._events.remove((momentum.since, ADD, momentum))
if momentum.until != +inf:
self._events.remove((momentum.until, REMOVE, momentum))
self.invalidate()
def add_momentum(self, *args, **kwargs):
"""Adds a momentum. A momentum includes the velocity and the times to
start to affect and to stop to affect. The determination would be
changed.
All arguments will be passed to :meth:`_make_momentum`.
:returns: a momentum object. Use this to remove the momentum by
:meth:`remove_momentum`.
:raises ValueError: `since` later than or same with `until`.
"""
momentum = self._make_momentum(*args, **kwargs)
self.add_momenta([momentum])
return momentum
def remove_momentum(self, *args, **kwargs):
"""Removes the given momentum. The determination would be changed.
All arguments will be passed to :meth:`_make_momentum`.
示例14: Log
# 需要导入模块: from sortedcontainers import SortedList [as 别名]
# 或者: from sortedcontainers.SortedList import remove [as 别名]
class Log(object):
"""Keep a random sample of stuff seen so far. Based on Dr. Menzies'
implementation."""
MAX_SIZE = 256
def __init__(self, inits=None, label=None, max_size=MAX_SIZE):
self._cache = SortedList()
self._report = None
self.label = label or ''
self._n = 0
self.max_size = max_size
self._valid_statistics = False
self._invalidate_statistics()
if inits:
map(self.__iadd__, inits)
def random_index(self):
return base.random_index(self._cache)
@classmethod
def wrap(cls, x, max_size=MAX_SIZE):
if isinstance(x, cls):
return x
return cls(inits=x, max_size=max_size)
def __len__(self):
return len(self._cache)
def extend(self, xs):
if not isinstance(xs, collections.Iterable):
raise TypeError()
map(self.__iadd__, xs)
def __iadd__(self, x):
if x is None:
return x
self._n += 1
if issubclass(x.__class__, Log):
map(self.__iadd__, x._cache)
return self
changed = False
# if cache has room, add item
if self.max_size is None or len(self._cache) < self.max_size:
changed = True
self._cache.add(x)
# cache is full: maybe replace an old item
else:
# items less likely to be replaced later in the run:
# leads to uniform sample of entire run
if random.random() <= self.max_size / len(self):
changed = True
self._cache.remove(random.choice(self._cache))
self._cache.add(x)
if changed:
self._invalidate_statistics()
self._change(x)
return self
def __add__(self, x, max_size=MAX_SIZE):
inits = itertools.chain(self._cache, x._cache)
return self.__class__(inits=inits, max_size=max_size)
def any(self):
return random.choice(self._cache)
def report(self):
if self._report is None:
self._report = self._generate_report()
return self._report
def setup(self):
raise NotImplementedError()
def as_list(self):
return self._cache.as_list()
def _invalidate_statistics(self):
'''
default implementation. if _valid_statistics is something other than
a boolean, reimplement!
'''
self._valid_statistics = False
def ish(self, *args, **kwargs):
raise NotImplementedError()
def _change(self, x):
'''
override to add incremental updating functionality
'''
pass
def _prepare_data(self):
#.........这里部分代码省略.........
示例15: test_remove_valueerror2
# 需要导入模块: from sortedcontainers import SortedList [as 别名]
# 或者: from sortedcontainers.SortedList import remove [as 别名]
def test_remove_valueerror2():
slt = SortedList(range(100), load=10)
slt.remove(100)