我想知道是否有一种有效的方法来对数字列表进行排序,而不重复其中任何一个,并且通过修改任何元素,其余的都会重新排序
例如
list = [1,2,3,4]
案例1:如果我向列表中添加另一个数字2,则列表中的数字2和下一个数字应被推送到下一个值。
[1,2,3,4] <- new 2 [1,2,3("old" 2),4("old" 3),5("old" 4)]
如果我在任何位置删除一个数字,则相同
案例1:我将数字4修改为2,列表必须->;“旧”2应转换为3,“旧”应转换为4,就像移动/推送数字一样
对sumary来说,插入一个数字的一些有效方法,这个数字将占据其值的位置,将另一个值向后或向前移动
我希望这是清楚的
编辑以澄清
这些数字与某些事情有关。本例中的产品,但现在无关紧要。 因此:
product1: have this value 1 produc1{id: 1}
product2: have this value 2 produc2{id: 2}
product3: have this value 3 produc3{id: 3}
product4: have this value 4 produc4{id: 4}
所以,如果我想更改订单,我想将例如product4修改为produc的2位置
product1: have this value 1 produc1{id: 1}
product2: have this value 2 produc2{id: 2}
product3: have this value 3 produc3{id: 3}
product4: have this value 4 produc4{id: 4} # I change this 4 for a 2
然后它应该按id排序
product1: have this value 1 produc1{id: 1}
product4: have this value 2 produc4{id: 2} # this was a 4 modified to 2 (manualy)
product2: have this value 3 produc2{id: 3} # this was a 2 converted to 3
product3: have this value 4 produc3{id: 4} # this was a 3 converted to 4
另一个案例
product1: have this value 1 produc1{id: 1}
product2: have this value 2 produc2{id: 2} # delete this product2
product3: have this value 3 produc3{id: 3}
product4: have this value 4 produc4{id: 4}
然后:
product1: have this value 1 produc1{id: 1}
product2: have this value 2 produc3{id: 2} # this id was 3
product3: have this value 3 produc4{id: 3} # this id was 4
关于得到一个索引号,我会以某种方式阻止它,但现在对此不感兴趣
我希望这一点现在已经清楚了
见下文-使用bisect
相关问题 更多 >
编程相关推荐