赞
踩
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在使用时必须提供一个排序过程总调用的函数:
x = ['mmm', 'mm', 'mm', 'm']
x.sort(key=len)
print(x) # 输出 ['m', 'mm', 'mm', 'mmm']
## 2、reverse实现降序排序,需要提供一个布尔值:
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)
输出:
不在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]
这个函数之所以能够正常运作,是基于下列三个原因:
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来实现这个函数:
Python 3中有一种特殊的写法,能够获取闭包内的数据。我们可以用nonlocal语句来表明这样的意图,也就是:给相关变量赋值的时候,应该在上层作用域中查找该变量。
nonlocal的唯一限制在于,它不能延伸到模块级别,这是为了防止它污染全局作用域。
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语句清楚地表明:如果在闭包内给该变量赋值,那么修改的其实是闭包外那个作用域中的变量。这与global语句互为补充,global用来表示对该变量的赋值操作,将会直接修改模块作用域里的那个变量。
然而,nonlocal也会像全局变量那样,遭到滥用,所以,建议大家只在极其简单的函数里使用这种机制。nonlocal的副作用很难追踪,尤其是在比较长的函数中,修饰某变量的nonlocal语句可能和修改该变量的赋值操作离得比较远,从而导致代码更加难以理解。
如果使用nonlocal的那些代码,已经写得越来越复杂,那就应该将相关的状态封装成辅助类(helper class)。下面定义的这个类,与nonlocal所达成的功能相同。它虽然有点长,但是理解起来相当容易(其中有个名叫_call_
的特殊方法
''' 学习中遇到问题没人解答?小编创建了一个Python学习交流群:711312441 寻找有志同道合的小伙伴,互帮互助,群里还有不错的视频学习教程和PDF电子书! ''' 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)
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。