【问题标题】:How to remove a particular substring from a list of strings?如何从字符串列表中删除特定的子字符串?
【发布时间】:2018-06-29 10:36:54
【问题描述】:

我有两个列表,

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

我需要删除y1 中的所有字符串,它位于word 中。 我需要输出为

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

我怎样才能做到这一点?

【问题讨论】:

  • 请展示你的努力?至少在询问 SO 之前尝试进行研究。
  • 那么,你尝试了什么?
  • 我尝试使用 split() 来拆分字符串,然后用 word 搜索这些字符串。如果它实现了,我使用 replace() 从 y1 中删除。但它没有用。跨度>

标签: python string list


【解决方案1】:

代码:

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

【讨论】:

    【解决方案2】:

    试试这个程序!

    它将完美运行! 另外我附上了程序的输出。

    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)
    

    【讨论】:

      【解决方案3】:

      早上好,

      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

      【讨论】:

      【解决方案4】:

      感谢大家的帮助,我终于解决了这个问题。不要介意变量名。我在那里太懒了。

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

      【讨论】:

        【解决方案5】:

        你可以使用正则表达式:

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

        【讨论】:

          【解决方案6】:

          试试这样的:

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

          【讨论】:

            猜你喜欢
            • 2015-10-02
            • 2021-06-24
            • 2015-04-23
            • 2020-05-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多