在Python的字符串列表中搜索字符串列表

问题描述 投票:0回答:1

我想在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)
python regex python-3.x list string-matching
1个回答
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']
© www.soinside.com 2019 - 2024. All rights reserved.