本文整理汇总了Python中sortedcontainers.SortedList.clear方法的典型用法代码示例。如果您正苦于以下问题:Python SortedList.clear方法的具体用法?Python SortedList.clear怎么用?Python SortedList.clear使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类sortedcontainers.SortedList
的用法示例。
在下文中一共展示了SortedList.clear方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_eq
# 需要导入模块: from sortedcontainers import SortedList [as 别名]
# 或者: from sortedcontainers.SortedList import clear [as 别名]
def test_eq():
this = SortedList(range(10), load=4)
that = SortedList(range(20), load=4)
assert not (this == that)
that.clear()
that.update(range(10))
assert this == that
示例2: test_init
# 需要导入模块: from sortedcontainers import SortedList [as 别名]
# 或者: from sortedcontainers.SortedList import clear [as 别名]
def test_init():
slt = SortedList()
slt._check()
slt = SortedList(load=10000)
assert slt._load == 10000
assert slt._twice == 20000
assert slt._half == 5000
slt._check()
slt = SortedList(range(10000))
assert all(tup[0] == tup[1] for tup in zip(slt, range(10000)))
slt.clear()
assert slt._len == 0
assert slt._maxes == []
assert slt._lists == []
slt._check()
示例3: test_init
# 需要导入模块: from sortedcontainers import SortedList [as 别名]
# 或者: from sortedcontainers.SortedList import clear [as 别名]
def test_init():
slt = SortedList()
assert slt.key is None
slt._check()
slt = SortedList()
slt._reset(10000)
assert slt._load == 10000
slt._check()
slt = SortedList(range(10000))
assert all(tup[0] == tup[1] for tup in zip(slt, range(10000)))
slt.clear()
assert slt._len == 0
assert slt._maxes == []
assert slt._lists == []
slt._check()
示例4: PriorityDict
# 需要导入模块: from sortedcontainers import SortedList [as 别名]
# 或者: from sortedcontainers.SortedList import clear [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.
#.........这里部分代码省略.........
示例5: evaluateValues
# 需要导入模块: from sortedcontainers import SortedList [as 别名]
# 或者: from sortedcontainers.SortedList import clear [as 别名]
clist = [ivalue]
cstart = ipos
cend = ipos
cPass = not options.debug #and (windowHigh-windowLow >= variCutoff)
# Increase position in window towards end of the list
posInWindow += 1
evaluateValues(chrom,cstart,cend,clist,cPass)
# Clean-up for next region
chrom = nchrom
pos = npos
step = nstep
cstart,cend = None,None
cPass = not options.debug
clist = []
windowValues = []
windowValuesSorted.clear()
posInWindow = 0
else:
if len(line.strip()) == 0: continue
pos += step
value = int(line)
# Window buffer has not reached its size yet
if len(windowValues) < windowSize:
windowValues.append(value)
windowValuesSorted.add(value)
# Window buffer has reached its size
elif len(windowValues) == windowSize:
# Buffer filled but current position is not yet centered in the window,
# work with current buffer state
if (posInWindow < halfWindow):
示例6: bool
# 需要导入模块: from sortedcontainers import SortedList [as 别名]
# 或者: from sortedcontainers.SortedList import clear [as 别名]
#.........这里部分代码省略.........
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):
'''
Replace rows with the values in row_map.
'''
nodes = [node for node in self._nodes if node.value in row_map]
for node in nodes:
node.value = row_map[node.value]
self._nodes.clear()
self._nodes.update(nodes)
def __repr__(self):
return '{0!r}'.format(list(self._nodes))