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 9208367
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T00:36:07+00:00 2026-06-18T00:36:07+00:00

I have the following python code: import regex original = the quick ‘ brown

  • 0

I have the following python code:

import regex
original = " the  quick ' brown 1 fox! jumps-over the 'lazy' doG? !  "
s = [i for i in original.split(" ")]

I want to write a function called get_sentence which takes an element within s and returns the sentence as a string to which the element belongs. For example:

"brown" ->  "the  quick ' brown 1 fox!"

if the first “the” is passed to the function, then:

"the" -> the  quick ' brown 1 fox!"

if the second the:

"the" -> "jumps-over the 'lazy' doG?"

What would you pass as an argument to such a function? In C++ I might pass in an std::vector::const_iterator. In C I would pass in an int (array index) or maybe even a pointer.

  • 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-18T00:36:08+00:00Added an answer on June 18, 2026 at 12:36 am
    >>> from itertools import product, chain
    >>> #Assuming your original sentence is
    >>> origional = " the  quick ' brown 1 fox! jumps-over the 'lazy' doG? !  "
    >>> #Sentence terminators are
    >>> sent_term = "[?!.;]"
    >>> #I will use regex to split it into sentences
    >>> re.split(sent_term, origional.strip())
    ["the  quick ' brown 1 fox", " jumps-over the 'lazy' doG", ' ', '']
    >>> #And then split it as words
    >>> #I could have used str.split, but that would include punctuations
    >>> #Which you may not be interested
    >>> #For each of the words, I create a mapping with the sentence using product
    >>> word_map = ((product(re.split("\W",e),[e])) 
                     for e in re.split(sent_term, origional.strip()))
    >>> #Chain it as a single list
    >>> word_map = chain(*((product(re.split("\W",e),[e])) 
                            for e in re.split(sent_term, origional.strip())))
    >>> from collections import defaultdict
    >>> #Create a default dict
    >>> words = defaultdict(list)
    >>> #And populated all non trivial words
    >>> for k, v in word_map:
        if k.strip():
            words[k]+=[v]
    
    
    >>> words
    defaultdict(<type 'list'>, {'brown': ["the  quick ' brown 1 fox"], 'lazy': [" jumps-over the 'lazy' doG"], 'jumps': [" jumps-over the 'lazy' doG"], 'fox': ["the  quick ' brown 1 fox"], 'doG': [" jumps-over the 'lazy' doG"], '1': ["the  quick ' brown 1 fox"], 'quick': ["the  quick ' brown 1 fox"], 'the': ["the  quick ' brown 1 fox", " jumps-over the 'lazy' doG"], 'over': [" jumps-over the 'lazy' doG"]})
    >>> #Now to get the first word
    >>> words['the'][0]
    "the  quick ' brown 1 fox"
    >>> #Now to get the second sentence
    >>> words['the'][1]
    " jumps-over the 'lazy' doG"
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following code in python from selenium import webdriver from selenium.webdriver.common.by import
I have the following python code: import pty import subprocess os=subprocess.os from subprocess import
I have the following chunk of python code: import hashlib class User: def _set_password(self,
I have the following python code... import Tkinter root = Tkinter.Tk() root.tk.eval('puts {printed by
I have the following Python (3.2) code: from pygame import * class Application: def
I have the following python code: import logging import logging.config logging.config.fileConfig('a.conf') logging.debug('aaaa') with the
I have the following python code: from django.db import models from datetime import datetime
I have written the following Python code: #!/usr/bin/python # -*- coding: utf-8 -*- import
I'm having an issues with bottle python where I have the following code import
I have the following python code. import sys ins = open( sys.argv[1], "r" )

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.