OSDir


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Index of entity in List with a Condition


subhaba... at gmail.com wrote:
> I have the following sentence,
> 
> "Donald Trump is the president of United States of America". 
> 
> I am trying to extract the index 'of', not only for single but also
> for its multi-occurance (if they occur), from the list of words of the
> string, made by simply splitting the sentence.
>  index1=[index for index, value in enumerate(words) if value == "of"],
> where words=sentence.split()
> 
> I could do this part more or less nicely. 
> 
> But I am trying to say if the list of words has the words "United"
> and "States" and it has "of " in the sentence then the previous 
> word before of is, president. 
> 
> I am confused how may I write this, if any one may help it.
> 
> Thanking in advance.

Typically when you ask us to do your homework for you, it is
considered bad taste to present the teacher's assignment
verbatim and then expect that we will provide a turn-key
solution. And although you _did_ provide some sort of
"Pythony looking" code, unfortunately the code is poorly
formatted.

Next time, please try to present your question in a formal,
well-though-out manner. Source code should either be
executable, or conspicuously labeled as psuedo code. Not
because we don't _know_ what pseudo code looks like, but
because we can intuit your level of knowledge from the
presentation. And i gotta tell ya, this presentation is not
exactly screaming valedictorian -- but i digress.

Now, as to your problem...

Well, first, hold on a second, because, i want to correct your
sentence. You presented your target string as:

    "Donald Trump is the president of United States of America"
    
No-no-no. This sentence seems to be missing a few things.
The first is a three letter word. And let's insert that word
now...

    >>> s = "Donald Trump is the president of United States of America"
    >>> s.index('U')
    33
    >>> s = s[:33] + 'the ' + s[33:]
    >>> s
    'Donald Trump is the president of the United States of America'
    
Ah yes. You see how much more smoothly that rolls off the
ol' tongue? Now, in the interest of public awareness, let's
add an addendum, shall we?

    >>> s += ", who was duly elected to office by the great people of this fine country in a free and open election. And no amount of whining; pouting; sniveling; conspiratorial hoopla; flailing of the arms; the legs; or any combination thereof for that matter; will change the reality that Donald *BIG JOHN* Trump is now the president of this fine country. Seriously folks. It's been an interesting ride. But the circus is over now. Elvis left the building over a _year_ ago. Heck, Jimmy Hoffa has already rolled over in his grave *THREE* times! But most disturbing of all, is that the dozens of emaciated cats trapped in each of your apartments are on the verge of cannibalism (yesh, you forgot about them, didn't you?), and the toxic ammonia fumes are melting the paint off the walls! It's time to go home and lick your wounds folks. Better luck next time. Aloha."
    
Whew! (*wipes brow*) Now that we've gotten all of that out of
the way... what was your question again? o_O