我想在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中作为“瓶”出现的“瓶”。
我尝试了这段代码,这些代码是我在stackoverflow上找到的,但实际上没有用。此代码无法获得多个匹配项,也无法从包含列表2值的列表1中检索整个字符串。这只是列出了列表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)
答案 0 :(得分:0)
通常您需要付出努力才能获得答复,但这一次可以为您提供帮助:
counter = 1
for words in list2:
for sen in list1:
all_exist = True
for w in words:
if w.lower() not in sen.lower():
all_exist = False
break
if all_exist:
print("Stage " + str(counter) + ": " + sen + " Values" + str(words) + "\n")
counter += 1
输出:
Stage 1: Blisters are made in a thermo-forming process from a PVC/PVDC base web. Each tablet
is filled into a separate blister and a lidding foil of aluminium is welded on. The blisters
are opened by pressing the tablets through the lidding foil. PVDC foil is in contact with
the tablets. Values['Blister', 'Foil', 'Aluminium']
Stage 2: Blisters are made in a cold-forming process from an aluminium base web. Each tablet is
filled into a separate blister and a lidding foil of aluminium is welded on. The blisters
are opened by pressing the tablets through the lidding foil. Values['Blister', 'Foil', 'Aluminium']
Stage 3: Blisters are made in a thermo-forming process from a PVC/PVDC base web. Each tablet
is filled into a separate blister and a lidding foil of aluminium is welded on. The blisters
are opened by pressing the tablets through the lidding foil. PVDC foil is in contact with
the tablets. Values['Blister', 'Base Web', 'PVC/PVDC']
Stage 4: The tablets are filled into cylindrically shaped bottles made of white coloured
polyethylene. The volumes of the bottles depend on the tablet strength and amount of
tablets, ranging from 20 to 175 ml. The screw type cap is made of white coloured
polypropylene and is equipped with a tamper proof ring. Values['Bottle', 'Cylindrically shaped Bottles', 'Polyethylene']
Stage 5: The tablets are filled into cylindrically shaped bottles made of white coloured
polyethylene. The volumes of the bottles depend on the tablet strength and amount of
tablets, ranging from 20 to 175 ml. The screw type cap is made of white coloured
polypropylene and is equipped with a tamper proof ring. Values['Bottle', 'Screw Type Cap', 'Polypropylene']
Stage 6: Blisters are made in a thermo-forming process from a PVC/PVDC base web. Each tablet
is filled into a separate blister and a lidding foil of aluminium is welded on. The blisters
are opened by pressing the tablets through the lidding foil. PVDC foil is in contact with
the tablets. Values['Blister', 'Base Web', 'PVC']