递归函数:'NoneType' 对象不可迭代
recursive function: 'NoneType' object is not iterable
我有一个嵌套字典,
d={
"A":1,
"depth":0,
"chain":[
{
"A1":0.7,
"depth":1,
"chain":[
{
"A11":0.3,
"depth":2,
"key2":{"direct":{},"cumulative":{"B":0.3}},
"chain":[]
},
{
"A12":0.4,
"depth":2,
"chain":[
{
"A121":0.4,
"depth":3,
"key2":{"direct": {}, "cumulative":{"C":0.2, "D": 0.2}},
"chain": []
}]}]},
{
"A2":0.3,
"depth":1,
"chain":[
{
"A11":0.3,
"depth":2,
"key2":{"direct":{}, "cumulative":{"D":0.3}},
"chain":[]
}]}]}
我想要return一个第一个键重复x次的列表。 x 是 "chain" 下的元素数。在这种情况下,它将 return:
["A", "A", "A1", "A1", "A2", "A12"]
我试过以下方法
def from_nodes(d):
from_n=[list(d.keys())[0]]*len(d["chain"])
for x in d["chain"]:
if x is not None:
from_n.extend(from_nodes(x))
return from_n
并得到错误
TypeError Traceback (most recent call last)
<ipython-input-196-6233463c604b>in <module>()
----> 1 from_nodes(test2)
<ipython-input-194-5b7ca4b6db75>in from_nodes(d)
3 for x in d["chain"]:
4 if x is not None:
----> 5 from_n.extend(from_nodes(x))
6 return from_n
<ipython-input-194-5b7ca4b6db75> in from_nodes(d)
3 for x in d["chain"]:
4 if x is not None:
----> 5 from_n.extend(from_nodes(x))
6 return from_n
正如我在 , your error is that the return statement is indented incorrectly. If d["chain"]
is empty or None
, your function will return None
implicitly 中提到的那样。
将您的函数更改为以下内容:
def from_nodes(d):
from_n=[list(d.keys())[0]]*len(d["chain"])
for x in d["chain"]:
if x is not None:
from_n.extend(from_nodes(x))
return from_n
将修复错误,我的计算机上的结果是:
print(from_nodes(d))
#['A', 'A', 'A1', 'A1', 'depth', 'depth']
这与您想要的输出不匹配——这是因为当您调用 .keys()
时不能保证获得确定性顺序。
为所需输出修改函数的一种方法是创建要忽略的键列表:
def from_nodes(d):
ignore_keys = {"chain", "depth", "key2"}
from_n=[list(k for k in d.keys() if k not in ignore_keys)[0]]*len(d["chain"])
for x in d["chain"]:
if x is not None:
from_n.extend(from_nodes(x))
return from_n
print(from_nodes(d))
#['A', 'A', 'A1', 'A1', 'A12', 'A2']
但是,这只是我推测您的要求。您需要为 "first" 键的含义定义正确的条件。
我有一个嵌套字典,
d={
"A":1,
"depth":0,
"chain":[
{
"A1":0.7,
"depth":1,
"chain":[
{
"A11":0.3,
"depth":2,
"key2":{"direct":{},"cumulative":{"B":0.3}},
"chain":[]
},
{
"A12":0.4,
"depth":2,
"chain":[
{
"A121":0.4,
"depth":3,
"key2":{"direct": {}, "cumulative":{"C":0.2, "D": 0.2}},
"chain": []
}]}]},
{
"A2":0.3,
"depth":1,
"chain":[
{
"A11":0.3,
"depth":2,
"key2":{"direct":{}, "cumulative":{"D":0.3}},
"chain":[]
}]}]}
我想要return一个第一个键重复x次的列表。 x 是 "chain" 下的元素数。在这种情况下,它将 return:
["A", "A", "A1", "A1", "A2", "A12"]
我试过以下方法
def from_nodes(d):
from_n=[list(d.keys())[0]]*len(d["chain"])
for x in d["chain"]:
if x is not None:
from_n.extend(from_nodes(x))
return from_n
并得到错误
TypeError Traceback (most recent call last)
<ipython-input-196-6233463c604b>in <module>()
----> 1 from_nodes(test2)
<ipython-input-194-5b7ca4b6db75>in from_nodes(d)
3 for x in d["chain"]:
4 if x is not None:
----> 5 from_n.extend(from_nodes(x))
6 return from_n
<ipython-input-194-5b7ca4b6db75> in from_nodes(d)
3 for x in d["chain"]:
4 if x is not None:
----> 5 from_n.extend(from_nodes(x))
6 return from_n
正如我在 d["chain"]
is empty or None
, your function will return None
implicitly 中提到的那样。
将您的函数更改为以下内容:
def from_nodes(d):
from_n=[list(d.keys())[0]]*len(d["chain"])
for x in d["chain"]:
if x is not None:
from_n.extend(from_nodes(x))
return from_n
将修复错误,我的计算机上的结果是:
print(from_nodes(d))
#['A', 'A', 'A1', 'A1', 'depth', 'depth']
这与您想要的输出不匹配——这是因为当您调用 .keys()
时不能保证获得确定性顺序。
为所需输出修改函数的一种方法是创建要忽略的键列表:
def from_nodes(d):
ignore_keys = {"chain", "depth", "key2"}
from_n=[list(k for k in d.keys() if k not in ignore_keys)[0]]*len(d["chain"])
for x in d["chain"]:
if x is not None:
from_n.extend(from_nodes(x))
return from_n
print(from_nodes(d))
#['A', 'A', 'A1', 'A1', 'A12', 'A2']
但是,这只是我推测您的要求。您需要为 "first" 键的含义定义正确的条件。