Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/351.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/regex/17.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_Regex - Fatal编程技术网

在python中从列表中提取特定字符串/单词

在python中从列表中提取特定字符串/单词,python,regex,Python,Regex,我有一份以下列方式的清单: ['0.089*"apple" + 0.089*"mango" + 0.089*"orange"','0.205*"apple" +0.167*"mango" + 0.167*"apple"','0.056*"orange" + 0.056*"apple" + 0.056*"orange"'] 我想通过以下方式从上面的列表生成一个输出 ['apple mango orange','apple mango apple','orange apple orange']

我有一份以下列方式的清单:

['0.089*"apple" + 0.089*"mango" + 0.089*"orange"','0.205*"apple" +0.167*"mango" + 0.167*"apple"','0.056*"orange" + 0.056*"apple" + 0.056*"orange"']
我想通过以下方式从上面的列表生成一个输出

['apple mango orange','apple mango apple','orange apple orange']
我尝试使用regex,但没有得到输出

import re 
lst = ['0.089*"apple" + 0.089*"mango" + 0.089*"orange"','0.205*"apple" +0.167*"mango" + 0.167*"apple"','0.056*"orange" + 0.056*"apple" + 0.056*"orange"']

result = [' '.join(re.findall(r'[a-zA-Z]+', s)) for s in lst]
输出(
结果
):


使用
re.sub

Ex:

import re
l = ['0.089*"apple" + 0.089*"mango" + 0.089*"orange"','0.205*"apple" +0.167*"mango" + 0.167*"apple"','0.056*"orange" + 0.056*"apple" + 0.056*"orange"']
for i in l:
    print( re.sub(r"[^A-Za-z\s]", "", i) )

res = [re.sub(r"[^A-Za-z\s]", "", i) for i in l] # ['apple  mango  orange', 'apple mango  apple', 'orange  apple  orange']
apple  mango  orange
apple mango  apple
orange  apple  orange
输出:

import re
l = ['0.089*"apple" + 0.089*"mango" + 0.089*"orange"','0.205*"apple" +0.167*"mango" + 0.167*"apple"','0.056*"orange" + 0.056*"apple" + 0.056*"orange"']
for i in l:
    print( re.sub(r"[^A-Za-z\s]", "", i) )

res = [re.sub(r"[^A-Za-z\s]", "", i) for i in l] # ['apple  mango  orange', 'apple mango  apple', 'orange  apple  orange']
apple  mango  orange
apple mango  apple
orange  apple  orange

我曾尝试使用re.findall提取单词,但没有得到正确的输出。谢谢Rakesh