-1

I want to check if a list of string is contained in another string but ignoring the case

For example:

Input: 'Hello World', ['he', 'o w']
Output: [True, True]
Input: 'Hello World', ['he', 'wol']
Output: [True, False]

I can write something like:

output =[]
for keyword in keywordlist:
  if keyword.lower() in string.lower():
    output.append(True)
  else:
    output.append(False)

But the issues with this are:

  1. the time complexity
  2. using lower()

I have found this question on stack overflow which is similar Check if multiple strings exist in another string

But it doesn’t work for ignore case.

Is there an efficient way to do this?

Ahsan Tarique
  • 581
  • 1
  • 11
  • 22
  • 1
    Why do you say either of those things are an issue? (`casefold` would be more correct than `lower`, but that's both very easy to fix and almost certainly not what you had in mind.) – user2357112 May 03 '23 at 15:05
  • How long are the strings, how many are there, and please fix your invalid quote characters. – Kelly Bundy May 03 '23 at 15:27
  • You don't need to continually call *lower()* on *string* just re-assign it before entering the loop – DarkKnight May 03 '23 at 15:34

1 Answers1

0

This is probably quite efficient:

# make sure the input string is lowercase - do this only once
string = 'Hello World'.lower()
keywordlist = ['he', 'o w'] # all keywords assumed to be lowercase
output = [kw in string for kw in keywordlist]
print(output)

Output:

[True, True]
DarkKnight
  • 19,739
  • 3
  • 6
  • 22