Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8949171
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T13:10:26+00:00 2026-06-15T13:10:26+00:00

I am looking for pythonic way to split a sentence into words, and also

  • 0

I am looking for pythonic way to split a sentence into words, and also store the index information of all the words in a sentence e.g

a = "This is a sentence"
b = a.split() # ["This", "is", "a", "sentence"]

Now, I also want to store the index information of all the words

c = a.splitWithIndices() #[(0,3), (5,6), (8,8), (10,17)]

What is the best way to implement splitWithIndices(), does python have any library method that I can use for that. Any method that helps me calculate the indices of the word would be great.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T13:10:27+00:00Added an answer on June 15, 2026 at 1:10 pm

    I think it’s more natural to return the start and end of the corresponding splices. eg (0, 4) instead of (0, 3)

    >>> from itertools import groupby
    >>> def splitWithIndices(s, c=' '):
    ...  p = 0
    ...  for k, g in groupby(s, lambda x:x==c):
    ...   q = p + sum(1 for i in g)
    ...   if not k:
    ...    yield p, q # or p, q-1 if you are really sure you want that
    ...   p = q
    ...
    >>> a = "This is a sentence"
    >>> list(splitWithIndices(a))
    [(0, 4), (5, 7), (8, 9), (10, 18)]
    
    >>> a[0:4]
    'This'
    >>> a[5:7]
    'is'
    >>> a[8:9]
    'a'
    >>> a[10:18]
    'sentence'
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm looking for a fast, clean, pythonic way to divide a list into exactly
I'm looking for a Pythonic way to get all the diagonals of a (square)
I want to split my list into rows that have all the same number
I am looking for a Pythonic way to simplify this code: fix = re.compile(r'((?<=>\n)(\t){2}(?=<))')
Looking for a perl one-liner what will find all words with the next pattern:
I'm looking for a pythonic way of iterating over first n items of an
I am looking for the most pythonic way of splitting a list of numbers
I am looking for the most pythonic way to replace the first and last
I am looking for a Pythonic way to solve the following problem. I have
I'm looking for the most pythonic way to implement a version of the list

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.