[英]Trying to solve with python map function with lambda expression inside for loop
n=10
fun=list(map(lambda x:[j for j in range(x)],n))
print(fun)
expected output is:预计 output 是:
['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
# using map (map works on an iterable)
n=10
fun=lambda x: (range(x)) # remember this is a function (so call it)
fun=list(map(str, fun(n)))
print(fun)
# directly
n=10
fun=list(map(str, (range(n))))
print(fun)
['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
Python docs for reference: Python 文档供参考:
map()
: https://docs.python.org/3/library/functions.html#map map()
: https://docs.python.org/3/library/functions.html#maprange()
: https://docs.python.org/3/library/functions.html#func-range range()
: https://docs.python.org/3/library/functions.html#func-rangelambda()
: https://docs.python.org/3/library/ast.html?highlight=lambda#ast.Lambda lambda()
: https://docs.python.org/3/library/ast.html?highlight=3104BCAD8BZ04AEEBB2C
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.