Sultan Zaki
Sultan Zaki

Reputation: 21

Splitting words and take specific words

I have a problem with my python script, it's a straightforward problem but I can't resolve it. For example, I have 11 words text and then I split using re.split(r"\s+", text) function

import re

text = "this is the example text and i will splitting this text"
split = re.split(r"\s+", text)
for a in (range(len(split))):
    print(split[a])

The result is

this
is
the
example
text
and
i
will
splitting
this
text

I only need to take 10 words from 11 words, so the result I need is only like this

is
the
example
text
and
i
will
splitting
this
text

Can you solve this problem? It will very helpful Thank you!

Upvotes: 0

Views: 53

Answers (2)

Ryszard Czech
Ryszard Czech

Reputation: 18611

No need of regex:

text = "this is the example text and i will splitting this text"
l = text.split()  # Split with whitespace
l.pop(0)          # Remove first item
print(l)          # Print the results

Results: ['is', 'the', 'example', 'text', 'and', 'i', 'will', 'splitting', 'this', 'text']

See Python proof.

Upvotes: 1

crayxt
crayxt

Reputation: 2405

Just index like that

>>> import re
>>>
>>> text = "this is the example text and i will splitting this text"
>>> split = re.split(r"\s+", text)
>>> split
['this', 'is', 'the', 'example', 'text', 'and', 'i', 'will', 'splitting', 'this', 'text']
>>> split[-10:]
['is', 'the', 'example', 'text', 'and', 'i', 'will', 'splitting', 'this', 'text']

Upvotes: 1

Related Questions