-7

I have two lists,

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","name","identity","card","age"]

I need to remove all the strings in y1 which is in word. I need to get output as

output=['fem j /male','diwan singh saggu','rahul saggu','zfk0281501','as on 1.1.2008 23']

I have split individual elements in y1 and tried comparing it with word.But i dont know what to do next? How to remove the strings??Here is what i tried,

y1new=[]
for y in y1:
    tmp=y.split()
    y1new.append(tmp)
for i in y1new:
    for j in i:
        if j in word:
            y1new[i].remove(y1new[i][j])

How can i achieve this?

VPJ
  • 53
  • 2
  • 9
  • I tried using the split() to split the strings and then searched those strings with word.If it came true i used replace() to remove from y1.But it didn't worked. – VPJ Jan 20 '18 at 07:59

6 Answers6

0

CODE:

temp = ""
for y1_sentence in y1:
    y1_word = y1_sentence.split(" ")

    for i in y1_word:
        if i not in word:
            temp = temp + " " + i
    output.append(temp)
    temp = ""

real_output = []

for output_string in output:
    temp1 = output_string.strip()
    real_output.append(temp1)

Code

Output

khush
  • 31
  • 7
-1

Good morning,

there is a function in python called str.replace(old, new[, max]).

old stands for the old substring to be replaced.

new stands for the new substring, which would replace old substring.

max is optional, not needed in your case.

It is also important to mention that strings are immutable in python. That means you have to assign the return value of replace() to your used variable.

for x in y1:
    for w in word:
        x = x.replace(w, "")

This should work just fine, but I am sure there is a smarter way to do it in Python. Just have a look here for example:https://www.tutorialspoint.com/python/string_replace.htm

Andre
  • 4,185
  • 5
  • 22
  • 32
  • I tried using replace(),but it didnt came as i expected.May be my logic is wrong, – VPJ Jan 20 '18 at 07:57
  • It is the best way. Have a look at older questions and at the answers: https://stackoverflow.com/questions/3559559/how-to-delete-a-character-from-a-string-using-python – Andre Jan 20 '18 at 08:04
-1

Try this program !

It will work perfectly ! Also I am attach the output of the program .

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)

enter image description here

Usman
  • 1,983
  • 15
  • 28
-1

Thanks for the help guys,i finally managed to solve the problem.Don't mind the variable names.I was too lazy there.

output=[]
y1new=[]
p=[]
word=["sex","father's","name","elector's","name","identity","card","age"]
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']

for y in y1:
    tmp=y.split()
    y1new.append(tmp)


for y in range(0,len(y1new)):
    tmp=y1new[y]
    for i in range(0,len(tmp)):
        if tmp[i] in word:
            p.insert(0,y1[y].replace(str(tmp[i])," " ))
            y1.remove(y1[y])
            y1.insert(y,p[0].replace(str(tmp[i])," " ))
for i in y1:
    tp=i.split()
    tp = ' '.join(tp)
    output.append(tp)
...........................................................................................

output

output
Out[14]: 
['fem j / male',
 'diwan singh saggu',
 'rahul saggu',
 '',
 'zfk0281501',
 'as on 1.1.2008 23']
VPJ
  • 53
  • 2
  • 9
-1

You can use regex:

import re
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","name","identity","card","age"]
new_y1 = [re.sub('\n+|(?<=^)\s+|\s+(?=$)', '', re.sub('|'.join(word), '', i)) for i in y1]

Output:

['fem j  / male', 'diwan singh saggu', 'rahul saggu', '', 'zfk0281501', 'as on 1.1.2008   23']
Ajax1234
  • 69,937
  • 8
  • 61
  • 102
-1

Try something like this:

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","name","identity","card","age"]

result=[]
for j in y1:
    data=j.split()
    for m,k in enumerate(data):
        if k in word:
            del data[m]
    result.append(" ".join(data))

print(result)

output:

['fem j / male', 'name diwan singh saggu', 'name rahul saggu', 'card', 'zfk0281501', 'as on 1.1.2008 23']