我正在使用 Bisect 函数在 python 中编写一个简单的集合抽象。这里的问题是,当我使用 insort 函数将数据添加到列表中时,该函数返回 None。但是当我使用 BISECT.BISECT(lst,ele) 时,该函数返回的值将是列表的索引,如果需要,可以在其中插入元素。
# Python program to implement collection abstraction
# using Bisect Algorithm
# The program would add an element into the SORTED LIST"
# which would be the input
# The program would test whether the element is in the collection
# And also, to return the element of the collection if it is there
from bisect import bisect
from bisect import insort_right
the_list = []
def add(lst,ele):
return insort_right(lst,ele)
#def test(lst,ele)
#def remove(lst,ele):
print("Enter the size of the list:")
N = int(input())
for x in range(N):
x = input("")
the_list.append(x)
the_list.sort()
print(the_list)
print("Enter the element to be added in the list")
element = input("")
add_element = add(the_list,element)
print("The element is added in collection at location:", add_element)