pythonlambdacmp
‘壹’ python中lambda和itemgetter的区别
operator.itemgetter函数 operator模块提供itemgetter函数用于获取象哪些维数据参数些序号(即需要获取数据象序号)面看例 a = [一,二,三] >>> b=operator.itemgetter(一) //定义函数b获取象第一域值 >>> b(a) 二 >>> b=operator.itemgetter(一,0) //定义函数b获取象第一域第0值 >>> b(a) (二, 一) 要注意operator.itemgetter函数获取值定义函数通该函数作用象才能获取值 sorted函数 Python内置排序函数sortedlist或者iterator进行排序官中国文档见:中国docs.python.org/二/library/functions.html?highlight=sorted#sorted该函数原型: sorted(iterable[, cmp[, key[, reverse]]]) 参数解释: (一)iterable指定要排序list或者iterable用说; (二)cmp函数指定排序进行比较函数指定函数或者lambda函数: students类象list没员三域用sorted进行比较自定cmp函数例要通比较第三数据员排序代码写: students = [('john', 'A', 一5), ('jane', 'B', 一二), ('dave', 'B', 一0)] sorted(students, key=lambda student : student[二]) (三)key函数指定取待排序元素哪项进行排序函数用面例说明代码: sorted(students, key=lambda student : student[二]) key指定lambda函数功能元素student第三域(即:student[二])sorted排序students所元素第三域进行排序 面operator.itemgetter函数用该函数实现例要通student第三域排序写: sorted(students, key=operator.itemgetter(二)) sorted函数进行级排序例要根据第二域第三域进行排序写: sorted(students, key=operator.itemgetter(一,二)) 即先跟句第二域排序再根据第三域排序 (四)reverse参数用说bool变量表示升序降序排列默认false(升序排列)定义True按降序排
‘贰’ python list 比较大小问题
我觉得应该是 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
ration, and raises ValueError if it
can detect that the list has been mutated ring a sort.
‘叁’ 如何用Python添加数据库一列有序数字
通过例子来说明sorted的用法: 1. 对由tuple组成的List排序 Python代码 >>> students = [('john', 'A', 15), ('jane', 'B', 12), ('dave', 'B', 10),] 用key函数排序(lambda的用法见 注释1) Python代码 >>> sorted(students, key=lambda student : student[2]) # sort by age [('dave', 'B', 10), ('jane', 'B', 12), ('john', 'A', 15)] 用cmp函数排序 Python代码 >>> sorted(students, cmp=lambda x,y : cmp(x[2], y[2])) # sort by age [('dave', 'B', 10), ('jane', 'B', 12), ('john', 'A', 15)] 用 operator 函数来加快速度, 上面排序等价于:(itemgetter的用法见 注释2) Python代码 >>> from operator import itemgetter, attrgetter >>> sorted(students, key=itemgetter(2)) 用 operator 函数进行多级排序 Python代码 >>> sorted(students, key=itemgetter(1,2)) # sort by grade then by age [('john', 'A', 15), ('dave', 'B', 10), ('jane', 'B', 12)] 2. 对由字典排序 Python代码 >>> d = {'data1':3, 'data2':1, 'data3':2, 'data4':4} >>> sorted(d.iteritems(), key=itemgetter(1), reverse=True) [('data4', 4), ('data1', 3), ('data3', 2), ('data2', 1)]
‘肆’ 如何对列表进行排序 python
很多时候,我们需要对List进行排序,Python提供了两个方法,对给定的List
L进行排序:
方法1.用List的成员函数sort进行排序
方法2.用built-in函数sorted进行排序(从2.4开始)
这两种方法使用起来差不多,以第一种为例进行讲解:
从Python2.4开始,sort方法有了三个可选的参数,Python
Library
Reference里是这样描述的
复制代码代码如下:
cmp:cmp
specifies
a
custom
comparison
function
of
two
arguments
(iterable
elements)
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=lambda
x,y:
cmp(x.lower(),
y.lower())"
key:key
specifies
a
function
of
one
argument
that
is
used
to
extract
a
comparison
key
from
each
list
element:
"key=str.lower"
reverse: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.
以下是sort的具体实例。
实例1:
复制代码代码如下:
>>>L
=
[2,3,1,4]
>>>L.sort()
>>>L
>>>[1,2,3,4]
实例2:
复制代码代码如下:
>>>L
=
[2,3,1,4]
>>>L.sort(reverse=True)
>>>L
>>>[4,3,2,1]
实例3:
复制代码代码如下:
>>>L
=
[('b',2),('a',1),('c',3),('d',4)]
>>>L.sort(cmp=lambda
x,y:cmp(x[1],y[1]))
>>>L
>>>[('a',
1),
('b',
2),
('c',
3),
('d',
4)]
实例4:
复制代码代码如下:
>>>L
=
[('b',2),('a',1),('c',3),('d',4)]
>>>L.sort(key=lambda
x:x[1])
>>>L
>>>[('a',
1),
('b',
2),
('c',
3),
('d',
4)]
实例5:
复制代码代码如下:
>>>L
=
[('b',2),('a',1),('c',3),('d',4)]
>>>import
operator
>>>L.sort(key=operator.itemgetter(1))
>>>L
>>>[('a',
1),
('b',
2),
('c',
3),
('d',
4)]
实例6:(DSU方法:Decorate-Sort-Undercorate)
复制代码代码如下:
>>>L
=
[('b',2),('a',1),('c',3),('d',4)]
>>>A
=
[(x[1],i,x)
for
i,x
in
enumerate(L)]
#i
can
confirm
the
stable
sort
>>>A.sort()
>>>L
=
[s[2]
for
s
in
A]
>>>L
>>>[('a',
1),
('b',
2),
('c',
3),
('d',
4)]
以上给出了6中对List排序的方法,其中实例3.4.5.6能起到对以List
item中的某一项
为比较关键字进行排序.
效率比较:
复制代码代码如下:
cmp
<
DSU
<
key
通过实验比较,方法3比方法6要慢,方法6比方法4要慢,方法4和方法5基本相当
多关键字比较排序:
实例7:
复制代码代码如下:
>>>L
=
[('d',2),('a',4),('b',3),('c',2)]
>>>
L.sort(key=lambda
x:x[1])
>>>
L
>>>[('d',
2),
('c',
2),
('b',
3),
('a',
4)]
我们看到,此时排序过的L是仅仅按照第二个关键字来排的,如果我们想用第二个关键字
排过序后再用第一个关键字进行排序呢?有两种方法
实例8:
复制代码代码如下:
>>>
L
=
[('d',2),('a',4),('b',3),('c',2)]
>>>
L.sort(key=lambda
x:(x[1],x[0]))
>>>
L
>>>[('c',
2),
('d',
2),
('b',
3),
('a',
4)]
实例9:
复制代码代码如下:
>>>
L
=
[('d',2),('a',4),('b',3),('c',2)]
>>>
L.sort(key=operator.itemgetter(1,0))
>>>
L
>>>[('c',
2),
('d',
2),
('b',
3),
('a',
4)]
为什么实例8能够工作呢?原因在于tuple是的比较从左到右之一比较的,比较完第一个,如果
相等,比较第二个
‘伍’ lambda x: float(x[1:-1]) 这个函数是什么意思,python 3.5
先拆分来看:
lambda在python中是匿名函数的意思,同一般的函数不一样,它没有名字,也就不能调用,并且实质上lambda就是一个表达式,但是可以赋值给一个变量,通过这个变量来调用匿名函数,比如:
补充:字符串也是一个数组对象,可以被切片
‘陆’ python的lambda形式的理解
lambda 创建一个匿名函数。冒号前面是传入参数,后面是一个处理传入参数的单行表达式。
调用lambda函数返回表达式的结果。
list的sort 同于内建函数sorted。它有三个参数cmp,key,reverse,均有默认值。
cmp是一个比较器,自定义比较器的话传入两个参数x,y,返回值限制为1(x>y), 0(x=y), -1(x<y)。默认是常规比较,数字比较,字符串比较等等。
key是对序列元素处理(比如问题中序列元素仍是序列,就可以再对序列取索引值),也就是sort的比较器实际接收到值是key处理后的结果(问题中实际参与比较的是1,2,3,4)。
reverse默认是False,意思是从小到大排序,传入True,就从大到小排。
‘柒’ 各位大神,用python对数据库中的某列数据排序怎么搞不是用sql来排序哦
通过例子来说明sorted的用法:
1.对由tuple组成的List排序
Python代码
>>>students=[('john','A',15),('jane','B',12),('dave','B',10),]
用key函数排序(lambda的用法见注释1)
Python代码
>>>sorted(students,key=lambdastudent:student[2])#sortbyage
[('dave','B',10),('jane','B',12),('john','A',15)]
用cmp函数排序
Python代码
>>>sorted(students,cmp=lambdax,y:cmp(x[2],y[2]))#sortbyage
[('dave','B',10),('jane','B',12),('john','A',15)]
用operator函数来加快速度,上面排序等价于:(itemgetter的用法见注释2)
Python代码
>>>fromoperatorimportitemgetter,attrgetter
>>>sorted(students,key=itemgetter(2))
用operator函数进行多级排序
Python代码
>>>sorted(students,key=itemgetter(1,2))#sortbygradethenbyage
[('john','A',15),('dave','B',10),('jane','B',12)]2.对由字典排序
Python代码
>>>d={'data1':3,'data2':1,'data3':2,'data4':4}
>>>sorted(d.iteritems(),key=itemgetter(1),reverse=True)
[('data4',4),('data1',3),('data3',2),('data2',1)]
‘捌’ 求大神用Python解决下面这个。具体问题写在问题补充内
a="aAsmr3idd4bgs7Dlsf9eAF"
b=list(filter(str.isalpha,a))
c=sorted(b,cmp=lambdax,y:1if(x.upper()>y.upper())else-1)
print''.join(c)
之前没看到要求5行内,现在四行搞定啦,其实还能缩到3行,不过影响美观,呵呵
‘玖’ python3 为什么取消了sort方法中的cmp参数
Python3 里面sort是用 key = lambda ....来排序的吧
This(key) is easier to use and faster to run. When using the cmp parameter, the sorting compares pairs of values, so the compare-function is called multiple times for every item. The larger the set of data, the more times the compare-function is called per item. With the key function the sorting instead keeps the key value for each item and compares those, so the key function is only called once for every item. This results in much faster sorts for large sets of data.