我的 python 函数是 returning None,即使在声明 return 语句之后也是如此。我不明白我在哪里绑腿。递归是必须的
My python function is returning None, even if after declaring return statement. I'm unable to understand where I am legging. Recursion is mandatory
# i want to read 'jsondict' json below store it in cond,field,operator,val variable using recursion(mandatory) but my function returning None, what should i do.
'''
jsondict = {
“条件”:“和”,
“规则”:[
{
"id": "价格",
“字段”:“价格”,
“类型”:“双”,
“输入”:“数字”,
“运营商”:“少”,
“价值”:10.25
},
{
“条件”:“或”,
“规则”:[
{
"id": "类别",
“字段”:“类别”,
“类型”:“整数”,
“输入”:“select”,
“运算符”:“等于”,
“价值”:2
},
{
"id": "类别",
“字段”:“类别”,
“类型”:“整数”,
“输入”:“select”,
“运算符”:“等于”,
“价值”:1
}
]
}
]
}
cond = []
field = []
operator = []
val = []
def rules(n):
for key, value in n.items():
#print(key, value)
if key == 'condition':
cond.append(value)
elif key == 'rules':
#print(key, values)
for i in value:
#print(i)
for a, b in i.items():
#print(a,b)
if a == 'field':
field.append(b)
#print(b)
elif a == 'operator':
operator.append(b)
elif a == 'value':
val.append(b)
elif a == 'condition':
cond.append(b)
elif a == 'rules':
for j in b:
print(rules(j))
return rules(j) # HERE CALLING FUNTION
rules(jsondict)
print(field) # CHECKING IF VALUES GOING IN VARIABLES
print(operator)
print(val)
print(cond)
'''
- 在递归的第 2 步中,您将规则方法应用于此类字典:
{
"id": "category",
"field": "category",
"type": "integer",
"input": "select",
"operator": "equal",
"value": 2 }
它不起作用,因为没有“规则”键。
- 此外,return调用该函数会导致 for 循环中断。调用不带“return”关键字的方法。
尝试这样的事情:
def rules(n):
for key, value in n.items():
if key == 'condition':
cond.append(value)
elif key =='field':
field.append(value)
elif key == 'operator':
operator.append(value)
elif key == 'value':
val.append(value)
elif key == 'rules':
for v in value:
rules(v)
# i want to read 'jsondict' json below store it in cond,field,operator,val variable using recursion(mandatory) but my function returning None, what should i do.
''' jsondict = { “条件”:“和”, “规则”:[ { "id": "价格", “字段”:“价格”, “类型”:“双”, “输入”:“数字”, “运营商”:“少”, “价值”:10.25 }, { “条件”:“或”, “规则”:[ { "id": "类别", “字段”:“类别”, “类型”:“整数”, “输入”:“select”, “运算符”:“等于”, “价值”:2 }, { "id": "类别", “字段”:“类别”, “类型”:“整数”, “输入”:“select”, “运算符”:“等于”, “价值”:1 } ] } ] }
cond = []
field = []
operator = []
val = []
def rules(n):
for key, value in n.items():
#print(key, value)
if key == 'condition':
cond.append(value)
elif key == 'rules':
#print(key, values)
for i in value:
#print(i)
for a, b in i.items():
#print(a,b)
if a == 'field':
field.append(b)
#print(b)
elif a == 'operator':
operator.append(b)
elif a == 'value':
val.append(b)
elif a == 'condition':
cond.append(b)
elif a == 'rules':
for j in b:
print(rules(j))
return rules(j) # HERE CALLING FUNTION
rules(jsondict)
print(field) # CHECKING IF VALUES GOING IN VARIABLES
print(operator)
print(val)
print(cond)
'''
- 在递归的第 2 步中,您将规则方法应用于此类字典:
{ "id": "category", "field": "category", "type": "integer",
"input": "select", "operator": "equal", "value": 2 }
它不起作用,因为没有“规则”键。
- 此外,return调用该函数会导致 for 循环中断。调用不带“return”关键字的方法。
尝试这样的事情:
def rules(n):
for key, value in n.items():
if key == 'condition':
cond.append(value)
elif key =='field':
field.append(value)
elif key == 'operator':
operator.append(value)
elif key == 'value':
val.append(value)
elif key == 'rules':
for v in value:
rules(v)