Python:如何使用不区分大小写的匹配从一组字符串中删除/丢弃一个字符串?

问题描述 投票:0回答:0

我有一个来自维基数据的案例,其中字符串

Articles containing video clips
出现在一组
categories
中,需要删除。麻烦的是,它在其他集合中也显示为
articles containing video clips
(小写“a”)。

删除它的简单/安全方法似乎是

   setA.discard("Articles containing video clips").discard("articles containing video clips")

完全够用,但在复杂情况下无法扩展。除了使用 casefold 进行比较的明显循环或列表/集合理解之外,有什么不同的方法吗?

  unwantedString = 'Articles containing video clip'
  setA = {'tsunami', 'articles containing video clip'}

  reducedSetA = {nonmatch for nonmatch in setA if nonmatch.casefold() != 
      unwantedString.casefold }

  print(reducedSetA)
  {'tsunami'}

请注意,这 not 字符串替换情况 - 它是从一组字符串中删除一个字符串。

python case-insensitive set-comprehension
© www.soinside.com 2019 - 2024. All rights reserved.