python list 比较大小问题

有个a=[[a1, b1, c1], [a2, b2, c2], [a3, b3, c3], [a4, b4, c4]]
要按c1, c2, c3, c4的值重新排a中四个元素的位置
应该怎么做啊?
谢谢大虾们~~~

我觉得应该是 a.sort(cmp=lambda x,y: cmp(x[3],y[3])) 是in place 排序

下面是联机文档的内容:
The sort() and reverse() methods modify the list in place for economy of
space when sorting or reversing a large list. To remind you that they operate by
side effect, they don’t return the sorted or reversed list.
The sort() method takes optional arguments for controlling the
comparisons.cmp specifies a custom comparison function of two arguments (list
items) which should return a negative, zero or positive number depending on
whether the first argument is considered smaller than, equal to, or larger than
the second argument: cmp=lambdax,y:cmp(x.lower(),y.lower()). The
default value is None.key specifies a function of one argument that is used to extract a
comparison key from each list element: key=str.lower. The default value is None.reverse is a boolean value. If set to True, then the list
elements are sorted as if each comparison were reversed.In general, the key and reverse conversion processes are
much faster than specifying an equivalent cmp function. This is because
cmp is called multiple times for each list element while key
and reverse touch each element only once. Use functools.cmp_to_key() to convert an old-style
cmp function to a key function.Changed in version 2.3:
Support for None as an equivalent to omitting cmp was
added.Changed in version 2.4:
Support for key and reverse was added.
Starting with Python 2.3, the sort() method is
guaranteed to be stable. A sort is stable if it guarantees not to change the
relative order of elements that compare equal — this is helpful for sorting in
multiple passes (for example, sort by department, then by salary grade).
CPython implementation detail: While a list is being sorted,
the effect of attempting to mutate, or even inspect, the list is undefined. The
C implementation of Python 2.3 and newer makes the list appear empty for the
duration, and raises ValueError if it
can detect that the list has been mutated during a sort.
温馨提示:答案为网友推荐,仅供参考
第1个回答  2012-08-11
a.sort(key = lambda x:x[-1])
相似回答