Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

nth word in a text

Tags:

python

string

how can I find the nth word in a text.

example:

my_txt("hello to you all" , 3)

all

I don't wanna use any built in function ...and this is not a homework :D

like image 582
user531225 Avatar asked Nov 30 '22 05:11

user531225


2 Answers

The obvious way to do it is:

"hello to you all".split()[3]

The 80's way to do it is - that is, you have to walk the text, keeping note of the state of things you have found - it can become better than this, probably, but that is the idea. Perceive one has to use a lot o "built-in" functions either way. I just avoid the ones that make it straight like above.

def my_txt(text, target):
    count = 0
    last_was_space = False
    start = end = 0
    for index, letter in enumerate(text):
        if letter.isspace():
            if not last_was_space:
                 end = index
            last_was_space = True
        elif last_was_space:
            last_was_space = False
            count += 1
            if count > target:
                return text[start:end]
            elif count == target:
                start = index
    if count == target:
        return text[start:].strip()
    raise ValueError("Word not found")
like image 125
jsbueno Avatar answered Dec 05 '22 01:12

jsbueno


OK you asked for this. You need a "split into words" function. Here it is. Assumes that "words" are delimited by whitespace.

No built-in functions, no imported anythings, no methods of built-in types, not even any panty-waist stuff like +=. And it's tested.

C:\junk>\python15\python
Python 1.5.2 (#0, Apr 13 1999, 10:51:12) [MSC 32 bit (Intel)] on win32
Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam
>>> def mysplit(s):
...     words = []
...     inword = 0
...     for c in s:
...         if c in " \r\n\t": # whitespace
...             inword = 0
...         elif not inword:
...             words = words + [c]
...             inword = 1
...         else:
...             words[-1] = words[-1] + c
...     return words
...
>>> mysplit('')
[]
>>> mysplit('x')
['x']
>>> mysplit('foo')
['foo']
>>> mysplit('  foo')
['foo']
>>> mysplit('  foo    ')
['foo']
>>> mysplit('\nfoo\tbar\rzot ugh\n\n   ')
['foo', 'bar', 'zot', 'ugh']
>>>
like image 36
John Machin Avatar answered Dec 05 '22 02:12

John Machin