【问题标题】:Search a list of list of strings inside a list of strings in Python在 Python 中的字符串列表中搜索字符串列表
【发布时间】: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


【解决方案1】:

通常您需要付出努力才能获得回复,但这次这将帮助您:

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']

【讨论】:

  • 谢谢。您能否解释一下您的代码以及如何将 list2 中的 'bottle' 与 list1 中的 'bottles' 匹配
  • 它在list1小写的每个句子中查找list2的每个小写项目中的每个单词,如果其中一个单词不是 存在,它停止寻找,并开始下一个组合。它查找句子中的字符序列,因此“bottles”中存在“bottle”。不应该算吗?
  • 它运行良好。哪一行“查找句子中的字符序列”?只是一个补充,我想将像“PVC/PVDC”这样的条目视为一个实体。现在它将作为第 1 阶段的 PVC/PVDC、第 2 阶段的 PVC 和第 3 阶段的 PVDC。这可能吗?
  • @techsmart 这就是循环的作用。并且“PVC/PVDC”被视为单个实体,但在list2 中,您也有一个“PVC”实体和一个“PVDC”实体,所以这些匹配项也会出现.
  • 我需要你的另一个帮助。我想检查 words[0] 的值,如果其中存在“Bottle”,我想在该字符串中搜索颜色,否则我只想返回没有任何颜色值的瓶子。我在“最新更改”下添加的代码为我不想要的每个“阶段”提取颜色。仅当 word[0] 颜色中存在 'Bottle' 时,才应搜索。关于如何解决这个问题的任何想法
猜你喜欢
  • 1970-01-01
  • 2011-08-19
  • 1970-01-01
  • 2016-04-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-08-30
相关资源
最近更新 更多