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.