【发布时间】:2020-03-12 23:22:26
【问题描述】:
我想在 Python 中的另一个字符串列表中搜索一个字符串列表。如果找到匹配项,我想检索两个列表的匹配字符串。我也想获得部分匹配。清单 1 和清单 2 都很大,所以只是提供一个示例
例子:
list 1 = [ 'The tablets are filled into cylindrically shaped bottles made of white coloured\npolyethylene. The volumes of the bottles depend on the tablet strength and amount of\ntablets, ranging from 20 to 175 ml. The screw type cap is made of white coloured\npolypropylene and is equipped with a tamper proof ring.', 'PVC/PVDC blister pack', 'Blisters are made in a cold-forming process from an aluminium base web. Each tablet is\nfilled into a separate blister and a lidding foil of aluminium is welded on. The blisters\nare opened by pressing the tablets through the lidding foil.', '\n']
list 2 = [['Blister', 'Foil', 'Aluminium'], ['Blister', 'Base Web', 'PVC/PVDC'], ['Bottle', 'Cylindrically shaped Bottles', 'Polyethylene'], ['Bottle', 'Screw Type Cap', 'Polypropylene'], ['Bottle', 'Safety Ring', ''], ['Blister', 'Base Web', 'PVC'], ['Blister', 'Base Web', 'PVD/PVDC'], ['Bottle', 'Square Shaped Bottle', 'Polyethylene']]
如果列表 1 的同一字符串中不存在匹配项,则列表 1 中列表 2 的每个匹配项都应作为单独的阶段输出
样本预期输出:
Stage 1: 'The tablets are filled into cylindrically shaped bottles made of white coloured\npolyethylene. The volumes of the bottles depend on the tablet strength and amount of\ntablets, ranging from 20 to 175 ml. The screw type cap is made of white coloured\npolypropylene and is equipped with a tamper proof ring.', values : ['Bottle', 'Cylindrically shaped Bottles', 'Polyethylene']
Stage 2: 'Blisters are made in a cold-forming process from an aluminium base web. Each tablet is\nfilled into a separate blister and a lidding foil of aluminium is welded on. The blisters\nare opened by pressing the tablets through the lidding foil.', Values: ['Blister', 'Foil', 'Aluminium']
匹配条件:
1.) 我想匹配忽略列表 1 中的 \n。
2.) 我想匹配列表 1 中的列表 2,忽略复数/单数,这意味着应该匹配列表 1 中作为 'bottles' 出现的 'Bottle'。
我已经尝试过在 stackoverflow 上找到的这段代码,但并没有真正起作用。无法使用此代码获得多个匹配项,也无法从列表 1 中检索包含列表 2 值的整个字符串。这只是列出了列表 2 中的一些值:
from itertools import product
def generate_edges(iterable, control):
edges = []
control_set = set(control)
for e in iterable:
e_set = set(e)
common = e_set & control_set
to_pair = e_set - common
edges.extend(product(to_pair, common))
return edges
generate_edges(list2, list1)
最新变化:
counter = 1
for words in final_ref:
for sen in paragraphs:
all_exist = True
for w in words:
if w.lower() not in sen.lower():
all_exist = False
break
if all_exist:
#print(words[0])
colours = ["White","Yellow","Blue","Red","Green","Black","Brown","Silver","Purple","Navy blue","Gray","Orange","Maroon","pink","colourless","blue"]
if words[0] == 'Bottle':
for wd in colours:
if wd in sen.split():
wd = wd
#print(wd)
# wordsnew = wd + words[0]
# print(wordsnew)
# else:
# wordsnew = words
# print(wordsnew)
# break
#print(wd)
fr = "Stage " + str(counter) + ": " + "Package Description" + ": " + sen + " Values" + ": " + str(words) + "Colour" + ": " + str(wd) + "\n" + "\n" + "\n"
result.append(fr)
result = [i.replace('\n','') for i in result]
result = [i.replace('\t','') for i in result]
counter += 1
print(result)
【问题讨论】:
-
@jonrsharpe 添加代码供您参考
-
无法与此代码获得多个匹配项,也无法从列表 1 中检索包含列表 2 值的整个字符串。这只是列出了列表 2 中的一些值
-
我已经包含了相同的
-
好的!那么你做了什么尝试来适应你的特定需求呢?
标签: python regex python-3.x list string-matching