如何从字符串列表中删除特定子字符串?

时间:2018-01-20 07:32:25

标签: python string list

我有两个清单,

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"]

我需要删除y1word中的所有字符串。 我需要输出

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

我在y1中拆分了单个元素,并尝试将其与word进行比较。但我不知道下一步该做什么?如何删除字符串?这是我试过的,

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])

我怎样才能实现这个目标?

6 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:0)

早上好,

python中有一个名为str.replace(old, new[, max])的函数。

old代表要替换的旧子字符串。

new代表新的子字符串,它将替换旧的子字符串。

max是可选的,在您的情况下不需要。

提及字符串在python中是不可变的也很重要。这意味着您必须将replace()的返回值分配给已使用的变量。

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

这应该可以正常工作,但我确信在Python中有更聪明的方法。请看这里例如:https://www.tutorialspoint.com/python/string_replace.htm

答案 2 :(得分:0)

试试这个程序!

它会完美运作! 另外我附上程序的输出。

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

答案 3 :(得分:0)

感谢帮助人员,我终于设法解决了这个问题。不介意变量名称。我在那里太懒了。

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
Out[14]: 
['fem j / male',
 'diwan singh saggu',
 'rahul saggu',
 '',
 'zfk0281501',
 'as on 1.1.2008 23']

答案 4 :(得分:0)

您可以使用正则表达式:

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]

输出:

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

答案 5 :(得分:0)

尝试这样的事情:

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)

输出:

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