Insert an item into sorted list in Python

前端 未结 7 944
野趣味
野趣味 2020-11-29 01:06

I\'m creating a class where one of the methods inserts a new item into the sorted list. The item is inserted in the corrected (sorted) position in the sorted list. I\'m not

7条回答
  •  萌比男神i
    2020-11-29 01:31

    Hint 1: You might want to study the Python code in the bisect module.

    Hint 2: Slicing can be used for list insertion:

    >>> s = ['a', 'b', 'd', 'e']
    >>> s[2:2] = ['c']
    >>> s
    ['a', 'b', 'c', 'd', 'e']
    

提交回复
热议问题