LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   Python: pull words from a text, setting them as variables (http://www.linuxquestions.org/questions/programming-9/python-pull-words-from-a-text-setting-them-as-variables-795317/)

General 03-14-2010 08:38 AM

Python: pull words from a text, setting them as variables
 
I've got a list of verbs:

Code:

verbs = ['eat', 'fly', 'look', 'throw', 'take']
And a text:

Code:

text = "eatcatthrowballthenlookforthesnake"
I've used this to replace all of the verbs in the text with "VERB":

Code:

for x in verbs:
        if re.search(x, text):
                text = text.replace(x, str('VERB'))

So if I do:

Code:

>> print text
I get:

Code:

VERBcatVERBballthenVERBforthesnake
So far so good.

I need to add another part, but I don't know any way to impliment it. As you saw above, three verbs appeared in the text, I'd like to set them each as variables, in the same order that they appeared in the text:

verb1='eat'
verb2='throw'
verb3='look'**

Thus, (in maybe an absurd example) if the text has 10,000 verbs, the last one will be called verb10000='fly'.

Just as another example, a text such as this:

Code:

text = "eatfoodeatcandydonoteatsnakesthrowthecloud"
Results in:

verb1='eat'
verb2='eat'
verb3='eat'
verb4='throw'

**(or a similar way to create and manage lots of variables)

Sergei Steshenko 03-14-2010 08:46 AM

Quote:

Originally Posted by General (Post 3897741)
I've got a list of verbs:

Code:

verbs = ['eat', 'fly', 'look', 'throw', 'take']
And a text:

Code:

text = "eatcatthrowballthenlookforthesnake"
I've used this to replace all of the verbs in the text with "VERB":

Code:

for x in verbs:
        if re.search(x, text):
                text = text.replace(x, str('VERB'))

So if I do:

Code:

>> print text
I get:

Code:

VERBcatVERBballthenVERBforthesnake
So far so good.

I need to add another part, but I don't know any way to impliment it. As you saw above, three verbs appeared in the text, I'd like to set them each as variables, in the same order that they appeared in the text:

verb1='eat'
verb2='throw'
verb3='look'**

Thus, (in maybe an absurd example) if the text has 10,000 verbs, the last one will be called verb10000='fly'.

Just as another example, a text such as this:

Code:

text = "eatfoodeatcandydonoteatsnakesthrowthecloud"
Results in:

verb1='eat'
verb2='eat'
verb3='eat'
verb4='throw'

**(or a similar way to create and manage lots of variables)

I am not a Python guy, but why do you need to assign verbs to different variables and not to, say, elements of an elastic array.

The point is that in whatever language dealing with unknown at compilation time variables poses risks like name contention and nuisances like checking whether a to be created variable has actually been created.

The approach for creating variables at runtime is 'eval', but, again, am not sure you really need such variables.

ghostdog74 03-14-2010 09:14 AM

Quote:

Originally Posted by General (Post 3897741)
Thus, (in maybe an absurd example) if the text has 10,000 verbs, the last one will be called verb10000='fly'.

Just as another example, a text such as this:

Code:

text = "eatfoodeatcandydonoteatsnakesthrowthecloud"
Results in:

verb1='eat'
verb2='eat'
verb3='eat'
verb4='throw'

**(or a similar way to create and manage lots of variables)

just use a dictionary to store the found words

Code:

d={}
i=0
for x in verbs:
        if re.search(x, text):
                text = text.replace(x, str('VERB'))
                i=i+1           
                d["verb_"+str(i)]=x



All times are GMT -5. The time now is 04:46 PM.