Re: Index of entity in List with a Condition

2018-06-12 Thread Cameron Simpson

On 12Jun2018 21:06, Subhabrata Banerjee  wrote:

Dear Sir,
Thank you for your kind reply. I am trying in few days time and getting back. 
I made a small fix of my own and I would discuss it, too. Thank you for your 
kind words, but I ignore unnecessary remarks. So please do not worry. 
Meanwhile, please see as you are posting your mail id is coming here, it may 
be misused. If possible, please omit it as you post next time.


I have long considered my email id to be impossible to conceal, so I usually 
don't try. Please don't worry if it is misused by others, that is a risk I have 
accepted.


Of course, as consideration to others, I try to avoid reciting someone else's 
contact info unless it is already publicly in play where I am.


Cheers,
Cameron Simpson 
--
https://mail.python.org/mailman/listinfo/python-list


Re: Index of entity in List with a Condition

2018-06-12 Thread subhabangalore
On Wednesday, June 13, 2018 at 6:30:45 AM UTC+5:30, Cameron Simpson wrote:
> On 11Jun2018 13:48, Subhabrata Banerjee 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.
> 
> You will probably have to drop the list comprehension and go with something 
> more elaborate.
> 
> Also, lists have an "index" method:
> 
>   >>> L = [4,5,6]
>   >>> L.index(5)
>   1
> 
> though it doesn't solve your indexing problems on its own.
> 
> I would be inclined to deconstuct the sentence into a cross linked list of 
> elements. Consider making a simple class to encapsulate the knowledge about 
> each word (totally untested):
> 
>   class Word:
> def __init__(word):
>   self.word = word
> 
>   words = []
>   for index, word in sentence.split():
> W = Word(word)
> W.index = index
> words.append(W)
> W.wordlist = words
> 
> Now you have a list of Word objects, each of which knows its list position 
> _and_ also knows about the list itself, _and_ you have the list of Word 
> objects 
> correspnding to your sentence words.
> 
> You'll notice we can just hang whatever attributes we like off these "Word" 
> objects: we added a .wordlist and .index on the fly. It isn't great formal 
> object design, but it makes building things up very easy.
> 
> You can add methods or properties to your class, such as ".next":
> 
>   @property
>   def next(self):
> return self.wordlist[self.index - 1]
> 
> and so forth. That will let you write expressions about Words:
> 
>   for W in wordlist:
> if W.word == 'of' and W.next.word == 'the' and W.next.next.word == 
> 'United' ...:
>   if W.previous.word != 'president':
> ... oooh, unexpected preceeding word! ...
> 
> You can see that you could also write methods like "is_preceeded_by":
> 
>   def is_preceed_by(self, word2):
> return self.previous.word == word2
> 
> and test "W.is_preceeded_by('president')".
> 
> In short, write out what you would like to express. Then write methods that 
> implement the smaller parts of what you just wrote.
> 
> Cheers,
> Cameron Simpson https://mail.python.org/mailman/listinfo/python-list


helpfulness Re: Index of entity in List with a Condition

2018-06-12 Thread Cameron Simpson

On 11Jun2018 18:03, Rick Johnson  wrote:

subhaba...@gmail.com wrote:

I have the following sentence,

"Donald Trump is the president of United States of America".

[...]


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.


Rick, this is unhelpful.

Firstly, Subhabrata isn't getting us to do his work for him; he's posted here 
several times in the past and clearly is working on probems in good faith with 
demonstrated willingness _and_ ability to think things through in detail. He 
comes here having tried things, with specific questions following from problems 
he has encountered.


Secondly, his question is reasonably expressed. Lacking a lot of code, it is 
perfectly feasible and reasonable to respond in general prose form instead of 
purely code.


Finally, adding in political editorial in your code response doesn't improve 
the discussion environment.


If you want to encourage more detail from him, just ask without overtones.

If you're unwilling to engage with his post for whatever criteria, just don't 
engage. Easy!


It is better for all concerned, both the OP and we the list readers.

Thanks,
Cameron Simpson 


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
-- https://mail.python.org/mailman/listinfo/python-list

--
https://mail.python.org/mailman/listinfo/python-list


Re: Index of entity in List with a Condition

2018-06-12 Thread Cameron Simpson

On 11Jun2018 13:48, Subhabrata Banerjee  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.


You will probably have to drop the list comprehension and go with something 
more elaborate.


Also, lists have an "index" method:

 >>> L = [4,5,6]
 >>> L.index(5)
 1

though it doesn't solve your indexing problems on its own.

I would be inclined to deconstuct the sentence into a cross linked list of 
elements. Consider making a simple class to encapsulate the knowledge about 
each word (totally untested):


 class Word:
   def __init__(word):
 self.word = word

 words = []
 for index, word in sentence.split():
   W = Word(word)
   W.index = index
   words.append(W)
   W.wordlist = words

Now you have a list of Word objects, each of which knows its list position 
_and_ also knows about the list itself, _and_ you have the list of Word objects 
correspnding to your sentence words.


You'll notice we can just hang whatever attributes we like off these "Word" 
objects: we added a .wordlist and .index on the fly. It isn't great formal 
object design, but it makes building things up very easy.


You can add methods or properties to your class, such as ".next":

 @property
 def next(self):
   return self.wordlist[self.index - 1]

and so forth. That will let you write expressions about Words:

 for W in wordlist:
   if W.word == 'of' and W.next.word == 'the' and W.next.next.word == 'United' 
...:
 if W.previous.word != 'president':
   ... oooh, unexpected preceeding word! ...

You can see that you could also write methods like "is_preceeded_by":

 def is_preceed_by(self, word2):
   return self.previous.word == word2

and test "W.is_preceeded_by('president')".

In short, write out what you would like to express. Then write methods that 
implement the smaller parts of what you just wrote.


Cheers,
Cameron Simpson 
--
https://mail.python.org/mailman/listinfo/python-list


Re: Index of entity in List with a Condition

2018-06-11 Thread Rick Johnson
subhaba...@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
-- 
https://mail.python.org/mailman/listinfo/python-list