V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX 提问指南
hao1032
V2EX  ›  问与答

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

  •  
  •   hao1032 · 2015-07-14 11:48:33 +08:00 · 2091 次点击
    这是一个创建于 3210 天前的主题,其中的信息可能已经有所发展或是发生改变。
    我现在经常使用‘ab’ in str,只是因为这样写好看一点。不知道这样做是不是对的。
    1 条回复    2015-07-14 11:51:57 +08:00
    yjmade
        1
    yjmade  
       2015-07-14 11:51:57 +08:00
    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 更好 啊
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2792 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 22ms · UTC 06:14 · PVG 14:14 · LAX 23:14 · JFK 02:14
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.