本文整理匯總了Python中sortedcontainers.SortedList.index方法的典型用法代碼示例。如果您正苦於以下問題:Python SortedList.index方法的具體用法?Python SortedList.index怎麽用?Python SortedList.index使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類sortedcontainers.SortedList
的用法示例。
在下文中一共展示了SortedList.index方法的14個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: test_index
# 需要導入模塊: from sortedcontainers import SortedList [as 別名]
# 或者: from sortedcontainers.SortedList import index [as 別名]
def test_index():
slt = SortedList(range(100), load=17)
for val in range(100):
assert val == slt.index(val)
assert slt.index(99, 0, 1000) == 99
slt = SortedList((0 for rpt in range(100)), load=17)
for start in range(100):
for stop in range(start, 100):
assert slt.index(0, start, stop + 1) == start
for start in range(100):
assert slt.index(0, -(100 - start)) == start
assert slt.index(0, -1000) == 0
示例2: test_index_valueerror6
# 需要導入模塊: from sortedcontainers import SortedList [as 別名]
# 或者: from sortedcontainers.SortedList import index [as 別名]
def test_index_valueerror6():
slt = SortedList(range(10), load=4)
slt.index(3, 5)
示例3: test_index_valueerror5
# 需要導入模塊: from sortedcontainers import SortedList [as 別名]
# 或者: from sortedcontainers.SortedList import index [as 別名]
def test_index_valueerror5():
slt = SortedList()
slt.index(1)
示例4: test_index_valueerror4
# 需要導入模塊: from sortedcontainers import SortedList [as 別名]
# 或者: from sortedcontainers.SortedList import index [as 別名]
def test_index_valueerror4():
slt = SortedList([0] * 10, load=4)
slt.index(1)
示例5: test_index_valueerror2
# 需要導入模塊: from sortedcontainers import SortedList [as 別名]
# 或者: from sortedcontainers.SortedList import index [as 別名]
def test_index_valueerror2():
slt = SortedList([0] * 10, load=4)
slt.index(0, 0, -10)
示例6: test_index_valueerror7
# 需要導入模塊: from sortedcontainers import SortedList [as 別名]
# 或者: from sortedcontainers.SortedList import index [as 別名]
def test_index_valueerror7():
slt = SortedList([0] * 10 + [2] * 10, load=4)
slt.index(1, 0, 10)
示例7: PriorityDict
# 需要導入模塊: from sortedcontainers import SortedList [as 別名]
# 或者: from sortedcontainers.SortedList import index [as 別名]
#.........這裏部分代碼省略.........
Create a new dictionary with keys from `iterable` and values set to
`value`. The default *value* is 0.
"""
return PriorityDict((key, value) for key in iterable)
def get(self, key, default=None):
"""
Return the value for *key* if *key* is in the dictionary, else
*default*. If *default* is not given, it defaults to ``None``,
so that this method never raises a KeyError.
"""
return self._dict.get(key, default)
def has_key(self, key):
"""Return True if and only in *key* is in the dictionary."""
return key in self._dict
def pop(self, key, default=_NotGiven):
"""
If *key* is in the dictionary, remove it and return its value,
else return *default*. If *default* is not given and *key* is not in
the dictionary, a KeyError is raised.
"""
if key in self._dict:
value = self._dict[key]
self._list.remove((value, key))
return self._dict.pop(key)
else:
if default == _NotGiven:
raise KeyError
else:
return default
def popitem(self, index=-1):
"""
Remove and return item at *index* (default: -1). Raises IndexError if
dict is empty or index is out of range. Negative indices are supported
as for slice indices.
"""
value, key = self._list.pop(index)
del self._dict[key]
return key, value
def setdefault(self, key, default=0):
"""
If *key* is in the dictionary, return its value. If not, insert *key*
with a value of *default* and return *default*. *default* defaults to
``0``.
"""
if key in self._dict:
return self._dict[key]
else:
self._dict[key] = default
self._list.add((default, key))
return default
def elements(self):
"""
Return an iterator over elements repeating each as many times as its
count. Elements are returned in value sort-order. If an element’s count
is less than one, elements() will ignore it.
"""
values = (repeat(key, value) for value, key in self._list)
return chain.from_iterable(values)
def most_common(self, count=None):
示例8: test_index_valueerror7
# 需要導入模塊: from sortedcontainers import SortedList [as 別名]
# 或者: from sortedcontainers.SortedList import index [as 別名]
def test_index_valueerror7():
slt = SortedList([0] * 10 + [2] * 10)
slt._reset(4)
with pytest.raises(ValueError):
slt.index(1, 0, 10)
示例9: stress_index2
# 需要導入模塊: from sortedcontainers import SortedList [as 別名]
# 或者: from sortedcontainers.SortedList import index [as 別名]
def stress_index2(slt):
values = list(slt)[:3] * 200
slt = SortedList(values)
for idx, val in enumerate(slt):
assert slt.index(val, idx) == idx
示例10: test_index_valueerror6
# 需要導入模塊: from sortedcontainers import SortedList [as 別名]
# 或者: from sortedcontainers.SortedList import index [as 別名]
def test_index_valueerror6():
slt = SortedList(range(10))
slt._reset(4)
with pytest.raises(ValueError):
slt.index(3, 5)
示例11: test_index_valueerror5
# 需要導入模塊: from sortedcontainers import SortedList [as 別名]
# 或者: from sortedcontainers.SortedList import index [as 別名]
def test_index_valueerror5():
slt = SortedList()
with pytest.raises(ValueError):
slt.index(1)
示例12: test_index_valueerror3
# 需要導入模塊: from sortedcontainers import SortedList [as 別名]
# 或者: from sortedcontainers.SortedList import index [as 別名]
def test_index_valueerror3():
slt = SortedList([0] * 10)
slt._reset(4)
with pytest.raises(ValueError):
slt.index(0, 7, 3)
示例13: prev_workspace_for_current_output_num
# 需要導入模塊: from sortedcontainers import SortedList [as 別名]
# 或者: from sortedcontainers.SortedList import index [as 別名]
def prev_workspace_for_current_output_num():
current_workspace, output_workspaces = workspaces_for_current_output()
sorted_workspaces = SortedList(
workspace["num"] for workspace in output_workspaces)
current_num = sorted_workspaces.index(current_workspace["num"])
return sorted_workspaces[(current_num - 1) % len(sorted_workspaces)]
示例14: SweepLine
# 需要導入模塊: from sortedcontainers import SortedList [as 別名]
# 或者: from sortedcontainers.SortedList import index [as 別名]
class SweepLine(object):
'''
This class represents the vertical sweep line which sweeps over
the set of segments in the Bentley-Ottmann algorithm.
At any moment, it contains a sorted list of all the
ComparableSegments which intersect with the sweep line in its
current position.
Note that if 2 segments s1 and s2 are overlapping, you cannot
assume anything about their order in the sorted queue, as
s1 < s2 and s1 > s2 are both false
Such sorted list would usually rely on a balanced binary search
tree data structure in order to have O(log(N)) insertion,
deletion and swapping.
Instead, I chose to use a SortedList of Grant Jenks' SortedContainers
module, which has several advantages that you can discover by browsing
its page. It allows O(log(N)) insertion, deletion and swapping, and I
find it to be faster in practice.
'''
def __init__(self):
'''
Initializes an empty sweep line.
'''
self.l = SortedList()
def isEmpty(self):
'''
Returns true if and only if the sweep line is empty.
'''
return len(self.l) == 0
def addSegment(self, seg):
'''
Adds seg to the sweep line.
'''
ComparableSegment.currentX = seg.x1
self.l.add(seg)
def removeSegment(self, seg):
'''
Removes seg from the sweep line.
'''
self.l.remove(seg)
def belowSegments(self, seg):
'''
Returns a list containing :
- The highest segment s_below contained in the sweep line
such as s_below.isBelow(seg)
- All the segments s contained before s_below in the sweep
line but such as s.isBelow(s_below) is false, (i.e. which have the
same y-coordinate at ComparableSegment.currentX and gradient).
'''
res = []
# i = index of seg
i = self.l.index(seg)
# Passes segments which have same y-coordinate and gradient
# to find s_below
while i-1 >= 0:
prev = self.l[i-1]
i -= 1
if prev.isBelow(seg):
res.append(prev)
break
# Appends all the segments which have same y-coordinate and
# gradient as s_below
while i-1 >= 0:
prev = self.l[i-1]
if prev.isBelow(res[0]):
break
res.append(prev)
i -= 1
return res
def aboveSegments(self, seg):
'''
Returns a list containing :
- The lowest segment s_above contained in the sweep line
such as seg < s_above
- All the segments s contained after s_above in the sweep
line but such as s_below < s is false, (i.e. which have the
same y-coordinate at ComparableSegment.currentX and gradient).
'''
res = []
# i = index of seg
i = self.l.index(seg)
# Passes segments which have same y-coordinate and gradient
# to find s_above
while i+1 < len(self.l):
succ = self.l[i+1]
i += 1
if seg.isBelow(succ):
res.append(succ)
break
# Appends all the segments which have same y-coordinate and
# gradient as s_above
#.........這裏部分代碼省略.........