【发布时间】:2020-08-31 20:24:10
【问题描述】:
例如,如果我们有l=[[0,1,2],[3,4,5,6],[4,5,6]],因为列表[3,10,11] 更大,我们可以排序并返回结果为l=[[0,1,2], [4,5,6], [3,4,5,6]]
现在,如果我们将 [1] 附加到列表中,它会给出==> [[1],[0,1,2],[4,5,6],[3,4,5,6]]
基本上我的意思是,就像在bisect.insort(list,element) 中一样,它通过使用排序方法自动将元素插入到正确的位置,但在这里我喜欢根据长度插入元素。
如果我解释得更清楚,那么如果你是 c++ 用户,那么
struct cmp {
bool operator() (const pair<int, int> &a,
const pair<int, int> &b) const {
int lena = a.second - a.first + 1;
int lenb = b.second - b.first + 1;
if (lena == lenb) return a.first < b.first;
return lena > lenb;
}
};
set<pair<int, int>, cmp> segs;
我想要这种类型的东西在 python
【问题讨论】:
标签: python sorting data-structures