【发布时间】:2014-05-05 20:48:47
【问题描述】:
我可以在list.sort() 中对列表进行排序时访问它吗
b = ['b', 'e', 'f', 'd', 'c', 'g', 'a']
f = 'check this'
def m(i):
print i, b, f
return None
b.sort(key=m)
print b
返回
b [] check this
e [] check this
f [] check this
d [] check this
c [] check this
g [] check this
a [] check this
请注意,列表 b 的各个项目被发送到函数 m。但是在m 列表b 是空的,但是它可以看到变量f,它与列表b 具有相同的范围。为什么函数m 打印b 为[]?
【问题讨论】:
-
m确实可以看到列表b(否则会引发错误),只是一调用sort就为空。 -
没错,我现在改了问题。
标签: python list sorting python-internals