简体   繁体   中英

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:

['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:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM