【问题标题】:fastest and elegant way to check whether a given list contains some element by regular expression通过正则表达式检查给定列表是否包含某些元素的最快和优雅的方法
【发布时间】:2019-06-24 10:10:46
【问题描述】:

检查正则表达式表示的某个元素是否在给定列表中的最快和优雅的方法。

例如: 给定一个列表:

newlist = ['this','thiis','thas','sada']
regex = re.compile('th.s')

在这个问题中:Regular Expressions: Search in list

list(filter(regex.match,newlist))

给我一​​份清单

['this','thas']

但是,我只想返回 True 或 False。因此,上述方法效率不高,因为它会查看 newlist 的所有元素。有没有办法像

'this' in newlist

高效优雅地检查正则表达式表示的某个元素是否在给定列表中。

【问题讨论】:

  • 首先使用regex 可能不是最有效的方法...
  • 使用any() 而不是filter()?
  • 从 newlist 中创建一个字符串:,this,thiis,thas,sada,。保留它,然后运行 ​​findall(),或使用新构造的正则表达式 ,(th.s), 进行单个匹配。这将为您提供['this','thas'] 或 TRUE/FALSE,而无需迭代。
  • @Loocid 你的意思是任何(过滤器(regex.match,newlist))?它仍然会查看 newlist 的所有元素以返回“True or False”,对吧?
  • @Loocid 你的意思是在 python3 中 filter() 给了我一个迭代器而不是一个完整的列表,因此当我合成 any() 时,如果不是最坏的情况,它不会遍历所有元素的新列表返回 True?

标签: regex python-3.x


【解决方案1】:

按照 Loocid 的建议,您可以使用 any。我会用这样的生成器表达式来做到这一点:

newlist = ['this','thiis','thas','sada']
regex = re.compile('th.s')

result = any(regex.match(word) for word in newlist)
print(result) # True

这是另一个带有map 的版本,速度稍快:

result = any(map(regex.match, newlist))

【讨论】:

  • 非常感谢。顺便说一句,我有一个问题: any(filter(regex.match,newlist)) 是否与您的代码具有相同的效果?由于我刚刚在 python3 中阅读过滤器给出了一个迭代器而不是一个完整列表。
  • @maplemaple 是的,但基本上你所做的只是检查filter(regex.match,newlist) 的长度是否大于0。我认为我的版本更明确。
  • @maplemaple 仅供参考,我用map添加了一个稍快的版本。
  • 还有一个问题。如果我给regex = re.compile('th.'),结果应该是False,因为'th.'不在列表中。但是代码还是True
  • @maplemaple 将$ 添加到您的正则表达式以使其成为'th.$'。 $ 表示字符串结束。
【解决方案2】:

这将评估列表,直到找到第一个匹配项。

def search_for_match(list):
    result = False
    for i in newlist:
        if bool(re.match(r"th.s", i)) is True:
            result = True
            break
    return result

或者说得更笼统:

def search_for_match(list, pattern):
    result = False
    for i in list:
        if bool(re.match(pattern, i)) is True:
            result = True
            break
    return result

newlist = ['this','thiis','thas','sada']
found = search_for_match(newlist, r"th.s")
print(found) # True

只是为了好玩,我通过计时器运行了这些。我输了:

t = time.process_time()
newlist = ['this','thiis','thas','sada']
search_for_match(newlist, r"th.s")
elapsed_time1 = time.process_time() - t
print(elapsed_time1) # 0.00015399999999998748

t2 = time.process_time()
newlist = ['this','thiis','thas','sada']
regex = re.compile('th.s')
result = any(regex.match(word) for word in newlist)
elapsed_time2 = time.process_time() - t2
print(elapsed_time2) # 1.1999999999900979e-05

t3 = time.process_time()
newlist = ['this','thiis','thas','sada']
regex = re.compile('th.s')
result = any(map(regex.match, newlist))
elapsed_time3 = time.process_time() - t3
print(elapsed_time3) # 5.999999999950489e-06        

【讨论】:

  • @Tomothy32,我通过计时器运行它们是因为我很好奇,而不是因为我不相信你。哇是我的慢。
  • @CarlBrubaker 没问题。顺便说一句,你正在通过计时所有事情来扭曲结果,而不仅仅是那一行代码。您应该只分别计时search_for_match(newlist, r"th.s")、result = any(regex.match(word) for word in newlist) 和result = any(map(regex.match, newlist))。 timeit 模块也是一个更好的主意,因为它不止一次地重复测试并且总体上更准确。另外,作为旁注,您可以通过预编译正则表达式来加速您的功能。
  • 还有一个问题。如果我给regex = re.compile('th.'),结果应该是False,因为'th.'不在列表中。但是代码还是True
【解决方案3】:

我能想到(除了使用any)

next((x for x in newlist if regex.match(x)), False)

不返回 True,但如果您没有空字符串,则条件测试可能没问题 :)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-09-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-29
    • 1970-01-01
    • 2020-07-12
    • 2016-12-20
    相关资源
    最近更新 更多