本文整理汇总了Python中sortedcontainers.SortedList.update方法的典型用法代码示例。如果您正苦于以下问题:Python SortedList.update方法的具体用法?Python SortedList.update怎么用?Python SortedList.update使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类sortedcontainers.SortedList
的用法示例。
在下文中一共展示了SortedList.update方法的13个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_irange
# 需要导入模块: from sortedcontainers import SortedList [as 别名]
# 或者: from sortedcontainers.SortedList import update [as 别名]
def test_irange():
sl = SortedList(load=7)
assert [] == list(sl.irange())
values = list(range(53))
sl.update(values)
for start in range(53):
for end in range(start, 53):
assert list(sl.irange(start, end)) == values[start:(end + 1)]
assert list(sl.irange(start, end, reverse=True)) == values[start:(end + 1)][::-1]
for start in range(53):
for end in range(start, 53):
assert list(range(start, end)) == list(sl.irange(start, end, (True, False)))
for start in range(53):
for end in range(start, 53):
assert list(range(start + 1, end + 1)) == list(sl.irange(start, end, (False, True)))
for start in range(53):
for end in range(start, 53):
assert list(range(start + 1, end)) == list(sl.irange(start, end, (False, False)))
for start in range(53):
assert list(range(start, 53)) == list(sl.irange(start))
for end in range(53):
assert list(range(0, end)) == list(sl.irange(None, end, (True, False)))
assert values == list(sl.irange(inclusive=(False, False)))
assert [] == list(sl.irange(53))
assert values == list(sl.irange(None, 53, (True, False)))
示例2: test_eq
# 需要导入模块: from sortedcontainers import SortedList [as 别名]
# 或者: from sortedcontainers.SortedList import update [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
示例3: collect_matches
# 需要导入模块: from sortedcontainers import SortedList [as 别名]
# 或者: from sortedcontainers.SortedList import update [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)
示例4: __init__
# 需要导入模块: from sortedcontainers import SortedList [as 别名]
# 或者: from sortedcontainers.SortedList import update [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
示例5: test_bisect_right
# 需要导入模块: from sortedcontainers import SortedList [as 别名]
# 或者: from sortedcontainers.SortedList import update [as 别名]
def test_bisect_right():
slt = SortedList()
assert slt.bisect_right(10) == 0
slt = SortedList(range(100), load=17)
slt.update(range(100))
slt._check()
assert slt.bisect_right(10) == 22
assert slt.bisect_right(200) == 200
示例6: test_bisect_left
# 需要导入模块: from sortedcontainers import SortedList [as 别名]
# 或者: from sortedcontainers.SortedList import update [as 别名]
def test_bisect_left():
slt = SortedList()
assert slt.bisect_left(0) == 0
slt = SortedList(range(100), load=17)
slt.update(range(100))
slt._check()
assert slt.bisect_left(50) == 100
assert slt.bisect_left(200) == 200
示例7: test_bisect
# 需要导入模块: from sortedcontainers import SortedList [as 别名]
# 或者: from sortedcontainers.SortedList import update [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
示例8: test_update
# 需要导入模块: from sortedcontainers import SortedList [as 别名]
# 或者: from sortedcontainers.SortedList import update [as 别名]
def test_update():
slt = SortedList()
slt.update(range(1000))
assert all(tup[0] == tup[1] for tup in zip(slt, list(range(1000))))
assert len(slt) == 1000
slt._check()
slt.update(range(10000))
assert len(slt) == 11000
slt._check()
示例9: test_contains
# 需要导入模块: from sortedcontainers import SortedList [as 别名]
# 或者: from sortedcontainers.SortedList import update [as 别名]
def test_contains():
slt = SortedList()
assert 0 not in slt
slt.update(range(10000))
for val in range(10000):
assert val in slt
assert 10000 not in slt
slt._check()
示例10: test_islice
# 需要导入模块: from sortedcontainers import SortedList [as 别名]
# 或者: from sortedcontainers.SortedList import update [as 别名]
def test_islice():
sl = SortedList(load=7)
assert [] == list(sl.islice())
values = list(range(53))
sl.update(values)
for start in range(53):
for stop in range(53):
assert list(sl.islice(start, stop)) == values[start:stop]
for start in range(53):
for stop in range(53):
assert list(sl.islice(start, stop, reverse=True)) == values[start:stop][::-1]
for start in range(53):
assert list(sl.islice(start=start)) == values[start:]
assert list(sl.islice(start=start, reverse=True)) == values[start:][::-1]
for stop in range(53):
assert list(sl.islice(stop=stop)) == values[:stop]
assert list(sl.islice(stop=stop, reverse=True)) == values[:stop][::-1]
示例11: __init__
# 需要导入模块: from sortedcontainers import SortedList [as 别名]
# 或者: from sortedcontainers.SortedList import update [as 别名]
class Rational:
def __init__(self):
self.numerator = SortedList()
self.denominator = SortedList()
def multiply_by(self,f):
self.numerator.update(primefac.primefac(f))
def divide_by(self,d):
self.denominator.update(primefac.primefac(d))
def value(self):
if len(self.numerator) == 0 or len(self.denominator) == 0:
return None
numerator_index = 0
denominator_index = 0
while numerator_index < len(self.numerator) and denominator_index < len(self.denominator):
if self.numerator[numerator_index] == self.denominator[denominator_index]:
del self.numerator[numerator_index]
del self.denominator[denominator_index]
elif self.numerator[numerator_index] < self.denominator[denominator_index]:
numerator_index += 1
else:
denominator_index += 1
self.numerator.add(1)
self.denominator.add(1)
num_product = reduce(lambda x, y: mpfr(x)*y, self.numerator)
den_product = reduce(lambda x, y: mpfr(x)*y, self.denominator)
if num_product <= 0 or den_product <= 0:
return 0
val = num_product/den_product
if val > 1:
return 1
return val
示例12: test_update
# 需要导入模块: from sortedcontainers import SortedList [as 别名]
# 或者: from sortedcontainers.SortedList import update [as 别名]
def test_update():
slt = SortedList()
slt.update(range(1000))
assert len(slt) == 1000
slt._check()
slt.update(range(100))
assert len(slt) == 1100
slt._check()
slt.update(range(10000))
assert len(slt) == 11100
slt._check()
values = sorted(chain(range(1000), range(100), range(10000)))
assert all(tup[0] == tup[1] for tup in zip(slt, values))
示例13: bool
# 需要导入模块: from sortedcontainers import SortedList [as 别名]
# 或者: from sortedcontainers.SortedList import update [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))