Converting a String to a List of Words

Converting a String to a List of Words?

Try this:

import re

mystr = 'This is a string, with words!'
wordList = re.sub("[^\w]", " ", mystr).split()

How it works:

From the docs :

re.sub(pattern, repl, string, count=0, flags=0)

Return the string obtained by replacing the leftmost non-overlapping occurrences of pattern in string by the replacement repl. If the pattern isn’t found, string is returned unchanged. repl can be a string or a function.

so in our case :

pattern is any non-alphanumeric character.

[\w] means any alphanumeric character and is equal to the character set
[a-zA-Z0-9_]

a to z, A to Z , 0 to 9 and underscore.

so we match any non-alphanumeric character and replace it with a space .

and then we split() it which splits string by space and converts it to a list

so 'hello-world'

becomes 'hello world'

with re.sub

and then ['hello' , 'world']

after split()

let me know if any doubts come up.

List of strings to list of words

You can try like this (rstrip the string from . rather than a strip and split around white-spaces, join the lists obtained after splitting via sum):

>>> sum([i.rstrip(".").split(" ") for i in list_of_strings], [])
['This', 'is', 'string', 'one', 'This', 'is', 'string', 'two', 'This', 'is', 'string', 'three']

How do I split a string into a list of words?

Given a string sentence, this stores each word in a list called words:

words = sentence.split()

Swift: converting string to a list of word and range in the string

You can use String method enumerateSubstrings(in:, options:) and use byWords options:

func enumerateSubstrings<R>(in range: R, options opts: EnumerationOptions = [], _ body: @escaping (String?, Range<Index>, Range<Index>, inout Bool) -> Void) where R : RangeExpression, R.Bound == Index


extension String {
var byWordsAndRanges: [(String, Range<Index>)] {
var wordsAndRanges: [(String, Range<Index>)] = []
enumerateSubstrings(in: startIndex..., options: .byWords) { word, range, _, _ in
// The force unwrap of word is safe. It will only be nil if `substringNotRequired` is included in opts
wordsAndRanges.append((word!, range))
}
return wordsAndRanges
}
}


let test = "a fox jumps over another fox"
for (word, range) in test.byWordsAndRanges {
print("word:", word)
print("substring:", test[range])

}

This will print:

word: a

substring: a

word: fox

substring: fox

word: jumps

substring: jumps

word: over

substring: over

word: another

substring: another

word: fox

substring: fox

How can I convert single word string to a list?

stringInput="Banana"
listString=list(stringInput)

That's it. Easy peasy!



Related Topics



Leave a reply



Submit