我是python的新手,尝试将2.x中的脚本移植到3.xi遇到错误TypeError; 必须在python 3.x中使用关键字参数或键函数.下面是一段代码:请帮忙
def resort_working_array( self, chosen_values_arr, num ):
for item in self.__working_arr[num]:
data_node = self.__pairs.get_node_info( item )
new_combs = []
for i in range(0, self.__n):
# numbers of new combinations to be created if this item is appended to array
new_combs.append( set([pairs_storage.key(z) for z in xuniqueCombinations( chosen_values_arr+[item], i+1)]) - self.__pairs.get_combs()[i] )
# weighting the node
item.weights = [ -len(new_combs[-1]) ] # node that creates most of new pairs is the best
item.weights += [ len(data_node.out) ] # less used outbound connections most likely to produce more new pairs while search continues
item.weights += [ len(x) for x in reversed(new_combs[:-1])]
item.weights += [ -data_node.counter ] # less used node is better
item.weights += [ -len(data_node.in_) ] # otherwise we will prefer node with most of free inbound connections; somehow it works out better ;)
self.__working_arr[num].sort( key = lambda a,b: cmp(a.weights, b.weights) )
Run Code Online (Sandbox Code Playgroud)
Kev*_*vin 17
看起来问题出在这一行.
self.__working_arr[num].sort( key = lambda a,b: cmp(a.weights, b.weights) )
Run Code Online (Sandbox Code Playgroud)
该key调用应该只取一个参数.尝试:
self.__working_arr[num].sort(key = lambda a: a.weights)
Run Code Online (Sandbox Code Playgroud)
如果尝试将key参数作为位置参数传递,则会出现完全相同的错误消息。
错误:
sort(lst, myKeyFunction)
Run Code Online (Sandbox Code Playgroud)
正确:
sort(lst, key=myKeyFunction)
Run Code Online (Sandbox Code Playgroud)
的Python 3.6.6
| 归档时间: |
|
| 查看次数: |
17170 次 |
| 最近记录: |