python写一个递归函数,python写递归函数,a = [1, 2, 3
python写一个递归函数,python写递归函数,a = [1, 2, 3
a = [1, 2, 3, 4]
如何才能生成
{'pid':1,'id':{'pid':2,'id':{'pid':3,'id':{...}}}
这样的结构呢
python3
>>> def f(ls): if len(ls)>1: x,*y=ls else: x,y=ls[0],None return {'pid':x,'id':f(y) if y else None}>>> f([1, 2, 3, 4]){'pid': 1, 'id': {'pid': 2, 'id': {'pid': 3, 'id': {'pid': 4, 'id': None}}}}
def fact(n): if n==1: return 1 return n * fact(n - 1)
计算n的阶乘
python 2.7
def gen(arr): if not arr: return None firstValue = arr[0] del arr[0] return {'pid': firstValue, 'id':gen(arr)} print(gen([1, 2, 3, 4]))
输出:
不过,题主呀,做伸手党是不好滴。。。
编橙之家文章,
相关内容
- IO瓶颈: python open读写文件时如何实现异步IO,,python op
- Python3 来定义类时, 为何该值不是所有实例共享呢 ?,
- git能向远程仓库Pull,不能向远程仓库push的问题?,git
- Python 标准操作符与逻辑运算符,python操作符,python同大
- Python程序调试的概念,python调试概念,基本上初学者在网
- Python 字符串是什么及简单操作方法,python字符串,pyth
- Python print语句不换行、换行函数语句操作方法,python
- Python dir()内置函数作用及使用方法,pythondir,Python内置的
- Python基础知识之专用术语讲解,python基础知识术语,在学
- Python help函数命令怎么使用,pythonhelp函数命令,help函数
评论关闭