python list 比较大小问题

有个a=[[a1,b1,c1],[a2,b2,c2],[a3,b3,c3],[a4,b4,c4]]要按c1,c2,c3,c4的值重新排a中四个元素的位置应该怎么做啊?谢谢... 有个a=[[a1, b1, c1], [a2, b2, c2], [a3, b3, c3], [a4, b4, c4]]
要按c1, c2, c3, c4的值重新排a中四个元素的位置
应该怎么做啊?
谢谢大虾们~~~
展开
 我来答
matlab2000
推荐于2016-01-09 · TA获得超过2323个赞
知道大有可为答主
回答量:1678
采纳率:100%
帮助的人:1061万
展开全部
我觉得应该是 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.
linkun
2012-08-11 · TA获得超过503个赞
知道小有建树答主
回答量:140
采纳率:50%
帮助的人:103万
展开全部
a.sort(key = lambda x:x[-1])
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
?>

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式