求和任意数量数组的所有可能组合,并应用限制和返回索引 [英] summing all possible combinations of an arbitrary number of arrays and applying limits and returning indices

查看:51
本文介绍了求和任意数量数组的所有可能组合,并应用限制和返回索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是对的修改>这个问题,除了元素本身之外,我还想返回数组元素的索引.我已经成功地修改了arraysums()arraysums_recursive(),但是我却在为arraysums_recursive_anyvals()苦苦挣扎.详细信息如下:

This is a modification of this question in which I would like to return the indices of the arrays elements in addition to the elements themselves. I've successfully modified arraysums(), arraysums_recursive(), but I'm struggling with arraysums_recursive_anyvals(). Here are the details:

我修改了arraysums():

def arraysums(arrays,lower,upper):
    products = itertools.product(*arrays)
    result = list()

    indices = itertools.product(*[np.arange(len(arr)) for arr in arrays])
    index = list()

    for n,k in zip(products,indices):
        s = sum(n)
        if lower <= s <= upper:
            result.append(n)
            index.append(k)                
    return result,index

现在它返回元素和元素的索引:

It now returns the elements and the indices of the elements:

N = 8
a = np.arange(N)
b = np.arange(N)-N/2    
arraysums((a,b),lower=5,upper=6)


([(2, 3),
  (3, 2),
  (3, 3),
  (4, 1),
  (4, 2),
  (5, 0),
  (5, 1),
  (6, -1),
  (6, 0),
  (7, -2),
  (7, -1)],
 [(2, 7),
  (3, 6),
  (3, 7),
  (4, 5),
  (4, 6),
  (5, 4),
  (5, 5),
  (6, 3),
  (6, 4),
  (7, 2),
  (7, 3)])

我还修改了@unutbu的递归解决方案,该解决方案也返回与arraysums()相同的结果:

I also modified @unutbu's recursive solution which also returns the same result as arraysums():

def arraysums_recursive(arrays, lower, upper):
    if len(arrays) <= 1:
        result = [(item,) for item in arrays[0] if lower <= item <= upper]
        index = []   # this needs to be fixed
    else:
        result = []
        index = []
        for item in arrays[0]:
            subarrays = [[item2 for item2 in arr if item2 <= upper-item] 
                      for arr in arrays[1:]]
            result.extend(
                [(item,)+tup for tup in arraysums(
                    subarrays, lower-item, upper-item)[0]])
            index.extend(
                [(item,)+tup for tup in arraysums(
                    subarrays, lower-item, upper-item)[1]])

    return result,index

最后,我修改了arraysums_recursive_anyvals(),但是我似乎无法理解为什么它不返回索引:

Finally, I modified arraysums_recursive_anyvals(), but I can't seem to understand why it does not return the indices:

def arraysums_recursive_anyvals(arrays, lower, upper):
    if len(arrays) <= 1:
        result = [(item,) for item in arrays[0] if lower <= item <= upper]
        index = []   # this needs to be fixed
    else:
        minval = min(item for arr in arrays for item in arr)
        # Subtract minval from arrays to guarantee all the values are positive
        arrays = [[item-minval for item in arr] for arr in arrays]
        # Adjust the lower and upper bounds accordingly
        lower -= minval*len(arrays)
        upper -= minval*len(arrays)

        result = []
        index = []
        for item in arrays[0]:
            subarrays = [[item2 for item2 in arr if item2 <= upper-item] 
                      for arr in arrays[1:]]
            if min(len(arr) for arr in subarrays) == 0:
                continue
            result.extend(
                [(item,)+tup for tup in arraysums_recursive(
                    subarrays, lower-item, upper-item)[0]])
            index.extend(
                [(item,)+tup for tup in arraysums_recursive(
                    subarrays, lower-item, upper-item)[1]])

        # Readjust the result by adding back minval
        result = [tuple([item+minval for item in tup]) for tup in result]
    return result,index

结果:

arraysums_recursive_anyvals((a,b),lower=5,upper=6)

([(2, 3),
  (3, 2),
  (3, 3),
  (4, 1),
  (4, 2),
  (5, 0),
  (5, 1),
  (6, -1),
  (6, 0),
  (7, -2),
  (7, -1)],
 [])

推荐答案

arraysums_recursive的主要功能是它抛出了可能对结果无用的值:

A key feature of arraysums_recursive is that it throws out values which can not possibly contribute to the result:

subarrays = [[item2 for item2 in arr if item2 <= upper-item] 
              for arr in arrays[1:]]

尽管扔东西使索引的记录变得复杂,但这并不难. 首先,在arraysums_recursive中,展开arrays以包括索引以及项目值:

While throwing things out complicates the recording of indices, it's not too hard. First, in arraysums_recursive expand arrays to include the index as well as the item value:

def arraysums_recursive(arrays, lower, upper):
    arrays = [[(i, item) for i, item in enumerate(arr)] for arr in arrays]
    ...
    index, result = zip(*arraysums_recursive_all_positive(arrays, lower, upper))
    return result, index

现在重写arraysums_recursive_all_positive来处理arrays,它由(index, item) tuples 的列表组成.

Now rewrite arraysums_recursive_all_positive to handle arrays which consist of a list of lists of (index, item) tuples.

def arraysums_recursive(arrays, lower, upper):
    arrays = [[(i, item) for i, item in enumerate(arr)] for arr in arrays]
    minval = min(item for arr in arrays for i, item in arr)
    # Subtract minval from arrays to guarantee all the values are positive
    arrays = [[(i, item-minval) for i, item in arr] for arr in arrays]
    # Adjust the lower and upper bounds accordingly
    lower -= minval*len(arrays)
    upper -= minval*len(arrays)
    index, result = zip(*arraysums_recursive_all_positive(arrays, lower, upper))
    # Readjust the result by adding back minval
    result = [tuple([item+minval for item in tup]) for tup in result]
    return result, index

def arraysums_recursive_all_positive(arrays, lower, upper):
    # Assumes all values in arrays are positive
    if len(arrays) <= 1:
        result = [((i,), (item,)) for i, item in arrays[0] if lower <= item <= upper]
    else:
        result = []
        for i, item in arrays[0]:
            subarrays = [[(i, item2) for i, item2 in arr if item2 <= upper-item] 
                         for arr in arrays[1:]]
            if min(len(arr) for arr in subarrays) == 0:
                continue
            result.extend(
                [((i,)+i_tup, (item,)+item_tup) for i_tup, item_tup in 
                 arraysums_recursive_all_positive(subarrays, lower-item, upper-item)])
    return result


import numpy as np
N = 8
a = np.arange(N)
b = np.arange(N)-N/2    
result, index = arraysums_recursive((a,b),lower=5,upper=6)

产量result:

[(2.0, 3.0),
 (3.0, 2.0),
 (3.0, 3.0),
 (4.0, 1.0),
 (4.0, 2.0),
 (5.0, 0.0),
 (5.0, 1.0),
 (6.0, -1.0),
 (6.0, 0.0),
 (7.0, -2.0),
 (7.0, -1.0)]

index:

((2, 7),
 (3, 6),
 (3, 7),
 (4, 5),
 (4, 6),
 (5, 4),
 (5, 5),
 (6, 3),
 (6, 4),
 (7, 2),
 (7, 3))

这篇关于求和任意数量数组的所有可能组合,并应用限制和返回索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