我在列表方面遇到了一些麻烦。所以,基本上,我有一个清单:
a=["Britney spears", "red dog", "\xa2xe3"]
我有另一个列表,看起来像:
b = ["cat","dog","red dog is stupid", "good stuff \xa2xe3", "awesome Britney spears"]
我想要检查a
中的元素是否属于b
中某个元素的一部分 - 如果是,请将其从b
的元素中删除。所以,我希望b
看起来像:
b = ["cat","dog","is stupid","good stuff","awesome"]
实现此目的的最pythonic(以2.7.x)方式是什么?
我假设我可以循环检查每个元素,但我不确定这是非常有效的 - 我有一个大小为50k的列表(b
)。
答案 0 :(得分: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']
答案 1 :(得分:2)
好吧,我不知道这是否算作 pythonic ,因为reduce
被放逐到python3中的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
提取到命名函数以便于阅读。
答案 2 :(得分:1)
嗯,最简单的是直接列表理解,只要a
很小,它甚至是一种非常有效的方法。
b = [i for i in b if i not in a]