LinuxQuestions.org
Register a domain and help support LQ
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
LinkBack Search this Thread
Old 03-14-2010, 08:38 AM   #1
General
Member
 
Registered: Aug 2005
Distribution: Debian 6.0
Posts: 465

Rep: Reputation: 31
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)
 
Old 03-14-2010, 08:46 AM   #2
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 451Reputation: 451Reputation: 451Reputation: 451Reputation: 451
Quote:
Originally Posted by General View Post
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.
 
Old 03-14-2010, 09:14 AM   #3
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,695
Blog Entries: 5

Rep: Reputation: 239Reputation: 239Reputation: 239
Quote:
Originally Posted by General View Post
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
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Separate words (Python) General Programming 5 12-19-2009 06:09 PM
Python Reading and Declaring Variables from a Text File dudeman41465 Programming 7 01-31-2009 04:54 PM
bash - pull out variables from a | deliminated string elinenbe Programming 11 02-15-2008 11:36 PM
python, os.system() function. howto use python variables? jhwilliams Programming 5 07-28-2007 01:56 AM
Problem loading file of words in python Teoryn Programming 1 07-25-2005 07:40 PM


All times are GMT -5. The time now is 02:35 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration