Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python运行时错误:cmp中超过了最大递归深度_Python_List_Dictionary_Recursion - Fatal编程技术网

Python运行时错误:cmp中超过了最大递归深度

Python运行时错误:cmp中超过了最大递归深度,python,list,dictionary,recursion,Python,List,Dictionary,Recursion,我正试图处理一个复杂的数据结构 数据结构说明:我有一本类词典。关键是一个名字。该值是一个类引用。该类包含两个词典列表 下面是我的数据结构的一个简单示例: import scipy.stats class employee_salaries(object): def __init__(self,management, players, disparity): self.management = management self.players = playe

我正试图处理一个复杂的数据结构

数据结构说明:我有一本类词典。关键是一个名字。该值是一个类引用。该类包含两个词典列表

下面是我的数据结构的一个简单示例:

import scipy.stats

class employee_salaries(object):
    def __init__(self,management, players, disparity):
        self.management = management
        self.players = players
        self.disparity = disparity

# the coach's salary was 12 his 1st year and 11 his 2nd year
mgmt1 = [{'Coach':12, 'Owner':15, 'Team Manager': 13}, {'Coach':11, 'Owner':14, 'Team Manager':15}]
plyrs1 = [{'Point Guard': 14, 'Power Forward':16,},{'Point Guard':16, 'Power Forward':18}]

NBA = {}

mgmt2 = [{'Coach':10, 'Owner':12}, {'Coach':13,'Owner':15}]
plyrs2 = [{'Point Guard':17, 'Power Forward':14}, {'Point Guard': 22, 'Power Forward':16}]

NBA['cavs'] = employee_salaries(mgmt1,plyrs1,0)
NBA['celtics'] = employee_salaries(mgmt2,plyrs2,0)
比如说,我想确定这两年控球后卫的工资和老板的工资之间的差距

for key, value in NBA.iteritems():
    x1=[]; x2=[]
    num = len(NBA[key].players)
    for i in range(0,num):
        x1.append(NBA[key].players[i]['Point Guard'])
        x2.append(NBA[key].management[i]['Owner'])
    tau, p_value = scipy.stats.kendalltau(x1, x2)

    NBA[key].disparity = tau
print NBA['cavs'].disparity
请记住,这不是我的真实数据。在我的实际数据结构中,有150多个键。字典列表中还有更多的元素。当我在真实数据上运行上面的代码时,我得到一个运行时错误

RuntimeError:cmp错误中超出了最大递归深度

如何更改上面的代码,使其不会出现最大递归深度错误?我想进行这种类型的比较,并能够保存值

您传入的是空数组,而函数对它的处理不正确。更新Scipy,或者在数组为空时跳过(尽管要检查数据是否正确,以及是否有空数组)


对代码的一些建议

for team in NBA.itervalues():
#Or `for name, team in NBA.iteritems()` if you use the name.
    x1, x2 = [], []
    # Not `x1 = x2 = []`, since that would be two names for one list

    for player, manager in izip(team.players, team.management):
        x1.append(player['Point Guard'])
        x2.append(manager['Owner'])
    # Or lose the `for` loop and say:
    # `x1 = [player['Point Guard'] for player in team.players]`
    # `x2 = [manager['Owner'] for manager in team.management]`
    # (This can be more efficient.)

    tau, p_value = scipy.stats.kendalltau(x1, x2)

    team.disparity = tau

print NBA['cavs'].disparity

重现错误的示例数据将非常有用。没有它,人们只能猜测错误到底发生在哪里;不过我还是怀疑有人进来了。您是否尝试用不同的实现替换kendalltau?在错误消息中显示堆栈(或者更确切地说,它的关键部分,并描述其余部分,因为它应该非常大)可能会有所帮助。出现此错误时,一定会出现过度递归。请尝试
scipy.stats.kendalltau(x1,x2,initial\u lexsort=False)
。错误消息中的堆栈显示
File”/usr/lib/python2.7/dist packages/scipy/stats/stats.py“,第2961行,在mergesort exchcnt+=mergesort(offs,length0)
重复。尝试插入
assert len(x1)和len(x2)
就在调用kendalltau之前。