gpt4 book ai didi

python - 如何将 python 列表理解转换为 map/filter 函数调用?

转载 作者:太空宇宙 更新时间:2023-11-04 06:53:21 25 4
gpt4 key购买 nike

我有一些看起来像这样的 python 代码:

return [x.doSomething() for x in getIterator() if x[0]=="z"]

如何使用 map()filter() 获得相同的功能?

最佳答案

翻译应该是

return map(lambda x: x.doSomething(), 
filter(lambda x: x[0] == "z", getIterator()))

但是,请使用列表理解。它更快、更简洁且更具可读性。

关于python - 如何将 python 列表理解转换为 map/filter 函数调用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8897536/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com