我有我的代码设置,所以它得到一个API,并输出到一个.txt文件中的某些变量,但我希望它自己的顺序,而不是按字母顺序或任何东西,但在优先级顺序,所以如果其中一个变量是MVP,它将被放置在较高的.txt作为VIP和80将高于20 这是一个.txt的示例
b-88698@alt.com : thegiant20** [10]
e-b77b2@alt.com : Rancher** [1] [VIP]
5-c1eb6@alt.com : dogdad** [4] [VIP]
1-15a1f@alt.com : mb1mi** [0]
5-cd91b@alt.com : shalexa** [18] [MVP_PLUS]
但我要订的是这样的
5-cd91b@alt.com : shalexa** [18] [MVP_PLUS]
5-c1eb6@alt.com : dogdad** [4] [VIP]
e-b77b2@alt.com : Rancher** [1] [VIP]
b-88698@alt.com : thegiant20** [10]
1-15a1f@alt.com : mb1mi** [0]
有没有什么我可以投入,可以轻松实现这一点
list
的sort
方法有一个key
参数,您可以在其中提供一个函数来确定排序顺序。此函数必须找到它是MVP还是VIP,并且必须找到方括号中的值比较是用一个元组来完成的。第一个值表示我们是否有MVP。相应的值为
True
或False
(或视为整数1
或0
)。第二个值与VIP相同。最后一个值是括号中转换为整数的数字结果
在
return
之前的sortkey
函数中添加以下行,您将看到为其生成并用于排序的值和键输出
相关问题 更多 >
编程相关推荐