我目前正在编写一个用python2.6.6编写的程序。它使用的字典如下所示:
{ 'somekeystring': someobject("a name", 1, 3),
'anotherkey': someobject("another name", 2, 2),
'keythree': someobject("third name", 3, 1) }
对象具有以下属性:
name
startOrder
stopOrder
我要做的是把字典整理好。一次someobject.startOrder,一次someobject.stopOrder命令. 你知道吗
我试过了
sortedByStartOrder = sorted(mydict.iteritems(), key=lambda x: x[1].startOrder)
但这似乎不起作用。无论在上面的示例中使用startOrder还是stopOrder,列表项都按相同的顺序排序。你知道吗
有什么提示吗?你知道吗
这个例子似乎对我有用:
产生
我猜问题不在排序本身;您尝试排序的结构可能有问题。你知道吗
非常感谢您的投入!我写了一个简单的例子,像Odexios的,它与
线索是:
我浏览了我的代码,找到了一个代码段,我把两个变量都设置为0。你知道吗
您可以定义your own comparing function,它被赋予关键字
cmp
中的sorted
内置项。你知道吗相关问题 更多 >
编程相关推荐