本文整理汇总了Python中bisect.bisect_left方法的典型用法代码示例。如果您正苦于以下问题:Python bisect.bisect_left方法的具体用法?Python bisect.bisect_left怎么用?Python bisect.bisect_left使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类bisect
的用法示例。
在下文中一共展示了bisect.bisect_left方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: intranges_contain
# 需要导入模块: import bisect [as 别名]
# 或者: from bisect import bisect_left [as 别名]
def intranges_contain(int_, ranges):
"""Determine if `int_` falls into one of the ranges in `ranges`."""
tuple_ = _encode_range(int_, 0)
pos = bisect.bisect_left(ranges, tuple_)
# we could be immediately ahead of a tuple (start, end)
# with start < int_ <= end
if pos > 0:
left, right = _decode_range(ranges[pos-1])
if left <= int_ < right:
return True
# or we could be immediately behind a tuple (int_, end)
if pos < len(ranges):
left, _ = _decode_range(ranges[pos])
if left == int_:
return True
return False
示例2: contains
# 需要导入模块: import bisect [as 别名]
# 或者: from bisect import bisect_left [as 别名]
def contains(self, *args):
if len(args) == 1 and isinstance(args[0], Cell):
return self.contains(args[0].id())
elif len(args) == 1 and isinstance(args[0], CellId):
cell_id = args[0]
index = bisect.bisect_left(self.__cell_ids, cell_id)
if index < len(self.__cell_ids) \
and self.__cell_ids[index].range_min() <= cell_id:
return True
return index != 0 \
and self.__cell_ids[index - 1].range_max() >= cell_id
elif len(args) == 1 and isinstance(args[0], Point):
return self.contains(CellId.from_point(args[0]))
elif len(args) == 1 and isinstance(args[0], self.__class__):
cell_union = args[0]
for i in xrange(cell_union.num_cells()):
if not self.contains(cell_union.cell_id(i)):
return False
return True
else:
raise NotImplementedError()
示例3: intersects
# 需要导入模块: import bisect [as 别名]
# 或者: from bisect import bisect_left [as 别名]
def intersects(self, *args):
if len(args) == 1 and isinstance(args[0], CellId):
cell_id = args[0]
index = bisect.bisect_left(self.__cell_ids, cell_id)
if index != len(self.__cell_ids) \
and self.__cell_ids[index].range_min() <= cell_id.range_max():
return True
return index != 0 \
and self.__cell_ids[index - 1].range_max() \
>= cell_id.range_min()
elif len(args) == 1 and isinstance(args[0], CellUnion):
cell_union = args[0]
for cell_id in cell_union.__cell_ids:
if self.intersects(cell_id):
return True
return False
else:
raise NotImplementedError()
示例4: bisect_left
# 需要导入模块: import bisect [as 别名]
# 或者: from bisect import bisect_left [as 别名]
def bisect_left(self, value):
"""Return an index to insert `value` in the sorted-key list.
If the `value` is already present, the insertion point will be before
(to the left of) any existing values.
Similar to the `bisect` module in the standard library.
Runtime complexity: `O(log(n))` -- approximate.
>>> from operator import neg
>>> skl = SortedKeyList([5, 4, 3, 2, 1], key=neg)
>>> skl.bisect_left(1)
4
:param value: insertion index of value in sorted-key list
:return: index
"""
return self._bisect_key_left(self._key(value))
示例5: bisect_right
# 需要导入模块: import bisect [as 别名]
# 或者: from bisect import bisect_left [as 别名]
def bisect_right(self, value):
"""Return an index to insert `value` in the sorted-key list.
Similar to `bisect_left`, but if `value` is already present, the
insertion point with be after (to the right of) any existing values.
Similar to the `bisect` module in the standard library.
Runtime complexity: `O(log(n))` -- approximate.
>>> from operator import neg
>>> skl = SortedList([5, 4, 3, 2, 1], key=neg)
>>> skl.bisect_right(1)
5
:param value: insertion index of value in sorted-key list
:return: index
"""
return self._bisect_key_right(self._key(value))
示例6: find_longest_prefix
# 需要导入模块: import bisect [as 别名]
# 或者: from bisect import bisect_left [as 别名]
def find_longest_prefix(self, target, sep):
"""Identifies a known resource path which would contain the `target` path.
sep must be the current path separator (can vary from os.path.sep when
running under simulation).
Returns (str(Path), Path) if the prefix path is found, or (None, None) if no
such prefix exists.
"""
idx = bisect.bisect_left(self.path_strings, target)
if idx == len(self.paths):
return (None, None) # off the end
sPath, path = self.path_strings[idx], self.paths[idx]
if target == sPath :
return sPath, path
if idx > 0:
sPath, path = self.path_strings[idx-1], self.paths[idx-1]
if target.startswith(sPath+sep):
return sPath, path
return (None, None)
示例7: binary_search
# 需要导入模块: import bisect [as 别名]
# 或者: from bisect import bisect_left [as 别名]
def binary_search(ls, x, sort=False):
"""二分查找算法
>>> s = [1, 2, 3, 4, 5, 6, 10, 7]
>>> print(binary_search(s, 7, True))
:param ls: 列表。
:param x: 被查找的数。
:param sort: 是否要启动排序,False表示不启动排序,默认是不启动。
:return: 找到返回True,反之亦然
"""
if sort:
ls = sorted(ls)
v = bisect.bisect_left(ls, x)
if v != len(ls) and ls[v] == x:
return True
return False
示例8: keys
# 需要导入模块: import bisect [as 别名]
# 或者: from bisect import bisect_left [as 别名]
def keys(self, prefix=None):
if prefix is None or prefix == "" or not self._keys:
return set(self._keys)
if prefix.startswith(self._cachestr):
lo, hi = self._cachepoints
start = i = bisect_left(self._keys, prefix, lo, hi)
else:
start = i = bisect_left(self._keys, prefix)
keys = set()
if start == len(self._keys):
return keys
while self._keys[i].startswith(prefix):
keys.add(self._keys[i])
i += 1
self._cachestr = prefix
self._cachepoints = (start, i)
return keys
示例9: seek
# 需要导入模块: import bisect [as 别名]
# 或者: from bisect import bisect_left [as 别名]
def seek(self, key):
self._idx = bisect.bisect_left(self._data, (key, ""))
示例10: put
# 需要导入模块: import bisect [as 别名]
# 或者: from bisect import bisect_left [as 别名]
def put(self, key, val, **_kwargs):
if self._is_snapshot:
raise TypeError("cannot put on leveldb snapshot")
assert isinstance(key, str)
assert isinstance(val, str)
with self._lock:
idx = bisect.bisect_left(self._data, (key, ""))
if 0 <= idx < len(self._data) and self._data[idx][0] == key:
self._data[idx] = (key, val)
else:
self._data.insert(idx, (key, val))
示例11: delete
# 需要导入模块: import bisect [as 别名]
# 或者: from bisect import bisect_left [as 别名]
def delete(self, key, **_kwargs):
if self._is_snapshot:
raise TypeError("cannot delete on leveldb snapshot")
with self._lock:
idx = bisect.bisect_left(self._data, (key, ""))
if 0 <= idx < len(self._data) and self._data[idx][0] == key:
del self._data[idx]
示例12: get
# 需要导入模块: import bisect [as 别名]
# 或者: from bisect import bisect_left [as 别名]
def get(self, key, **_kwargs):
with self._lock:
idx = bisect.bisect_left(self._data, (key, ""))
if 0 <= idx < len(self._data) and self._data[idx][0] == key:
return self._data[idx][1]
return None
# pylint: disable=W0212
示例13: get_item
# 需要导入模块: import bisect [as 别名]
# 或者: from bisect import bisect_left [as 别名]
def get_item(self, i, d=None):
"""Finds out how many repeats this index implies, then picks strings."""
if i < self.offset_break:
by_bisect = bisect.bisect_left(self.offsets, (i, -1), hi=self.index_of_offset)
else:
by_bisect = bisect.bisect_left(self.offsets, (i, -1), lo=self.index_of_offset)
if by_bisect == len(self.offsets) or self.offsets[by_bisect][0] > i:
by_bisect -= 1
num = i - self.offsets[by_bisect][0]
count = self.offsets[by_bisect][1]
if count > 100 and self.content_length < 1000:
content = list(self.content)
else:
content = self.content
result = []
if count == 0:
return ''
for modulus in fastdivmod.divmod_iter(num, self.content_length):
result.append(content[modulus])
leftover = count - len(result)
if leftover:
assert leftover > 0
result.extend([content[0]] * leftover)
# smallest place value ends up on the right
return ''.join(result[::-1])
示例14: _update_tag_positions
# 需要导入模块: import bisect [as 别名]
# 或者: from bisect import bisect_left [as 别名]
def _update_tag_positions(self, rule):
"""
Update _tag_positions to reflect the changes to tags that are
made by *rule*.
"""
# Update the tag index.
for pos in self._positions_by_rule[rule]:
# Delete the old tag.
old_tag_positions = self._tag_positions[rule.original_tag]
old_index = bisect.bisect_left(old_tag_positions, pos)
del old_tag_positions[old_index]
# Insert the new tag.
new_tag_positions = self._tag_positions[rule.replacement_tag]
bisect.insort_left(new_tag_positions, pos)
示例15: uts46_remap
# 需要导入模块: import bisect [as 别名]
# 或者: from bisect import bisect_left [as 别名]
def uts46_remap(domain, std3_rules=True, transitional=False):
"""Re-map the characters in the string according to UTS46 processing."""
from .uts46data import uts46data
output = u""
try:
for pos, char in enumerate(domain):
code_point = ord(char)
uts46row = uts46data[code_point if code_point < 256 else
bisect.bisect_left(uts46data, (code_point, "Z")) - 1]
status = uts46row[1]
replacement = uts46row[2] if len(uts46row) == 3 else None
if (status == "V" or
(status == "D" and not transitional) or
(status == "3" and not std3_rules and replacement is None)):
output += char
elif replacement is not None and (status == "M" or
(status == "3" and not std3_rules) or
(status == "D" and transitional)):
output += replacement
elif status != "I":
raise IndexError()
return unicodedata.normalize("NFC", output)
except IndexError:
raise InvalidCodepoint(
"Codepoint {0} not allowed at position {1} in {2}".format(
_unot(code_point), pos + 1, repr(domain)))