本文整理汇总了Python中bisect.html方法的典型用法代码示例。如果您正苦于以下问题:Python bisect.html方法的具体用法?Python bisect.html怎么用?Python bisect.html使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类bisect
的用法示例。
在下文中一共展示了bisect.html方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __init__
# 需要导入模块: import bisect [as 别名]
# 或者: from bisect import html [as 别名]
def __init__(self, name, buckets, verbose_pretty_print=False):
"""Initializes the histogram with the given ranges.
Args:
name: String name of this histogram.
buckets: The ranges the histogram counts over. This is a list of values,
where each value is the inclusive lower bound of the range. An extra
range will be implicitly defined which spans from negative infinity
to the lowest given lower bound. The highest given lower bound
defines a range spaning to positive infinity. This way any value will
be included in the histogram counts. For example, if `buckets` is
[4, 6, 10] the histogram will have ranges
[-inf, 4), [4, 6), [6, 10), [10, inf).
verbose_pretty_print: If True, self.pretty_print will print the count for
every bucket. If False, only buckets with positive counts will be
printed.
"""
super(Histogram, self).__init__(name)
# List of inclusive lowest values in each bucket.
self.buckets = [float('-inf')] + sorted(set(buckets))
self.counters = dict((bucket_lower, 0) for bucket_lower in self.buckets)
self.verbose_pretty_print = verbose_pretty_print
# https://docs.python.org/2/library/bisect.html#searching-sorted-lists
示例2: bisect_right
# 需要导入模块: import bisect [as 别名]
# 或者: from bisect import html [as 别名]
def bisect_right(sorted_collection, item, lo=0, hi=None):
"""
Locates the first element in a sorted array that is larger than a given value.
It has the same interface as
https://docs.python.org/3/library/bisect.html#bisect.bisect_right .
:param sorted_collection: some ascending sorted collection with comparable items
:param item: item to bisect
:param lo: lowest index to consider (as in sorted_collection[lo:hi])
:param hi: past the highest index to consider (as in sorted_collection[lo:hi])
:return: index i such that all values in sorted_collection[lo:i] are <= item and
all values in sorted_collection[i:hi] are > item.
Examples:
>>> bisect_right([0, 5, 7, 10, 15], 0)
1
>>> bisect_right([0, 5, 7, 10, 15], 15)
5
>>> bisect_right([0, 5, 7, 10, 15], 6)
2
>>> bisect_right([0, 5, 7, 10, 15], 15, 1, 3)
3
>>> bisect_right([0, 5, 7, 10, 15], 6, 2)
2
"""
if hi is None:
hi = len(sorted_collection)
while lo < hi:
mid = (lo + hi) // 2
if sorted_collection[mid] <= item:
lo = mid + 1
else:
hi = mid
return lo
示例3: insort_left
# 需要导入模块: import bisect [as 别名]
# 或者: from bisect import html [as 别名]
def insort_left(sorted_collection, item, lo=0, hi=None):
"""
Inserts a given value into a sorted array before other values with the same value.
It has the same interface as
https://docs.python.org/3/library/bisect.html#bisect.insort_left .
:param sorted_collection: some ascending sorted collection with comparable items
:param item: item to insert
:param lo: lowest index to consider (as in sorted_collection[lo:hi])
:param hi: past the highest index to consider (as in sorted_collection[lo:hi])
Examples:
>>> sorted_collection = [0, 5, 7, 10, 15]
>>> insort_left(sorted_collection, 6)
>>> sorted_collection
[0, 5, 6, 7, 10, 15]
>>> sorted_collection = [(0, 0), (5, 5), (7, 7), (10, 10), (15, 15)]
>>> item = (5, 5)
>>> insort_left(sorted_collection, item)
>>> sorted_collection
[(0, 0), (5, 5), (5, 5), (7, 7), (10, 10), (15, 15)]
>>> item is sorted_collection[1]
True
>>> item is sorted_collection[2]
False
>>> sorted_collection = [0, 5, 7, 10, 15]
>>> insort_left(sorted_collection, 20)
>>> sorted_collection
[0, 5, 7, 10, 15, 20]
>>> sorted_collection = [0, 5, 7, 10, 15]
>>> insort_left(sorted_collection, 15, 1, 3)
>>> sorted_collection
[0, 5, 7, 15, 10, 15]
"""
sorted_collection.insert(bisect_left(sorted_collection, item, lo, hi), item)
示例4: insort_right
# 需要导入模块: import bisect [as 别名]
# 或者: from bisect import html [as 别名]
def insort_right(sorted_collection, item, lo=0, hi=None):
"""
Inserts a given value into a sorted array after other values with the same value.
It has the same interface as
https://docs.python.org/3/library/bisect.html#bisect.insort_right .
:param sorted_collection: some ascending sorted collection with comparable items
:param item: item to insert
:param lo: lowest index to consider (as in sorted_collection[lo:hi])
:param hi: past the highest index to consider (as in sorted_collection[lo:hi])
Examples:
>>> sorted_collection = [0, 5, 7, 10, 15]
>>> insort_right(sorted_collection, 6)
>>> sorted_collection
[0, 5, 6, 7, 10, 15]
>>> sorted_collection = [(0, 0), (5, 5), (7, 7), (10, 10), (15, 15)]
>>> item = (5, 5)
>>> insort_right(sorted_collection, item)
>>> sorted_collection
[(0, 0), (5, 5), (5, 5), (7, 7), (10, 10), (15, 15)]
>>> item is sorted_collection[1]
False
>>> item is sorted_collection[2]
True
>>> sorted_collection = [0, 5, 7, 10, 15]
>>> insort_right(sorted_collection, 20)
>>> sorted_collection
[0, 5, 7, 10, 15, 20]
>>> sorted_collection = [0, 5, 7, 10, 15]
>>> insort_right(sorted_collection, 15, 1, 3)
>>> sorted_collection
[0, 5, 7, 15, 10, 15]
"""
sorted_collection.insert(bisect_right(sorted_collection, item, lo, hi), item)
示例5: get_machine
# 需要导入模块: import bisect [as 别名]
# 或者: from bisect import html [as 别名]
def get_machine(self,key):
'''Returns the number of the machine which key gets sent to.'''
h = my_hash(key)
# edge case where we cycle past hash value of 1 and back to 0.
if h > self.hash_tuples[-1][2]:
return self.hash_tuples[0][0]
hash_values = map(lambda x: x[2],self.hash_tuples)
# bitsect is used to find the index that we want to insert h
# the index will be used same as finding the first clockwise node
# https://docs.python.org/2/library/bisect.html
index = bisect.bisect_left(hash_values,h)
return self.hash_tuples[index][0]
示例6: spookyrating
# 需要导入模块: import bisect [as 别名]
# 或者: from bisect import html [as 别名]
def spookyrating(self, ctx: commands.Context, who: discord.Member = None) -> None:
"""
Calculates the spooky rating of someone.
Any user will always yield the same result, no matter who calls the command
"""
if who is None:
who = ctx.author
# This ensures that the same result over multiple runtimes
self.local_random.seed(who.id)
spooky_percent = self.local_random.randint(1, 101)
# We need the -1 due to how bisect returns the point
# see the documentation for further detail
# https://docs.python.org/3/library/bisect.html#bisect.bisect
index = bisect.bisect(SPOOKY_DATA, (spooky_percent,)) - 1
_, data = SPOOKY_DATA[index]
embed = discord.Embed(
title=data['title'],
description=f'{who} scored {spooky_percent}%!',
color=Colours.orange
)
embed.add_field(
name='A whisper from Satan',
value=data['text']
)
embed.set_thumbnail(
url=data['image']
)
await ctx.send(embed=embed)
示例7: bisect_left
# 需要导入模块: import bisect [as 别名]
# 或者: from bisect import html [as 别名]
def bisect_left(sorted_collection, item, lo=0, hi=None):
"""
Locates the first element in a sorted array that is larger or equal to a given
value.
It has the same interface as
https://docs.python.org/3/library/bisect.html#bisect.bisect_left .
:param sorted_collection: some ascending sorted collection with comparable items
:param item: item to bisect
:param lo: lowest index to consider (as in sorted_collection[lo:hi])
:param hi: past the highest index to consider (as in sorted_collection[lo:hi])
:return: index i such that all values in sorted_collection[lo:i] are < item and all
values in sorted_collection[i:hi] are >= item.
Examples:
>>> bisect_left([0, 5, 7, 10, 15], 0)
0
>>> bisect_left([0, 5, 7, 10, 15], 6)
2
>>> bisect_left([0, 5, 7, 10, 15], 20)
5
>>> bisect_left([0, 5, 7, 10, 15], 15, 1, 3)
3
>>> bisect_left([0, 5, 7, 10, 15], 6, 2)
2
"""
if hi is None:
hi = len(sorted_collection)
while lo < hi:
mid = (lo + hi) // 2
if sorted_collection[mid] < item:
lo = mid + 1
else:
hi = mid
return lo