1、输入:JSON {"a":"aa","b":"bb","c":{"d":"dd","e":"ee"}} 输出:字典 {'a': 'aa', 'b': 'bb', 'd': 'dd', 'e': 'ee'}
1 def conversion(n,the_dict):
2 for key,value in n.items():
3 #如果value的值是字典,递归
4 if isinstance(value, dict):
5 conversion(value,the_dict)
6 else:
7 the_dict[key] = value
8 return the_dict
9 #实际上JSON对象应该要先用 json.loads(r)解码,转换为python的字典
10 #但是这里如果这样直接输入这种格式的数据,会默认为字典对象,就不解码了
11 r = {"a":"aa","b":"bb","c":{"d":"dd","e":"ee"}}
12
13 the_dict = dict()
14 print(conversion(r,the_dict))
2、计算n!,例如n=3(计算3*2*1=6)
1 def factorial(n):
2 if n == 1:
3 return 1
4 return n*factorial(n-1)
5
6 print(factorial(5))