我是Python的新手,无法找到删除无用文本的方法。主要目的是保留我想要的字,并删除所有其余的。在这个阶段,我可以检查我的 in_data 并找到我想要的单词。如果 sentence.find(wordToCheck)为正,则保留它。 in_data 是每行的句子,但当前输出是每行的一个单词。我想要的仍然是格式,在每一行中找到单词并删除其余部分。
import Orange
import orange
word = ['roaming','overseas','samsung']
out_data = []
for i in range(len(in_data)):
for j in range(len(word)):
sentence = str(in_data[i][0])
wordToCheck = word[j]
if(sentence.find(wordToCheck) >= 0):
print wordToCheck
输出
roaming
overseas
roaming
overseas
roaming
overseas
samsung
samsung
in_data 就像
一样contacted vodafone about going overseas and asked about roaming charges. The customer support officer says there isn't a charge but while checking my usage overseas.
我希望看到输出就像
overseas roaming overseas
答案 0 :(得分:3)
您可以使用正则表达式:
>>> import re
>>> word = ['roaming','overseas','samsung']
>>> s = "Contacted vodafone about going overseas and asked about roaming charges. The customer support officer says there isn't a charge but while checking my usage overseas."
>>> pattern = r'|'.join(map(re.escape, word))
>>> re.findall(pattern, s)
['overseas', 'roaming', 'overseas']
>>> ' '.join(_)
'overseas roaming overseas'
非正则表达式方法是将str.join
与str.strip
和生成器表达式一起使用。需要使用strip()调用来消除'.'
,','
等标点符号。
>>> from string import punctuation
>>> ' '.join(y for y in (x.strip(punctuation) for x in s.split()) if y in word)
'overseas roaming overseas'
答案 1 :(得分:2)
你可以做得更简单,就像这样:
for w in in_data.split():
if w in word:
print w
这里我们首先用空格分割in_data
,它返回一个单词列表。然后,我们遍历in数据中的每个单词,并检查单词是否等于您正在寻找的单词之一。如果是,那么我们打印它。
而且,为了更快地查找,请将word
- 列表设为一个集合。快得多。
此外,如果你想处理标点和符号,你需要使用正则表达式或检查字符串中的所有字符是否都是一个字母。所以,要获得你想要的输出:
import string
in_words = ('roaming','overseas','samsung')
out_words = []
for w in in_data.split():
w = "".join([c for c in w if c in string.letters])
if w in in_words:
out_words.append(w)
" ".join(out_words)
答案 2 :(得分:2)
这是一种更简单的方法:
>>> import re
>>> i
"Contacted vodafone about going overseas and asked about roaming charges. The customer support officer says there isn't a charge but while checking my usage overseas."
>>> words
['roaming', 'overseas', 'samsung']
>>> [w for w in re.findall(r"[\w']+", i) if w in words]
['overseas', 'roaming', 'overseas']
答案 3 :(得分:0)
使用拆分的答案将落在标点符号上。你需要用正则表达式来分解单词。
import re
in_data = "contacted vodafone about going overseas and asked about roaming charges. The customer support officer says there isn't a charge but while checking my usage overseas."
word = ['roaming','overseas','samsung']
out_data = []
word_re = re.compile(r'[^\w\']+')
for check_word in word_re.split(in_data):
if check_word in word:
print check_word