Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/282.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用Python删除包含字符或字母字符串的文本文件中的单词_Python_String_Nlp_Special Characters_Corpus - Fatal编程技术网

使用Python删除包含字符或字母字符串的文本文件中的单词

使用Python删除包含字符或字母字符串的文本文件中的单词,python,string,nlp,special-characters,corpus,Python,String,Nlp,Special Characters,Corpus,我有几行文本,希望删除其中包含特殊字符或固定给定字符串的任何单词(在python中) 例如: in_lines = ['this is go:od', 'that example is bad', 'amp is a word'] # remove any word with {'amp', ':'} out_lines = ['this is', 'that is bad', 'is

我有几行文本,希望删除其中包含特殊字符或固定给定字符串的任何单词(在python中)

例如:

in_lines = ['this is go:od', 
            'that example is bad', 
            'amp is a word']

# remove any word with {'amp', ':'}
out_lines = ['this is', 
             'that is bad', 
             'is a word']
我知道如何从给定的列表中删除单词,但无法删除带有特殊字符或字母较少的单词。请让我知道,我会补充更多信息

这是我用来删除所选单词的方法:

def remove_stop_words(lines):
   stop_words = ['am', 'is', 'are']
   results = []
   for text in lines:
        tmp = text.split(' ')
        for stop_word in stop_words:
            for x in range(0, len(tmp)):
               if tmp[x] == stop_word:
                  tmp[x] = ''
        results.append(" ".join(tmp))
   return results
out_lines = remove_stop_words(in_lines)

这与您的预期输出相匹配:

def remove_stop_words(lines):
  stop_words = ['am', ':']
  results = []
  for text in lines:
    tmp = text.split(' ')
    for x in range(0, len(tmp)):
      for st_w in stop_words:
        if st_w in tmp[x]:
          tmp[x] = ''
    results.append(" ".join(tmp))
  return results

这与您的预期输出相匹配:

def remove_stop_words(lines):
  stop_words = ['am', ':']
  results = []
  for text in lines:
    tmp = text.split(' ')
    for x in range(0, len(tmp)):
      for st_w in stop_words:
        if st_w in tmp[x]:
          tmp[x] = ''
    results.append(" ".join(tmp))
  return results
这句话听起来很奇怪

word for word in line.split() if not any([phrase in word for phrase in bad_list])
一次完成这里所有的艰苦工作。它为应用于单个单词的“坏”列表中的每个短语创建一个
True
/
False
值列表。
any
函数再次将该临时列表压缩为单个
True
/
False
值,如果该值为
False
,则可以安全地将该单词复制到基于行的输出列表中

例如,删除包含
a
的所有单词的结果如下所示:

remove_words(in_lines, ['a'])
>>> ['this is go:od', 'is', 'is word']
(也可以删除..行中的
行。但此时,可读性确实开始受到影响。)

这句话听起来很奇怪

word for word in line.split() if not any([phrase in word for phrase in bad_list])
一次完成这里所有的艰苦工作。它为应用于单个单词的“坏”列表中的每个短语创建一个
True
/
False
值列表。
any
函数再次将该临时列表压缩为单个
True
/
False
值,如果该值为
False
,则可以安全地将该单词复制到基于行的输出列表中

例如,删除包含
a
的所有单词的结果如下所示:

remove_words(in_lines, ['a'])
>>> ['this is go:od', 'is', 'is word']

(也可以删除..
行中的
行。此时,可读性确实开始受到影响。)

谢谢@usr2564301这是一个精确匹配。谢谢@usr2564301这是一个精确匹配。