通过重复应用功能创建列表(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.)
更多推荐
发布评论