如何从字符串列表中检索部分匹配?[重复]

如何从字符串列表中检索部分匹配?[重复],第1张

如何从字符串列表中检索部分匹配?[重复]
  • startswith
    in
    ,返回布尔值
  • in
    运营商成员的考验。
  • 可以使用
    list-comprehension
    或来执行
    filter
  • list-comprehension
    与一起使用
    in
    是经过测试的最快实施。
  • 如果大小写不是问题,请考虑将所有单词映射为小写。
    • l = list(map(str.lower, l))
filter
  • 使用

    filter
    会创建一个
    filter
    对象,因此
    list()
    用于显示中的所有匹配值
    list

    l = [‘ones’, ‘twos’, ‘threes’]
    wanted = ‘three’

    using startswith

    result = list(filter(lambda x: x.startswith(wanted), l))

    using in

    result = list(filter(lambda x: wanted in x, l))

    print(result)
    [out]:
    [‘threes’]

list-comprehension
l = ['ones', 'twos', 'threes']wanted = 'three'# using startswithresult = [v for v in l if v.startswith(wanted)]# using inresult = [v for v in l if wanted in v]print(result)[out]:['threes']
哪个实现更快?
  • 使用
    words
    语料库
    nltk
  • 用词

    'three'

    • ['three', 'threefold', 'threefolded', 'threefoldedness', 'threefoldly', 'threefoldness', 'threeling', 'threeness', 'threepence', 'threepenny', 'threepennyworth', 'threescore', 'threesome']

    from nltk.corpus import words

    %timeit list(filter(lambda x: x.startswith(wanted), words.words()))
    [out]:
    47.4 ms ± 1.9 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)

    %timeit list(filter(lambda x: wanted in x, words.words()))
    [out]:
    27 ms ± 1.78 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)

    %timeit [v for v in words.words() if v.startswith(wanted)]
    [out]:
    34.1 ms ± 768 µs per loop (mean ± std. dev. of 7 runs, 10 loops each)

    %timeit [v for v in words.words() if wanted in v]
    [out]:
    14.5 ms ± 63.3 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)



欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5664632.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-16
下一篇 2022-12-17

发表评论

登录后才能评论

评论列表(0条)

保存