x in 'aeiou' and y in 'aeiou',有没有更加优雅的写法? - V2EX
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
JasonLaw

x in 'aeiou' and y in 'aeiou',有没有更加优雅的写法?

  •  
  •   JasonLaw Apr 3, 2023 2075 views
    This topic created in 1135 days ago, the information mentioned may be changed or developed.

    我在做Count Vowel Strings in Ranges - LeetCode,下面是我的答案。

    class Solution: def vowelStrings(self, words: List[str], queries: List[List[int]]) -> List[int]: acc_arr = list(accumulate(words, lambda acc, word: acc + (word[0] in 'aeiou'and word[-1] in 'aeiou'), initial=0)) res = [] for l, r in queries: res.append(acc_arr[r + 1] - acc_arr[l]) return res 

    我觉得 word[0] in 'aeiou' and word[-1] in 'aeiou'不够优雅,有没有好的写法?

    Supplement 1    Apr 3, 2023

    优化后的代码如下:

    class Solution: def vowelStrings(self, words: List[str], queries: List[List[int]]) -> List[int]: acc_arr = list(accumulate(words, lambda acc, word: acc + all(c in 'aeiou' for c in {word[0], word[-1]}), initial=0)) return [acc_arr[r + 1] - acc_arr[l] for l, r in queries] 
    5 replies    2023-05-29 16:49:07 +08:00
    NessajCN
        1
    NessajCN  
       Apr 3, 2023
    你重点关注错了
    word[0] in 'aeiou' and word[-1] in 'aeiou' 优不优雅无所谓的,对性能没啥影响
    但是下面的 for append 不怎么优雅,建议用 list comprehension 或 map
    JasonLaw
        2
    JasonLaw  
    OP
       Apr 3, 2023 via iPhone
    @NessajCN #1 对,下面的 for loop 可以替换为 lost comprehension
    featureoverload
        3
    featureoverload  
       Apr 3, 2023
    `all([c in 'aeiou' for c in (x, y)])`
    JasonLaw
        4
    JasonLaw  
    OP
       Apr 3, 2023
    @featureoverload #3 谢谢,不过可以优化成`all(c in 'aeiou' for c in (x, y))`
    XueXianqi
        5
    XueXianqi  
       May 29, 2023
    @featureoverload

    `all([c in 'aeiou' for c in (x, y)])`看起来优雅,但是性能考虑得不够周全
    Python 中有个叫“短路运算”的,`a and b`,如果 a 不成立,就不去运算 b 了,显然这里的 all 用得就不够优雅了
    用 all 还需要注意的是:不能完全代替 and ,比如说:
    `if a.hasattr("name") and a.name.startswith("A"):`

    这里就不能用 all ,如果 a 没有 name 这个 attribute ,就会报错。
    About     Help     Advertise     Blog     API     FAQ     Solana     5735 Online   Highest 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 52ms UTC 03:26 PVG 11:26 LAX 20:26 JFK 23:26
    Do have faith in what you're doing.
    ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86