| | |
Question on List
![]() |
•
•
Join Date: Apr 2008
Posts: 10
Reputation:
Solved Threads: 0
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.
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.
•
•
Join Date: Dec 2006
Posts: 976
Reputation:
Solved Threads: 273
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.
Python Syntax (Toggle Plain Text)
a1="God Godess Borother Sister Family DemiGod" a1_list=a1.lower().split() a5=[] input_word=raw_input("PRINT A WORD ") for each_word in a1_list: if input_word in each_word: a5.append(each_word) print a5
May be you should be looking for something like this
NOTE: Use code tags always [code=python] <place your code here> [code]
ssharish
python Syntax (Toggle Plain Text)
from string import split, lower import os, string strOriginal = "God Godess Brother Sister Family" strFinal = [] strTofind = raw_input("Enter a word ot search") for token in split(strOriginal, " "): if string.find(lower(token), strTofind) != -1: strFinal.append(token) print "The final string" print strFinal ##my output ##The final string ##['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.
![]() |
Similar Threads
- Question RE using Array Values in Case/If statements (Visual Basic 4 / 5 / 6)
- how to generate a mysql query from a english question (MySQL)
- how to generate a mysql query from a english question #1 (Java)
- Icons at left of list of Threads (DaniWeb Community Feedback)
- Algorithm: BNF question (C)
- Seg Fault ~ Linked List Delete (C)
- xp professional video question HELP! (Windows NT / 2000 / XP)
- Question (C)
Other Threads in the Python Forum
- Previous Thread: py2app chokes on MySQLdb
- Next Thread: Append Results to ListBox
| Thread Tools | Search this Thread |
abrupt alarm ansi anti apache approximation array assignment backend beginner binary bluetooth builtin calculator character cmd converter countpasswordentry curved customdialog cx-freeze dan08 data decimals dictionary directory exe file float format function gnu halp heads homework http ideas inches input itunes java leftmouse library line lines linux list lists loop module mouse mysqlquery number numbers numeric output parsing path phonebook pointer prime programming push py2exe pygame python random recursion redirect schedule screensaverloopinactive script scrolledtext software sqlite ssh statictext statistics string strings sudokusolver terminal text thread time tlapse tuple twoup ubuntu unicode urllib urllib2 variable ventrilo webservice wikipedia wordgame write wxpython xlib





