从具有缺失值的列表列表构造值数组

时间:2017-08-02 11:28:35

标签: python algorithm sorting

我想构建一个字符串列表,如下:

ideal = ["cheese", "bread", "fire", "stone", "pot", "straw"]

我得到一个包含值的String数组列表。

args = [
    ["cheese", "bread", "fire", "stone", "pot"],
    ["cheese", "bread", "pot", "straw"]
    ["cheese", "fire", "stone", "pot", "straw"]
    ["cheese", "bread", "fire", "stone", "pot", "straw"]
    ["bread", "fire", "stone", "pot", "straw"]
]

如果我在列表中创建所有值的列表,我可以创建上面的理想列表。但是,某些数组中缺少某些值。我还需要保留标签出现在列表中的顺序。

解决方案应该找到正确的顺序,例如如果一个数组为[cheese, fire, pot]且下一个数组为[cheese, bread, fire],则应推断出[cheese, bread, fire, pot]是正确的序列。

如何实现这一目标?

1 个答案:

答案 0 :(得分:3)

这是一个有趣的问题,但我通过以下一系列步骤解决了这个问题:

  1. 创建2D列表:
  2. args = [
        ["cheese", "bread", "fire", "stone", "pot"],
        ["cheese", "bread", "pot", "straw"],
        ["cheese", "fire", "stone", "pot", "straw"],
        ["cheese", "bread", "fire", "stone", "pot", "straw"],
        ["bread", "fire", "stone", "pot", "straw"]
    ]
    
    1. 生成一个字典,其中包含出现在其他字符串之前的字符串数组:
    2. before_dict = {}
      for arg in args:
          for i in range(len(arg)):
              if arg[i] not in before_dict:
                  before_dict[arg[i]] = []
              for j in arg[:i]:
                  if j not in before_dict[arg[i]]:
                      before_dict[arg[i]].append(j)
      

      这会导致:

      {
          'fire': ['cheese', 'bread'],
          'pot': ['cheese', 'bread', 'fire', 'stone'],
          'bread': ['cheese'],
          'stone': ['cheese', 'bread', 'fire'],
          'cheese': [],
          'straw': ['cheese', 'bread', 'pot', 'fire', 'stone']
      }
      
       3.我们可以看到'cheese'首先出现,因为它有一个空数组(即没有任何内容出现在它之前),我们可以将它添加到最终结果然后从其他数组中删除干酪,然后重复以获得最终结果回答:

      ideal = []
      repeating = True
      while repeating:
          repeating = False
          for k in before_dict:
              if before_dict[k] == []:
                  repeating = True
                  ideal.append(k)
                  del before_dict[k]
                  for k2 in before_dict:
                      if k in before_dict[k2]:
                          before_dict[k2].remove(k)
                  break
      
      这最终导致:

      ['cheese', 'bread', 'fire', 'stone', 'pot', 'straw']
      

      获得了相同的结果:

      args = [
          ["bread", "fire", "stone", "pot", "straw"],
          ["cheese", "bread", "fire", "stone", "pot"],
          ["cheese", "bread", "pot", "straw"],
          ["cheese", "fire", "stone", "pot", "straw"],
          ["cheese", "bread", "fire", "stone", "pot", "straw"]
      ]
      

      并提供结果:

      ['cheese', 'bread', 'fire', 'pot']
      

      输入:

      [
          ["cheese", "fire", "pot"],
          ["cheese", "bread", "fire"]
      ]