NULL 数组上的笛卡尔坐标

发布于 2025-01-05 22:14:07 字数 1086 浏览 0 评论 0 原文

我需要 6 个数组的笛卡尔积 - 问题是任何时候最多 5 个数组可能为空。当所有数组都填充时它工作得很好,但当任何数组为空时它会爆炸

我的数组是这样的

MatrixArray_1[0] = 1
MatrixArray_1[1] = 2

MatrixArray_2[0] = null
MatrixArray_2[1] = null

MatrixArray_n[0] = 2
MatrixArray_n[1] = 2

等等。

我当前正在使用此代码...源自 http:// /blogs.msdn.com/b/ericlippert/archive/2010/06/28/computing-a-cartesian-product-with-linq.aspx

var product1 =  from first in MatrixArray_1
                from second in MatrixArray_2
                from third in MatrixArray_3
                from fourth in MatrixArray_4
                from fifth in MatrixArray_5
                from sixth in MatrixArray_6
                select new[] { first, second, third, fourth, fifth, sixth };
            string[][] myCombos_linq = product1.ToArray();

我已尝试将 MatrixArray_n 放置在第一个 != null 位置,但会在第一个空数组处停止,并且不会读取所有剩余数组,因此即使填充了 array1 和数组 3,我的返回数组始终为 0 行。

此时,代码/逻辑的任何更改都值得赞赏! TIA

I need a cartesian product of 6 arrays - the catch is that at any time upto 5 arrays could be null. It works great when all arrays are populated but bombs when any of the arrays are null

my arrays are something like this

MatrixArray_1[0] = 1
MatrixArray_1[1] = 2

MatrixArray_2[0] = null
MatrixArray_2[1] = null

MatrixArray_n[0] = 2
MatrixArray_n[1] = 2

etc.

I am using this code currently...derived from
http://blogs.msdn.com/b/ericlippert/archive/2010/06/28/computing-a-cartesian-product-with-linq.aspx

var product1 =  from first in MatrixArray_1
                from second in MatrixArray_2
                from third in MatrixArray_3
                from fourth in MatrixArray_4
                from fifth in MatrixArray_5
                from sixth in MatrixArray_6
                select new[] { first, second, third, fourth, fifth, sixth };
            string[][] myCombos_linq = product1.ToArray();

I have tried putting MatrixArray_n where first != null but that stops at the first null array and does not read thru all the remaining arrays so my return array is always 0 rows even though array1 and array 3 are populated.

Change of code/logic anything is appreciated at this point in time!
TIA

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

似最初 2025-01-12 22:14:07

由于 Eric 的方法是使用 IEnumerable>,因此您必须执行以下操作:

Eric 的代码:

static IEnumerable<IEnumerable<T>> CartesianProduct<T>(this IEnumerable<IEnumerable<T>> sequences) 
{ 
  IEnumerable<IEnumerable<T>> emptyProduct = new[] { Enumerable.Empty<T>() }; 
  return sequences.Aggregate( 
    emptyProduct, 
    (accumulator, sequence) => 
      from accseq in accumulator 
      from item in sequence 
      select accseq.Concat(new[] {item})); 
}

调用站点:

var sequences = new int?[][] { MatrixArray_1, MatrixArray_2, ..., MatrixArray_6 };
var cartesianSequence = sequences.CartesianProduct();

更改调用站点:

var cartesianSequence = sequences.Where(a => a.Any(e => e != null)).CartesianProduct();

Where 调用将排除所有序列元素为空。

要排除空数组以及仅包含空值的数组:

var cartesianSequence = sequences.Where(a => a != null && a.Any(e => e != null)).CartesianProduct();

或者,使用查询理解:

var filteredSequences = 
    from sequence in sequences
    where sequence != null && sequence.Any(e => e != null)
    select sequence
var cartesianSequence = filteredSequences.CartesianProduct();

编辑

另一种可能性是您想要排除每个序列的空元素,即使某些元素是非空的:

var filteredSequences = 
    from sequence in sequences
    where sequence != null && sequence.Any(e => e != null)
    select (from v in sequence where v.HasValue select s)
var cartesianSequence = filteredSequences.CartesianProduct();

或者

var cartesianSequence = sequences
    .Where(s => s != null && s.Any(e => e != null))
    .Select(s => s.Where(v => v != null))
    .CartesianProduct();

但是很难确切地知道应该提供什么建议,因为我们不知道您正在对结果做什么。

Since Eric's approach is to use an IEnumerable<IEnumerable<T>>, you must be doing something like this:

Eric's code:

