试试这个程序!在
它会完美工作的!
另外,我附上了程序的输出。在y1=['fem j sex / male \n', " father's name diwan singh saggu \n", "elector's name rahul saggu \n", 'identity card \n', 'zfk0281501', 'age as on 1.1.2008 23 \n']
word=["sex","father's","name","elector's","identity","card","age","\n"]
output =[] //output list
for i in range(0,len(y1)):
for j in range(0,len(word)):
a=y1[i].find(word[j]) //finds the word in a y1 list , if it found & returns index greater than -1 than replace the substring in a y1 list with empty string ' '
if a!=-1:
y1[i]=y1[i].replace(word[j],'')
y1[i]=y1[i].strip() //removes the leading & trailing whitespaces
if y1[i]!='':
output.append(y1[i]) // adds into the 'output' list
print(output)