0

I have a problem. I have a string like this: "Can you please turn off the lights?". Now I want to split the sentence like this:

['can', 'you', 'please', 'turn', 'off', 'the', 'lights?']

But now I also want to remove the ? at the end. I know I could just use substr but the expressions are not always available. How can I detecet them and if they are present remove them from a sentence?

Here is my code now:

given_command = "Can you please turn off the lights?"

data = given_command.lower().split(" ")
print(data)
A. Vreeswijk
  • 822
  • 1
  • 19
  • 57

4 Answers4

0

Try replace

In [98]: given_command = "Can you please turn off the lights?"
    ...:
    ...: data = given_command.lower().replace('?','').split(" ")
    ...:
    ...: print(data)
['can', 'you', 'please', 'turn', 'off', 'the', 'lights']
MichaelD
  • 1,274
  • 1
  • 10
  • 16
0

If you have just one symbol to remove (?), use str.replace:

...
>>> data = given_command.lower().replace('?', '').split(' ')
>>> print(data)
['can', 'you', 'please', 'turn', 'off', 'the', 'lights']

If you have more symbols, use re.sub (I'm using symbols ?, !, , and . as an example):

...
>>> import re
>>> data = re.sub(r'[?!,.]', '', given_command.lower()).split(' ')
>>> print(data)
['can', 'you', 'please', 'turn', 'off', 'the', 'lights']
DjaouadNM
  • 22,013
  • 4
  • 33
  • 55
0

As I referenced a link in comments you can see this one: enter link description here

It will split any mark for you:

import re
pattern = r"\w+(?:\.?\w+)*"
re.findall(pattern, 'hello, to world or not?', re.A)

result:

['hello', 'to', 'world', 'or', 'not']
Alireza HI
  • 1,873
  • 1
  • 12
  • 20
0

You can use re (regex) module:

import re
given_command = r"Can you please turn off the lights?"
data = given_command.lower().split(" ")
print(list(map(lambda x: re.sub('\\W', '', x), data))) # replace any non alphanumeric character with the empty string

Output:

['can', 'you', 'please', 'turn', 'off', 'the', 'lights']
Nir Alfasi
  • 53,191
  • 11
  • 86
  • 129