bisec module 应用

简介: bisec module 应用

bisec:Maintain Lists in Sorted Order
The bisec

Inserting in Sorted Order
Here is a simple example in which insort

The following codes:

import bisect

values = [14, 85, 77, 26, 50, 45, 66, 79, 10, 3, 84, 77, 1]

print('Before ordered:', values)

print('New   Pos   Contents')
print('---   ---   --------')
l = []
def get_order_lst(lst):

    for i in lst:
        position = bisect.bisect(l, i)
        bisect.insort(l, i)
        print('{:3}  {:3}   '.format(i, position), l)
    return l

get_order_lst(values)

print('\nAfter ordered:', l)

The first column of the output shows the new random number. The second column shows the position where the number will be inserted into the list. The remainder of each line is the current sorted list.

Before ordered: [14, 85, 77, 26, 50, 45, 66, 79, 10, 3, 84, 77, 1]
New   Pos   Contents
---   ---   --------
 14    0    [14]
 85    1    [14, 85]
 77    1    [14, 77, 85]
 26    1    [14, 26, 77, 85]
 50    2    [14, 26, 50, 77, 85]
 45    2    [14, 26, 45, 50, 77, 85]
 66    4    [14, 26, 45, 50, 66, 77, 85]
 79    6    [14, 26, 45, 50, 66, 77, 79, 85]
 10    0    [10, 14, 26, 45, 50, 66, 77, 79, 85]
  3    0    [3, 10, 14, 26, 45, 50, 66, 77, 79, 85]
 84    9    [3, 10, 14, 26, 45, 50, 66, 77, 79, 84, 85]
 77    8    [3, 10, 14, 26, 45, 50, 66, 77, 77, 79, 84, 85]
  1    0    [1, 3, 10, 14, 26, 45, 50, 66, 77, 77, 79, 84, 85]

After ordered: [1, 3, 10, 14, 26, 45, 50, 66, 77, 77, 79, 84, 85]

This is a simple example. In fact, given the amount of data being manipulated, it might be faster to simply build the list and then sort it once. By contrast, for long lists, significant time and memory savings can be achieved using an insertion sort algorithm such as this, especially when the operation to compare two members of the list requires expensive computation.

相关文章
|
前端开发
Module理解及使用
Module理解及使用
115 0
ModuleNotFoundError: No module named ‘sentencepiece‘
ModuleNotFoundError: No module named ‘sentencepiece‘
224 0
ModuleNotFoundError: No module named 'torchstat'
ModuleNotFoundError: No module named 'torchstat'
426 0
ModuleNotFoundError: No module named 'torchstat'
|
Python
ModuleNotFoundError: No module named ‘pyexpat‘
ModuleNotFoundError: No module named ‘pyexpat‘
212 0
|
机器学习/深度学习 数据可视化 TensorFlow
解决ModuleNotFoundError: No module named ‘tensorboardX‘问题
解决ModuleNotFoundError: No module named ‘tensorboardX‘问题
1364 0
解决ModuleNotFoundError: No module named ‘tensorboardX‘问题
|
计算机视觉 C++ Python
解决ModuleNotFoundError: No module named ‘skimage‘问题
解决ModuleNotFoundError: No module named ‘skimage‘问题
2628 0
解决ModuleNotFoundError: No module named ‘skimage‘问题
ModuleNotFoundError: No module named ‘torchversion‘
ModuleNotFoundError: No module named ‘torchversion‘
166 0
|
存储 编解码 API
成功解决ModuleNotFoundError: No module named codecs
成功解决ModuleNotFoundError: No module named codecs
成功解决ModuleNotFoundError: No module named codecs
|
编解码 Python
成功解决ModuleNotFoundError: No module named 'codecs'
成功解决ModuleNotFoundError: No module named 'codecs'
10.1 modules
import codecs       # import 就是引入别人写的模块 with codecs.open('1.txt','w') as f:     pass import zhouyuyao.
675 0