Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/334.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
尝试使用带有lambda表达式的python映射函数解决for循环_Python - Fatal编程技术网

尝试使用带有lambda表达式的python映射函数解决for循环

尝试使用带有lambda表达式的python映射函数解决for循环,python,Python,预期产出为: n=10 fun=list(map(lambda x:[j for j in range(x)],n)) print(fun) Python文档供参考: map(): range(): lambda(): 大家好,欢迎来到StackOverflow。请花些时间阅读帮助页面,特别是命名和的部分。更重要的是,请阅读。您可能还想了解。map需要传递整数的iterable。还有,为什么您要使用列表理解和映射?为什么您不直接使用list(map(str,range(n))?您不需要lis

预期产出为:

n=10
fun=list(map(lambda x:[j for j in range(x)],n))
print(fun)
Python文档供参考:

  • map()
  • range()
  • lambda()

大家好,欢迎来到StackOverflow。请花些时间阅读帮助页面,特别是命名和的部分。更重要的是,请阅读。您可能还想了解。
map
需要传递整数的iterable。还有,为什么您要使用列表理解和映射?为什么您不直接使用
list(map(str,range(n))
?您不需要
list(range(n))
<代码>范围(n)已经是一个可移植的工具。@AlbinPaul很好的捕获,我已经更新了它
['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']