我在python中有这些代码
类Tupla: def init (self,xid,prob): self.xid = xid self.prob = prob
我在列表中附加了这个类的一些对象
打印myList1
[(' X10',11,''),(' X9',11,''),(& #39; X11',11,''),(' X2',11,'')]
我尝试订购
myList1 = sorted(myList1,key = lambda Tupla:Tupla.xid,reverse = True)
打印myList1
[(' X9',11,''),(' X2',11,''),(& #39; X11',11,''),(' X10',11,'')]
我正在寻找人类排序 我需要像这样订单: [(' X2',11,''),(' X9',11,''),(' X10',11,''),(' X11',11,'')]
我怎么能这样做?
感谢
答案 0 :(得分:1)
def Tupla(xid, prob):
class _Tupla:
def __init__(self):
pass
def get_id(self):
return xid
def get_prob(self):
return prob
return _Tupla()
myList = map(lambda arg: Tupla(*arg), (('X10', 11), ('X9', 11), ('X11', 11), ('X2', 11)))
myList.sort(key = lambda obj: int(obj.get_id()[1:]))
print [(x.get_id(), x.get_prob()) for x in myList]
<强>输出:强>
[('X2', 11), ('X9', 11), ('X10', 11), ('X11', 11)]