【问题标题】:Merge two lists based on common parts of a string [closed]基于字符串的公共部分合并两个列表[关闭]
【发布时间】:2021-11-27 08:15:53
【问题描述】:

假设我有以下两个列表:

list1=["Equipment ONLY - Bees Technologies","Bees Technologies","Chris Metal SA - Central Office","NSA Aerospace tech"]

list2=["Bees Tech, Inc.","Chris Metal, SA","NSA Arerospace"]

如何合并这两个列表以获得以下结果:

final_list=["Equipment ONLY - Bees Technologies","Bees Technologies", "Bees Tech, Inc.", "Chris Metal SA - Central Office", "Chris Metal, SA","NSA Aerospace tech", "NSA Arerospace"]

【问题讨论】:

  • final_list = list1 + list2 ?
  • 如果列表 1 中的每个项目都对应于列表 2 中的一个项目,您可以使用 Levensthein Distance 或类似的方法来确定它对应的项目并进行相应的排序。
  • var list3 = list1.Union(list2).ToList();

标签: python list merge


【解决方案1】:

我认为对列表进行排序是您想要的:

list1=["Equipment ONLY - Bees Technologies","Bees Technologies","Chris Metal SA - Central Office","NSA Aerospace tech"]
list2=["Bees Tech, Inc.","Chris Metal, SA","NSA Arerospace"]

list1.extend(list2)
list1.sort()

print(list1) # Result : ['Bees Tech, Inc.', 'Bees Technologies', 'Chris Metal SA - Central Office', 'Chris Metal, SA', 'Equipment ONLY - Bees Technologies', 'NSA Aerospace tech', 'NSA Arerospace']

编辑

如果您只想合并它们,可以通过以下方式完成:

list1.extend(list2)

或通过:

list1 = list1 + list2

【讨论】:

  • 这些都不符合 OP 的预期输出。
【解决方案2】:

只需添加列表并使用排序

list1=["Equipment ONLY - Bees Technologies","Bees Technologies","Chris Metal SA - Central Office","NSA Aerospace tech"]

list2=["Bees Tech, Inc.","Chris Metal, SA","NSA Arerospace"]

final_list = list1+list2

print(sorted(final_list))

会给

['Bees Tech, Inc.', 'Bees Technologies', 'Chris Metal SA - Central Office', 'Chris Metal, SA', 'Equipment ONLY - Bees Technologies', 'NSA Aerospace tech', 'NSA Arerospace']

【讨论】:

    【解决方案3】:

    试试这个:

    final_list = sorted(list1 + list2)
    

    【讨论】:

      【解决方案4】:

      不完全清楚您在寻找什么。如果您想将list1list2 中的“匹配”项目排序在一起,您可以尝试使用difflib.SequenceMatcher 找到最佳匹配项。假设list2 中的(唯一)元素是您想要将元素从list1 排序为:

      list1=["Equipment ONLY - Bees Technologies","Bees Technologies","Chris Metal SA - Central Office","NSA Aerospace tech"]
      list2=["Bees Tech, Inc.","Chris Metal, SA","NSA Arerospace"]
      
      import difflib
      groups = {a: [] for a in list2}
      for b in list1:
          a = max(list2, key=lambda a: difflib.SequenceMatcher(a=a, b=b).ratio())
          groups[a].append(b)
      # {'Bees Tech, Inc.': ['Equipment ONLY - Bees Technologies', 'Bees Technologies'],
      #  'Chris Metal, SA': ['Chris Metal SA - Central Office'],
      #  'NSA Arerospace': ['NSA Aerospace tech']}
      
      res = [x for a in groups for x in (*groups[a], a)]
      # ['Equipment ONLY - Bees Technologies', 'Bees Technologies', 'Bees Tech, Inc.', 'Chris Metal SA - Central Office', 'Chris Metal, SA', 'NSA Aerospace tech', 'NSA Arerospace']
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-08-27
        • 2012-12-05
        相关资源
        最近更新 更多