static IEnumerable<IEnumerable<T>> CartesianProduct<T>(this IEnumerable<IEnumerable<T>> sequences) 
{ 
  IEnumerable<IEnumerable<T>> emptyProduct = new[] { Enumerable.Empty<T>() }; 
  return sequences.Aggregate( 
    emptyProduct, 
    (accumulator, sequence) => 
      from accseq in accumulator 
      from item in sequence 
      select accseq.Concat(new[] {item})); 
}

Call site:

var sequences = new int?[][] { MatrixArray_1, MatrixArray_2, ..., MatrixArray_6 };
var cartesianSequence = sequences.CartesianProduct();

Change the call site:

var cartesianSequence = sequences.Where(a => a.Any(e => e != null)).CartesianProduct();

The Where call will exclude sequences where all elements are null.

To exclude null arrays as well as arrays containing only null values:

var cartesianSequence = sequences.Where(a => a != null && a.Any(e => e != null)).CartesianProduct();

Or, with query comprehension:

var filteredSequences = 
    from sequence in sequences
    where sequence != null && sequence.Any(e => e != null)
    select sequence
var cartesianSequence = filteredSequences.CartesianProduct();

EDIT

Another possibility is that you want to exclude null elements of each sequence, even if some elements are non-null:

var filteredSequences = 
    from sequence in sequences
    where sequence != null && sequence.Any(e => e != null)
    select (from v in sequence where v.HasValue select s)
var cartesianSequence = filteredSequences.CartesianProduct();

OR

var cartesianSequence = sequences
    .Where(s => s != null && s.Any(e => e != null))
    .Select(s => s.Where(v => v != null))
    .CartesianProduct();

But it's hard to know exactly what to advise since we don't know what you're doing with the result.

知足的幸福 2025-01-12 22:14:07

我是否正确理解您想要排除 firstsecond 等中的任何一个(如果它们为 null)?这很简单:

只需添加

select new [] { first, second, third, fourth, fifth, sixth }.Where(x => x != null)

到您的查询中即可。

或者如果 firstsecond 等中的任何一个为 null,您是否想要排除整个六元组?这也很容易。只需添加

where new [] { first, second, third, fourth, fifth, sixth }.All(x => x != null)

到您的查询中即可。您甚至可以使用 let 这样就不会创建数组两次。

Am I understanding correctly that you want to exclude any of first, second, etc. if they are null? That's easy:

Just add

select new [] { first, second, third, fourth, fifth, sixth }.Where(x => x != null)

to your query.

Or is that you want to exclude the entire sextuple if any of first, second, etc. are null? That's easy too. Just add

where new [] { first, second, third, fourth, fifth, sixth }.All(x => x != null)

to your query. You can even use let so you're not creating the array twice.

℉絮湮 2025-01-12 22:14:07

根据定义(我引用前面提到的 Eric Lippert

如果概括这个定义,它将是:

任何 n 个序列 S1、S2、...Sn 的笛卡尔积是所有可能的 n 元素序列的序列其中第一个元素来自 S1 并且第二个元素来自 S2,N 元素来自 Sn

请注意,第一个元素应始终来自第一个元素。

如果您过滤掉第一个数组的空值,这意味着整个输入将丢失,则过滤出的输入如下所示:

(空,x2,...xn)

这是一个完整的示例:

数组1 = {null, 1, 2}
数组2 = {A}

array1 与 array1 的笛卡尔积array2 = { {null, A}, {1, A}, {2, A}
}

,让我们应用 First != null 的过滤器。

array1 与 array1 的笛卡尔积array2 = {{1, A}, {2, A}}

假设我们将 array1 的值更改为:

array1 现在 = {null, null}

array1 与 array1 的笛卡尔积带有过滤器的 array2 = {}

结果为空集。为了获得笛卡尔积,您必须从每个数组中获得一个输入。您的问题可以调整以解决这一事实。

By definition (and I am quoting from the previously mentioned Eric Lippert blog).
"The Cartesian product of two sequences S1 and S2 is the sequence of all possible two-element sequences where the first element is from S1 and the second element is from S2."

If you generalize the definition it would be:

The Cartesian product of any n sequences S1, S2,...Sn is the sequence of all possible n-element sequences where the first element is from S1 and the second element is from S2 and the N element is from Sn

Note that the first element should ALWAYS be from the first element.

If you are filtering out null values of the first array that means that the whole entery would be missing, the filtered out enterys looks like this:

(null, x2,...xn)

Here is a complete example:

array1 = {null, 1, 2}
array2 = {A}

The Cartesian product of array1 & array2 = { {null, A}, {1, A}, {2, A}
}

Now, let's apply the filter where First != null.

The Cartesian product of array1 & array2 = {{1, A}, {2, A}}

Say that we changed the value of array1 to:

array1 now = {null, null}

The Cartesian product of array1 & array2 with the filter = {}

The result is empty set. In order to have a cartesian product, you have to have an entery from each array. Your question could be adjusted to address this fact.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文