Python 3.4 - Bisect Module

The Bisect Module "provides support for maintaining a list in sorted order without having to sort the list after each insertion. For long lists of items with expensive comparison operations, this can be an improvement over the more common approach."

import bisect


mylist = [1,3,4,4,4,6,7]



print("the rightmost index is : ", end= "")
print(bisect.bisect(mylist, 4))

print("the rightmost index is : ", end= "")
print(bisect.bisect_left(mylist, 4))

mylist2 = [1,3,4,4,4,6,7]

bisect.insort(mylist2, 5, 0, 5)

print("the list is now:")

for i in range(0, 8):
    print(mylist2[i], end = "")

print('\r')

bisect.insort_left(mylist2, 2)

print("the list is now:")

for i in range(0, 9):
    print(mylist2[i], end = "")

 /Users/mattfassnacht/PycharmProjects/untitled6/venv/bin/python /Users/mattfassnacht/PycharmProjects/untitled6/functions.py
the rightmost index is : 5
the rightmost index is : 2
the list is now:
13444567
the list is now:
123444567
Process finished with exit code

 https://docs.python.org/3.4/library/bisect.html

https://www.youtube.com/watch?v=7eiXnQY2xmg

Comments

Popular posts from this blog

Python 3.4 - Caesar Cipher

UML - Use Case Diagram