【问题标题】:Generate all possible lists by replacing elements with 0通过将元素替换为 0 来生成所有可能的列表
【发布时间】:2018-10-10 15:00:29
【问题描述】:

我想从一个列表中创建所有不同的列表是 0,1,2,3...所有元素都被另一个替换 例如,如果替换项为 0:

L=[1,2,3]
->[1,2,3],[0,2,3],[1,0,3],[1,2,0],[0,0,3],[0,2,0],[1,0,0],[0,0,0]

到目前为止,我已经尝试使用 Itertools 完成我想做的事情,但仅在 1 值被 0 替换的情况下 有谁知道怎么做?

【问题讨论】:

    标签: python python-3.x list combinations


    【解决方案1】:

    这里的每个人都太努力了。我们希望每个值都是原始值或 0——我们需要像 (1,0)、(2,0) 和 (3,0) 这样的对:

    >>> from itertools import product, repeat
    >>> L = [1, 2, 3]
    >>> zip(L, repeat(0))
    <zip object at 0x7f931ad1bf08>
    >>> list(zip(L, repeat(0)))
    [(1, 0), (2, 0), (3, 0)]
    

    然后我们可以将其传递给product:

    >>> list(product(*zip(L, repeat(0))))
    [(1, 2, 3), (1, 2, 0), (1, 0, 3), (1, 0, 0), (0, 2, 3), (0, 2, 0), (0, 0, 3), (0, 0, 0)]
    

    【讨论】:

      【解决方案2】:

      这是使用itertools 的一种方式。这种方法的好处是它很懒。

      生成器transformer 的每次__next__ 调用都会生成一个新列表。

      或者,如下所示,您可以通过在生成器函数上调用list 来输出所有组合。

      from itertools import combinations, chain
      
      A = [1, 2, 3]
      
      def transformer(x):
          idx = chain.from_iterable(combinations(range(len(x)), i) for i in range(len(x)+1))
          for indices in idx:
              y = x.copy()
              for j in indices:
                  y[j] = 0
              yield y
      
      res = list(transformer(A))
      
      print(res)
      
      [[1, 2, 3], [0, 2, 3], [1, 0, 3], [1, 2, 0], [0, 0, 3], [0, 2, 0], [1, 0, 0], [0, 0, 0]]
      

      【讨论】:

        【解决方案3】:

        您可以使用递归。首先,创建一个可以为输入的每个索引生成完整组合的函数:

        def full_combinations(d, current = []):
           if len(d) == len(current):
             yield current
           else:
             yield current
             for i in range(len(d)):
                if len(set(current+[i])) == len(current)+1:
                   yield from full_combinations(d, current+[i])
        
        combination_list = list(full_combinations([1, 2, 3]))
        new_results = [[0 if c in i else a for c, a in enumerate([1, 2, 3])] for i in combination_list]
        full = [a for i, a in enumerate(new_results) if a not in new_results[:i]]
        

        输出:

        [[1, 2, 3], [0, 2, 3], [0, 0, 3], [0, 0, 0], [0, 2, 0], [1, 0, 3], [1, 0, 0], [1, 2, 0]]
        

        【讨论】:

          【解决方案4】:

          它并不漂亮,但我相信你可以让这个想法发挥作用。

          这个想法是使用 itertools.combinations 来获取每个长度的所有索引组合,然后我们使用 itertools.chain() 展平这个列表。

          然后我们遍历这个列表列表,将这些索引设置为替换字符。

          import itertools
          l = [1,2,3]
          replace = 0
          
          indices = list(itertools.chain(*[list(itertools.combinations(list(range(len(l))),z+1)) for z in range(len(l))]))
          
          allcombs = [[l]]
          for i in indices:
              l2 = l[:]
              for j in i:
                  l2[j] = replace
              allcombs.append(l2)
          
          print(allcombs)
          

          [[[1, 2, 3]], [0, 2, 3], [1, 0, 3], [1, 2, 0], [0, 0, 3], [0, 2 , 0], [1, 0, 0], [0, 0, 0]]

          【讨论】:

          • [1, 2, 3] 丢失了,而且解释为 0,非常难以阅读。
          • 只需添加: allcombs = [[l]] 在初始化 allcombs 时包含这种情况。我们的想法是我们获得不同长度组合的所有索引的组合,然后使用 itertools.chain 展平列表 - 然后循环遍历该列表,将这些元素设置为 replace_char。
          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2016-06-26
          • 2023-03-29
          • 2021-01-13
          • 2017-08-13
          • 1970-01-01
          相关资源
          最近更新 更多