[英]Check for Partial Match in 1 List With Partial Match Another List - Possible With List Comprehension?
有点Python /编程新手。
我编写了满足我需要的代码:
import re
syns = ['professionals|experts|specialists|pros', 'repayed|payed back', 'ridiculous|absurd|preposterous', 'salient|prominent|significant' ]
new_syns = ['repayed|payed back', 'ridiculous|crazy|stupid', 'salient|prominent|significant', 'winter-time|winter|winter season', 'professionals|pros']
def pipe1(syn):
# Find first word/phrase in list element up to and including the 1st pipe
r = r'.*?\|'
m = re.match(r, syn)
m = m.group()
return m
def find_non_match():
# Compare 'new_syns' with 'syns' and create new list from non-matches in 'new_syns'
p = '@#&' # Place holder created
joined = p.join(syns)
joined = p + joined # Adds place holder to beginning of string too
non_match = []
for syn in new_syns:
m = pipe1(syn)
m = p + m
if m not in joined:
non_match.append(syn)
return non_match
print find_non_match()
打印输出:
['winter-time|winter|winter season']
该代码检查new_syns
每个元素的单词/词组(包括第一个管道)并new_syns
匹配是否与syns
列表中的相同部分匹配项匹配。 该代码的目的是实际找到不匹配项,然后将它们附加到名为non_match
的新列表中。
但是,我想知道是否有可能实现相同的目的,但是使用列表理解的行数却要少得多。 我已经尝试过,但是我没有得到我想要的。 到目前为止,这是我想出的:
import re
syns = ['professionals|experts|specialists|pros', 'repayed|payed back', 'ridiculous|absurd|preposterous', 'salient|prominent|significant' ]
new_syns = ['repayed|payed back', 'ridiculous|crazy|stupid', 'salient|prominent|significant', 'winter-time|winter|winter season', 'professionals|pros']
def pipe1(syn):
# Find first word/phrase in list element up to and including the 1st pipe
r = r'.*?\|'
m = re.match(r, syn)
m = '@#&' + m.group() # Add unusual symbol combo to creatte match for beginning of element
return m
non_match = [i for i in new_syns if pipe1(i) not in '@#&'.join(syns)]
print non_match
打印输出:
['winter-time|winter|winter season', 'professionals|pros'] # I don't want 'professionals|pros' in the list
列表理解中的警告是,当使用@#&
加入syns
时,在现在加入的字符串的开头没有@#&
,而在上面的原始代码中我不使用列表理解,所以我添加了@#&
到连接字符串的开头。 结果是'professionals|pros'
漏网了。 但是我不知道如何在列表理解中做到这一点。
所以我的问题是“列表理解有可能吗?”。
我认为您想要类似的东西:
non_match = [i for i in new_syns if not any(any(w == s.split("|")[0]
for w in i.split("|"))
for s in syns)]
这不使用正则表达式,但是可以给出结果
non_match == ['winter-time|winter|winter season']
该列表包括new_syns
中的所有项目,其中'|'
都不存在( not any
) -分隔词语w
是在any
的第一个字( split("|")[0]
的每个同义词组s
从syns
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.