RY4N
RY4N

Reputation: 1112

How to take a user sentence and create a list of words out of it?

I'm unsure what the user will enter but I want to break their input sentence up into words in a list

User_input = raw_input("Please enter a search criterion: ")
User_Input_list[""]

# input example: steve at the office

# compiling the regular expression:
keyword = re.compile(r"\b[aA-zZ]\b")
     for word in User_input:
         User_Input_list.append(word?)

# going by thin put example input I'd want
# User_Input_list["steve", "at" , "the" , "office"] 

I'm unsure how to split the input up into separate words? I will give cookies for help!

Upvotes: 0

Views: 6287

Answers (6)

David Webb
David Webb

Reputation: 193686

The easiest solution is probably to use split:

>>> "steve at the office".split()
['steve', 'at', 'the', 'office']

But this won't remove punctuation, which may or may not be a problem for you:

>>> "steve at the office.".split()
['steve', 'at', 'the', 'office.']

You could use re.split() to only pluck out letters:

>>> re.split('\W+', 'steve at the office.')
['steve', 'at', 'the', 'office', '']

But as you can see above you might end up with empty entries to deal with, and things worse when you have more subtle punctuation:

>>> re.split("\W+", "steve isn't at the office.")
['steve', 'isn', 't', 'at', 'the', 'office', '']

So you could do some work here to pick a better regular expression, but you'll need to decide how you want to handle text like steve isn't at the 'the office'.

So to select the right solution for you, you'll have to think about what input you'll get and what output you want.

Upvotes: 1

Sebastian Blask
Sebastian Blask

Reputation: 2938

You found re already, there is a nice example of splitting a string:

re.split('\W+', 'Words, words, words.')

Like this you get all words, all punctuation removed.

Upvotes: 0

Francesco Turci
Francesco Turci

Reputation: 837

Do the following

User_input = raw_input("Please enter a search criterion: ")

User_Input_list = User_input.split()

Upvotes: 0

trickwallett
trickwallett

Reputation: 2468

User_input = raw_input("Please enter a search criterion: ")
User_Input_list = User_input.split(" ")

see:

http://docs.python.org/library/stdtypes.html

Upvotes: 0

Oleiade
Oleiade

Reputation: 6274

Basicaly,

you should do this:

User_Input_list = User_input.split(' ')

and that's it...

Upvotes: 0

theheadofabroom
theheadofabroom

Reputation: 22019

User_Input_list = User_input.split()

Upvotes: 3

Related Questions