Question on List

Reply

Join Date: Apr 2008
Posts: 10
Reputation: SUBHABRATAIISC is an unknown quantity at this point 
Solved Threads: 0
SUBHABRATAIISC SUBHABRATAIISC is offline Offline
Newbie Poster

Question on List

 
0
  #1
Jun 27th, 2008
Dear All,
I am trying to write the following code:

def try1(n):
a1="God Godess Borother Sister Family"
a2=a1.split()
a3=raw_input("PRINT A WORD")
a4=a1.find(a3)
print a4
a5=[]
if a4>0:
a5=a2.index(a3)
a6=a5+1
a7=a2[a6]
print "The new word is"
print a7
a8=a5.append(a7)
print a5
elif a4<0:
a11=a3
print "The word is not availiable in String"
print a11
a6=a5.append(a11)
print a5
else:
print "Error"


Now, my question is I like to see a5 or the empty list as appended
with elements. Results I am getting is a5 giving single value like
['God'],['Godess']... but I like to see it as ['God','Godess'...] etc.
Am I going wrong?
Do I have to rethink it in some other way?
If any one can kindly let me know.
Best Regards,
Subhabrata.
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 976
Reputation: woooee is a jewel in the rough woooee is a jewel in the rough woooee is a jewel in the rough 
Solved Threads: 273
woooee woooee is offline Offline
Posting Shark

Re: Question on List

 
0
  #2
Jun 27th, 2008
I would do something like this. Note that in your original code, the first word, "God", would never be found because you don't allow for a4==0.
  1. a1="God Godess Borother Sister Family DemiGod"
  2. a1_list=a1.lower().split()
  3. a5=[]
  4. input_word=raw_input("PRINT A WORD ")
  5. for each_word in a1_list:
  6. if input_word in each_word:
  7. a5.append(each_word)
  8. print a5
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 241
Reputation: ssharish2005 is on a distinguished road 
Solved Threads: 20
ssharish2005's Avatar
ssharish2005 ssharish2005 is offline Offline
Posting Whiz in Training

Re: Question on List

 
0
  #3
Jun 27th, 2008
May be you should be looking for something like this

  1. from string import split, lower
  2. import os, string
  3.  
  4. strOriginal = "God Godess Brother Sister Family"
  5. strFinal = []
  6.  
  7. strTofind = raw_input("Enter a word ot search")
  8.  
  9. for token in split(strOriginal, " "):
  10. if string.find(lower(token), strTofind) != -1:
  11. strFinal.append(token)
  12.  
  13. print "The final string"
  14. print strFinal
  15.  
  16. ##my output
  17. ##The final string
  18. ##['God', 'Godess']

NOTE: Use code tags always [code=python] <place your code here> [code]

ssharish
Last edited by ssharish2005; Jun 27th, 2008 at 9:01 pm.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC