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

时间:2018-08-01 11:33:15

标签: 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']

我尝试使用正则表达式,但没有得到输出。

2 个答案:

答案 0 :(得分:2)

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]

输出(result):

['apple mango orange', 'apple mango apple', 'orange apple orange']

答案 1 :(得分:0)

使用re.sub

例如:

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