In [90]: x = [4, 6, 2, 1, 7, 9]
In [91]: x.sort()
In [92]: x
Out[92]: [1, 2, 4, 6, 7, 9]
In [98]: aa = x.sort()
In [99]: aa # 返回None
In [94]: a = sorted(x)
In [95]: a
Out[95]: [1, 2, 4, 6, 7, 9]
In [96]: x
Out[96]: [4, 6, 2, 1, 7, 9]
## 1、key在使用時(shí)必須提供一個(gè)排序過(guò)程總調(diào)用的函數(shù):
x = ['mmm', 'mm', 'mm', 'm']
x.sort(key=len)
print(x) # 輸出 ['m', 'mm', 'mm', 'mmm']
## 2、reverse實(shí)現(xiàn)降序排序,需要提供一個(gè)布爾值:
y = [3, 2, 8, 0, 1]
y.sort(reverse=True)
print(y) # [8, 3, 2, 1, 0]
def sort_priority(values,group):
def helper(x):
if x in group:
print('在group',0,x)
return (0,x)
# print(values)
print('不在group',1,x)
return (1,x)
values.sort(key=helper)
# values.sort()
numbers = [8,3,1,2,5,4,7,6]
group = {2,3,5,7}
sort_priority(numbers,group)
print(numbers)
輸出:
站在用戶(hù)的角度思考問(wèn)題,與客戶(hù)深入溝通,找到延川網(wǎng)站設(shè)計(jì)與延川網(wǎng)站推廣的解決方案,憑借多年的經(jīng)驗(yàn),讓設(shè)計(jì)與互聯(lián)網(wǎng)技術(shù)結(jié)合,創(chuàng)造個(gè)性化、用戶(hù)體驗(yàn)好的作品,建站類(lèi)型包括:成都網(wǎng)站設(shè)計(jì)、成都做網(wǎng)站、企業(yè)官網(wǎng)、英文網(wǎng)站、手機(jī)端網(wǎng)站、網(wǎng)站推廣、國(guó)際域名空間、網(wǎng)絡(luò)空間、企業(yè)郵箱。業(yè)務(wù)覆蓋延川地區(qū)。
不在group 1 8
在group 0 3
不在group 1 1
在group 0 2
在group 0 5
不在group 1 4
在group 0 7
不在group 1 6
[2, 3, 5, 7, 1, 4, 6, 8]
這個(gè)函數(shù)之所以能夠正常運(yùn)作,是基于下列三個(gè)原因:
def sort_priority2(values,group):
found = False
def helper(x):
if x in group:
found = True
return (0,x)
return (1,x)
values.sort(key=helper)
return found
numbers = [8,3,1,2,5,4,7,6]
group = [8,5,2,3,4,7,9]
found = sort_priority2(numbers,group)
print('最后的numbers',numbers)
print("found",found)
輸出:最后的numbers [2, 3, 4, 5, 7, 8, 1, 6]
found False
下面用nonlocal來(lái)實(shí)現(xiàn)這個(gè)函數(shù):
Python 3中有一種特殊的寫(xiě)法,能夠獲取閉包內(nèi)的數(shù)據(jù)。我們可以用nonlocal語(yǔ)句來(lái)表明這樣的意圖,也就是:給相關(guān)變量賦值的時(shí)候,應(yīng)該在上層作用域中查找該變量。
nonlocal的唯一限制在于,它不能延伸到模塊級(jí)別,這是為了防止它污染全局作用域。
def sort_priority2(values,group):
found = False
def helper(x):
if x in group:
nonlocal found
found = True
return (0,x)
return (1,x)
values.sort(key=helper)
return found
numbers = [8,3,1,2,5,4,7,6]
group = [8,5,2,3,4,7,9]
found = sort_priority2(numbers,group)
print('最后的numbers',numbers)
print("found",found)
輸出:最后的numbers [2, 3, 4, 5, 7, 8, 1, 6]
found True
nonlocal語(yǔ)句清楚地表明:如果在閉包內(nèi)給該變量賦值,那么修改的其實(shí)是閉包外那個(gè)作用域中的變量。這與global語(yǔ)句互為補(bǔ)充,global用來(lái)表示對(duì)該變量的賦值操作,將會(huì)直接修改模塊作用域里的那個(gè)變量。
然而,nonlocal也會(huì)像全局變量那樣,遭到濫用,所以,建議大家只在極其簡(jiǎn)單的函數(shù)里使用這種機(jī)制。nonlocal的副作用很難追蹤,尤其是在比較長(zhǎng)的函數(shù)中,修飾某變量的nonlocal語(yǔ)句可能和修改該變量的賦值操作離得比較遠(yuǎn),從而導(dǎo)致代碼更加難以理解。
如果使用nonlocal的那些代碼,已經(jīng)寫(xiě)得越來(lái)越復(fù)雜,那就應(yīng)該將相關(guān)的狀態(tài)封裝成輔助類(lèi)(helper class)。下面定義的這個(gè)類(lèi),與nonlocal所達(dá)成的功能相同。它雖然有點(diǎn)長(zhǎng),但是理解起來(lái)相當(dāng)容易(其中有個(gè)名叫_call_
的特殊方法
'''
學(xué)習(xí)中遇到問(wèn)題沒(méi)人解答?小編創(chuàng)建了一個(gè)Python學(xué)習(xí)交流群:
尋找有志同道合的小伙伴,互幫互助,群里還有不錯(cuò)的視頻學(xué)習(xí)教程和PDF電子書(shū)!
'''
class Sorter(object):
def __init__(self,group):
self.group = group
self.found = False
def __call__(self,x):
if x in self.group:
self.found = True
return (0,x)
return (1,x)
group = [8,5,2,3,4,7,9]
numbers = [8,3,1,2,5,4,7,6]
sorter = Sorter(group)
numbers.sort(key=sorter)
assert sorter.found is True
print(sorter.found)
student_tuples = [
('john', 'A',20, 15),
('jane', 'B',21, 12),
('dave', 'B', 22,10),
]
print(sorted(student_tuples, key=lambda student: student[0])) # sort by age
# [('dave', 'B', 10), ('jane', 'B', 12), ('john', 'A', 15)]
L = [{1:5,3:4},{1:3,6:3},{1:1,2:4,5:6},{1:9}]
def f(x):
return len(x)
L.sort(key=f) #reverse = True #怎樣在此處天加速reverse
print (L)
輸出:
student_tuples = [
('john', 'A',20, 15),
('jane', 'B',21, 12),
('dave', 'B', 22,10),
]
print(sorted(student_tuples, key=lambda student: student[0])) # sort by age
# [('dave', 'B', 10), ('jane', 'B', 12), ('john', 'A', 15)]
L = [{1:5,3:4},{1:3,6:3},{1:1,2:4,5:6},{1:9}]
def f(x):
return len(x)
L.sort(key=f) #reverse = True #怎樣在此處天加速reverse
print (L)