【问题标题】:Check if part of a string is in part of a string检查字符串的一部分是否在字符串的一部分
【发布时间】:2022-01-15 23:03:29
【问题描述】:
string_to_check = "Amazon.com is a company"
listtocheck = ["Amazon.com", "Apple"]

res = any((x:=rea.lower()) in string_to_check.lower() for rea in listtocheck)
if(res == True):
   print(x)

此代码打印amazon.com。当listtocheck 不完全包含"amazon.com" 时,如何获得amazon.com 作为回报?假设它包含"amazon"。现在没有输出,因为“.com”丢失了。

我能否以某种方式说,例如 listtocheck 项目(amaz、.com、mazo)的 4 个字符必须与要检查的字符串匹配?因此,如果 ..% 的 listtocheck 项目与 stringtocheck 匹配,这就像给我一个回报。

【问题讨论】:

  • "amazon" 应该仍然匹配。 in 测试子字符串是否存在,而不是“单词”是否存在。

标签: python list substring any


【解决方案1】:

试试这个:切片rea.lower())[:4]

string_to_check = "Amazon.com is a company"
listtocheck = ["Amazon", "Apple"]

res = any((x:=rea.lower())[:4] in string_to_check.lower() for rea in
          listtocheck)
if(res == True):
   print(x)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-06-21
    • 2016-05-08
    • 2019-07-19
    • 2011-09-19
    • 2021-05-17
    • 2017-08-15
    • 2011-09-17
    相关资源
    最近更新 更多