Insert an item into sorted list in Python

前端 未结 7 963
野趣味
野趣味 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条回答
  •  再見小時候
    2020-11-29 01:39

    This is the best way to append the list and insert values to sorted list:

     a = [] num = int(input('How many numbers: ')) for n in range(num):
         numbers = int(input('Enter values:'))
         a.append(numbers)
    
     b = sorted(a) print(b) c = int(input("enter value:")) for i in
     range(len(b)):
         if b[i] > c:
             index = i
             break d = b[:i] + [c] + b[i:] print(d)`
    

提交回复
热议问题