我试图根据它是奇数还是偶数(甚至获得更高的优先级)对数字列表进行排序.例:
a=[1,2,3,4]
a.sort(key=org(a)) sorted will produce [2,4,1,3]. I want to use the sort method
def org(a):
for i in range(len(a)):
if a[i]%2==0:
b.append(a[i])
b.sort()
else:
c.append(a[i])
c.sort()
print(b+c)
Run Code Online (Sandbox Code Playgroud)
我从运行a.sort(key = org(a))收到此错误
Traceback (most recent call last):
File "<pyshell#80>", line 1, in <module>
a.sort(key=org(a))
TypeError: 'list' object is not callable
Run Code Online (Sandbox Code Playgroud)
我意识到每次排序都会让它变慢.我可以采用哪种方式而不必在每次循环后进行排序?