Problem with list.insert
SUBHABRATA
subhabrata.iisc at hotmail.com
Thu Aug 28 12:13:00 EDT 2008
Dear Group,
I wrote one program,
There is a dictionary.
There is an input string.
Every word of input string the word is matched against the dictionary
If the word of input string is matched against the dictionary it gives
the word of the dictionary.
But if it does not find it gives the original word.
After searching the words are joined back.
But as I am joining I am finding the words which are not available in
dictionary are printed in the last even if the word is given in the
first/middle.
Now, I want to take them in order.
I am applying a thumb rule that the position of the word of the string
is exact with the resultant string.
So, I am determining the word which is not in the dictionary, and its
position in the input string.
Now I am inserting it in the target string, for this I am splitting
both the given string and the output/result string.
Till now it is working fine.
But a problem happening is that if I insert it it is inserting same
words multiple times and the program seems to be an unending process.
What is the error happening?
If any one can suggest.
The code is given below:
import re
def wordchecker1(n):
# INPUTTING STRING
a1=raw_input("PRINT ONE ENGLISH SENTENCE FOR DICTIONARY CHECK:")
#CONVERTING TO LOWER CASE
a2=a1.lower()
#CONVERTING INTO LIST
a3=a2.split()
#DICTIONARY
a4=open("/python25/Changedict3.txt","r")
a5=a4.read()
a6=a5.split()
found=[]
not_found=[]
#SEARCHING DICTIONARY
for x in a3:
a7="\n"
a8=a7+x
if a8 in a5:
a9=a5.index(a8)
a10=a5[a9:]
a11=re.search("\xe0.*?\n",a10)
a12=a11.group()
a13=a12[:-1]
found.append(a13)
elif a8 not in a5:
a14=x
not_found.append(a14)
else:
print "Error"
found.extend(not_found)
# THE OUTPUT
print "OUTPUT STRING IS"
a15=(' '.join(found))
#THE OUTPUT STRING
print a15
# SPLITTING OUTPUT STRING IN WORDS
a16=a15.split()
#TAKING OUT THE WORD FROM OUTPUT STRING
for word in a16:
#MATCHING WITH GIVEN STRING
a17=a2.find(word)
if a17>-1:
print "The word is found in the Source String"
a18=a3.index(word)
a19=a3[a18]
print a19
#INSERTING IN THE LIST OF TARGET STRING
a20=a16.insert(a18,a19)
print a16
a21=(" ".join(a16))
print a21
Best Regards,
Subhabrata.
More information about the Python-list
mailing list