2

我在使用列表时遇到了一些问题。所以,基本上,我有一个清单:

a=["Britney spears", "red dog", "\xa2xe3"]

我还有另一个列表,看起来像:

b = ["cat","dog","red dog is stupid", "good stuff \xa2xe3", "awesome Britney spears"]

我想做的是检查元素 ina是否是某些元素的一部分b- 如果是,则将它们从b的元素中删除。所以,我想b看起来像:

b = ["cat","dog","is stupid","good stuff","awesome"]

实现这一目标的最 Pythonic(在 2.7.x 中)的方法是什么?

我假设我可以循环检查每个元素,但我不确定这是否非常有效 - 我有一个b大小约为 50k 的列表 ( )。

4

3 回答 3

4

我想我会在这里使用正则表达式:

import re

a=["Britney spears", "red dog", "\xa2xe3"]

regex = re.compile('|'.join(re.escape(x) for x in a))

b=["cat","dog","red dog is stupid", "good stuff \xa2xe3", "awesome Britney spears"]

b = [regex.sub("",x) for x in b ]
print (b)  #['cat', 'dog', ' is stupid', 'good stuff ', 'awesome ']

这样,正则表达式引擎可以优化替代列表的测试。

这里有一堆替代方法来展示不同的正则表达式的行为方式。

import re

a = ["Britney spears", "red dog", "\xa2xe3"]
b = ["cat","dog",
     "red dog is stupid", 
     "good stuff \xa2xe3", 
     "awesome Britney spears",
     "transferred dogcatcher"]

#This version leaves whitespace and will match between words.
regex = re.compile('|'.join(re.escape(x) for x in a))
c = [regex.sub("",x) for x in b ]
print (c) #['cat', 'dog', ' is stupid', 'good stuff ', 'awesome ', 'transfercatcher']

#This version strips whitespace from either end
# of the returned string
regex = re.compile('|'.join(r'\s*{}\s*'.format(re.escape(x)) for x in a))
c = [regex.sub("",x) for x in b ]
print (c) #['cat', 'dog', 'is stupid', 'good stuff', 'awesome', 'transfercatcher']

#This version will only match at word boundaries,
# but you lose the match with \xa2xe3 since it isn't a word
regex = re.compile('|'.join(r'\s*\b{}\b\s*'.format(re.escape(x)) for x in a))
c = [regex.sub("",x) for x in b ]
print (c) #['cat', 'dog', 'is stupid', 'good stuff \xa2xe3', 'awesome', 'transferred dogcatcher']


#This version finally seems to get it right.  It matches whitespace (or the start
# of the string) and then the "word" and then more whitespace (or the end of the 
# string).  It then replaces that match with nothing -- i.e. it removes the match 
# from the string.
regex = re.compile('|'.join(r'(?:\s+|^)'+re.escape(x)+r'(?:\s+|$)' for x in a))
c = [regex.sub("",x) for x in b ]
print (c) #['cat', 'dog', 'is stupid', 'good stuff', 'awesome', 'transferred dogcatcher']
于 2013-02-01T12:51:21.050 回答
2

好吧,我不知道这是否算作pythonic了,因为在python3中reduce被流放functools,有人不得不在桌子上放一个单行:

a = ["Britney spears", "red dog", "\xa2xe3"]
b = ["cat","dog","red dog is stupid", "good stuff \xa2xe3", "awesome Britney spears"]

b = [reduce(lambda acc, n: acc.replace(n, ''), a, x).strip() for x in b]

甚至更快

[reduce(lambda acc, n: acc.replace(n, '') if n in acc else acc, a, x).strip() for x in b]

但是随着可读性的降低,我认为它越来越不那么Pythonic了。

这是处理transferred dogcatcher此案的人。我借用了 mgilson 的正则表达式,但我认为这没关系,因为它非常简单:-):

def reducer(acc, n):
    if n in acc:
        return re.sub('(?:\s+|^)' + re.escape(n) + '(?:\s+|$)', '', acc)
    return acc

b = [reduce(reducer, a, x).strip() for x in b]

为了便于阅读,我将其提取lambda到命名函数中。

于 2013-02-01T13:03:19.073 回答
1

嗯,最简单的就是一个直接列表推导式,只要a它很小,它甚至是一个非常有效的方法。

b = [i for i in b if i not in a]
于 2013-02-01T12:48:51.173 回答