- A+
所属分类:Python习题
代码要求:
写一个函数为get_digits,要求get_digits(123456)==>[1,2,3,4,5]
参考代码:
- 我写的
- c = []
- def get_digits(n):
- b = str(n)
- for i in range(len(b)):
- c.append(int(b[i]))
- get_digits(123456)
- print(c)
- 精简版(QQ群>>528770819)
- def get_digits(n):
- return [int(_) for _ in str(n)]
- get_digits(123456)
- 精简版2
- def get_digits(n):
- return list(map(int,str(n)))
- get_digits(123456)
关于map:https://www.lijinlong.cc/python/pyxx/1833.html
- 递归
- result = []
- def get_digits(n):
- if n > 0:
- print(n)
- result.insert(0, n%10)
- get_digits(n//10)
- get_digits(12345)
- print(result)
insert():https://www.lijinlong.cc/python/pyxx/1535.html