Python 纸浆分选结果
Python PULP sorting results
出于线性优化的原因,我正在使用 PULP。一切正常,我得到了结果,我正在打印每个结果:
for i in range (0, len(opt_P)):
print (opt_P[i].name + " = " + str(opt_P[i].varValue))
输出如下所示:
0 = 20.0
1 = 20.0
10 = 1200.0
11 = 1200.0
2 = 20.0
3 = 20.0
4 = 20.0
5 = 20.0
6 = 1200.0
7 = 1200.0
8 = 45.895992
9 = 1200.0
我想用 opt_P.sort()
函数对结果进行排序,但出现错误:
'<' not supported between instances of 'LpVariable' and 'LpVariable'
我也试过这个 opt_P.sort(key=lambda s: s[1])
还有这个:opt_P.sort(key=lambda s: s["name"])
但出现错误:
'LpVariable' object is not subscriptable
你能支持我解决这个问题吗?
您可以指定要使用的按键功能。
示例:
>>> vars_list
[x_2, x_3, x_0, x_1, x_9, x_8, x_6, x_4, x_7, x_5]
>>> vars_list.sort(key=lambda x:x.name)
>>> vars_list
[x_0, x_1, x_2, x_3, x_4, x_5, x_6, x_7, x_8, x_9]
或者如果您想将它们排序为整数
>>> vars_list.sort(key=lambda x:int(x.name))
出于线性优化的原因,我正在使用 PULP。一切正常,我得到了结果,我正在打印每个结果:
for i in range (0, len(opt_P)):
print (opt_P[i].name + " = " + str(opt_P[i].varValue))
输出如下所示:
0 = 20.0
1 = 20.0
10 = 1200.0
11 = 1200.0
2 = 20.0
3 = 20.0
4 = 20.0
5 = 20.0
6 = 1200.0
7 = 1200.0
8 = 45.895992
9 = 1200.0
我想用 opt_P.sort()
函数对结果进行排序,但出现错误:
'<' not supported between instances of 'LpVariable' and 'LpVariable'
我也试过这个 opt_P.sort(key=lambda s: s[1])
还有这个:opt_P.sort(key=lambda s: s["name"])
但出现错误:
'LpVariable' object is not subscriptable
你能支持我解决这个问题吗?
您可以指定要使用的按键功能。
示例:
>>> vars_list
[x_2, x_3, x_0, x_1, x_9, x_8, x_6, x_4, x_7, x_5]
>>> vars_list.sort(key=lambda x:x.name)
>>> vars_list
[x_0, x_1, x_2, x_3, x_4, x_5, x_6, x_7, x_8, x_9]
或者如果您想将它们排序为整数
>>> vars_list.sort(key=lambda x:int(x.name))