通过重复应用功能创建列表(Create list by repeated application of function)

编程入门 行业动态 更新时间:2024-10-25 17:26:57
通过重复应用功能创建列表(Create list by repeated application of function)

我要这个:

[foo() for _ in xrange (100)]

但美丽。 ?

I want this:

[foo() for _ in xrange (100)]

but beautifuller. ?

最满意答案

恐怕除了某些人会建议针对一个“匿名”变量_ ,你不会得到比Python更漂亮的东西。 这是做你想做的Pythonic成语。

(这可能会让新手感到困惑,因为它可能会被误认为是特殊的语法,我使用它,但是仅在我的代码的“专家部分”中使用它,我也经常遇到它,但意见似乎仍然有点分歧在这一个上。)

I'm afraid you're not gonna get it any prettier than that in Python, except that some people would advise against _ for an "anonymous" variable. This is the Pythonic idiom for doing what you want.

(The _ can be considered confusing to novices because it can be mistaken for special syntax. I use it, but only in the "expert parts" of my code. I also encounter it more and more often, but opinion still seems a bit divided on this one.)

更多推荐

本文发布于:2023-08-03 17:17:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1395145.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:功能   列表   Create   list   application

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!