【发布时间】:2014-05-06 10:05:34
【问题描述】:
我想对列表进行就地排序,并尝试在排序期间使用列表本身(在key 函数中)。我发现列表本身在那里似乎是空的。
a = [1,4,5,3,2,6,0]
b = ['b', 'e', 'f', 'd', 'c', 'g', 'a']
b.sort(key=lambda x: a[b.index(x)])
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 1, in <lambda>
ValueError: 'b' is not in list
所以我尝试了:
def f(x):
print "FOO:", x, b
return b.index(x)
b.sort(key=f)
得到了
FOO: b []
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 3, in f
ValueError: 'b' is not in list
对此有何解释?
【问题讨论】: