[英]Check for Partial Match in 1 List With Partial Match Another List - Possible With List Comprehension?
somewhat of a python/programming newbie here. 有点Python /编程新手。
I have written code that does what I need it to: 我编写了满足我需要的代码:
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()
Printed output: 打印输出:
['winter-time|winter|winter season']
The code checks if the word/phrase up to and including the first pipe for each element in new_syns
is a match for the same partial match in syns
list. 该代码检查new_syns
每个元素的单词/词组(包括第一个管道)并new_syns
匹配是否与syns
列表中的相同部分匹配项匹配。 The purpose of the code is to actually find the non-matches and then append them to a new list called non_match
, which it does. 该代码的目的是实际找到不匹配项,然后将它们附加到名为non_match
的新列表中。
However, I am wonder if it is possible to achieve the same purpose, but in much fewer lines using list comprehension. 但是,我想知道是否有可能实现相同的目的,但是使用列表理解的行数却要少得多。 I have tried, but I am not getting what I want exactly. 我已经尝试过,但是我没有得到我想要的。 This is what I have come up with so far: 到目前为止,这是我想出的:
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
Printed output: 打印输出:
['winter-time|winter|winter season', 'professionals|pros'] # I don't want 'professionals|pros' in the list
The caveat in the list comprehension is that when joining syns
with @#&
, I don't have the @#&
at the beginning of the now joined string, whereas in my original code above that does not use the list comprehension I add @#&
to the beginning of the joined string. 列表理解中的警告是,当使用@#&
加入syns
时,在现在加入的字符串的开头没有@#&
,而在上面的原始代码中我不使用列表理解,所以我添加了@#&
到连接字符串的开头。 The result is that 'professionals|pros'
have slipped through the net. 结果是'professionals|pros'
漏网了。 But I don't know how to pull that off within the list comprehension. 但是我不知道如何在列表理解中做到这一点。
So my question is "Is this possible with the list comprehension?". 所以我的问题是“列表理解有可能吗?”。
I think you want something like: 我认为您想要类似的东西:
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)]
This doesn't use regular expressions, but does give the result 这不使用正则表达式,但是可以给出结果
non_match == ['winter-time|winter|winter season']
The list includes any items from new_syns
where none ( not any
) of the '|'
该列表包括new_syns
中的所有项目,其中'|'
都不存在( not any
) -separated words w
are in any
of the first word ( split("|")[0]
) of each synonym group s
from syns
-分隔词语w
是在any
的第一个字( split("|")[0]
的每个同义词组s
从syns
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.