hao1032
V2EX  ›  问与答

python 里面检查'ab'是否在 str 字符串里面: str.find('ab') > -1; 'ab' in str;哪个更好呢?

  •  
  •   hao1032 · Jul 14, 2015 · 2876 views
    This topic created in 3970 days ago, the information mentioned may be changed or developed.
    我现在经常使用‘ab’ in str,只是因为这样写好看一点。不知道这样做是不是对的。
    1 replies    2015-07-14 11:51:57 +08:00
    yjmade
        1
    yjmade  
       Jul 14, 2015
    In [11]: %timeit a.find("ab")
    1000000 loops, best of 3: 312 ns per loop

    In [13]: %timeit "ab" in a
    10000000 loops, best of 3: 72.3 ns per loop

    明显 in 更好 啊
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   3029 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 14:42 · PVG 22:42 · LAX 07:42 · JFK 10:42
    ♥ Do have faith in what you're doing.