我有下面的列表:someList = ['blablahihix', 'somethinghihi']
,我想返回一个列表,其中包含列表中两个元素之间的重复模式(在本例中为“hihi”)。你知道吗
下面是我要做的:
p, r = re.compile(r'(.+?)\1+'), []
for i in strList:
r.extend(p.findall(i) or [i])
当我print r
它给我['bla', 'hi', 'hi']
。我想要的只是['hihi']
。我不希望返回'blabla',因为我在列表的第二个元素中没有'blabla'。你知道吗
我错过了什么?你知道吗
使用^{} 操作获取匹配组的交集:
使用^{} or ^{} 获得两个匹配中出现的公共部分。你知道吗
您需要修改模式或使用^{} ,因为^{} 返回的方式取决于是否使用捕获组;如果模式中存在一个或多个组,则返回组列表,而不是整个匹配字符串的列表。你知道吗
更新
正如georg所建议的,您可以使用
set.intersection(*...)
;不需要使用reduce
。你知道吗相关问题 更多 >
编程相关推荐