如果我想基于多个属性来排序列表,除了将这些属性形成为数字然后使用OrderBy
之外,我看不到一种实现此目的的好方法,例如:
int someArbitraryNumber = 1000;
List<Tuple<int, int>> test = new List<Tuple<int, int>>() {
new Tuple<int, int>(1, 3),
new Tuple<int, int>(1, 4),
new Tuple<int, int>(1, 5),
new Tuple<int, int>(2, 3),
new Tuple<int, int>(9, 15)
};
var orderedTest = test
.OrderBy(x => x.Item1 + x.Item2 * someArbitraryNumber);
在这里,我希望得到的结果列表为:
(1, 3),
(2, 3),
(1, 4),
(1, 5),
(9, 15)
也就是说,首先由第二项排序,然后由第一项排序。
答案 0 :(得分:1)
使用import numpy as np
import timeit
def f_with_lambda():
a = np.array(range(5))
b = np.array(range(5))
A,B = np.meshgrid(a,b)
rst = list(map(lambda x,y : x+y , A, B))
return np.array(rst)
def f_with_for():
a = range(5)
b = np.array(range(5))
rst = [b+x for x in a]
return np.array(rst)
lambda_rst = f_with_lambda()
for_rst = f_with_for()
if __name__ == '__main__':
print(timeit.timeit("f_with_lambda()",setup = "from __main__ import f_with_lambda",number = 10000))
print(timeit.timeit("f_with_for()",setup = "from __main__ import f_with_for",number = 10000))
:
ThenBy()
int someArbitraryNumber = 10000; // Not used anymore
List<Tuple<int, int>> test = new List<Tuple<int, int>>() {
new Tuple<int, int>(1, 3),
new Tuple<int, int>(1, 4),
new Tuple<int, int>(1, 5),
new Tuple<int, int>(2, 3),
new Tuple<int, int>(9, 15)
};
var orderedTest = test.OrderBy(x => x.Item2).ThenBy(x => x.Item1);
var
而不是(int, int)
),并且可以为项目使用名称Tuple<int, int>