创建N * M值的所有组合

编程入门 行业动态 更新时间:2024-10-12 16:25:41
本文介绍了创建N * M值的所有组合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

说我有的数据结构的IEnumerable<&IEnumerable的LT;对象>> 是这样的:

{ { A, B } { 1, 2, 3 } { Z } }

凡外数组可以包含的内阵列的任何数量的的。和内部数组可以各自独立地包含的任意数量的的元素。并假定,为了简单起见,没有阵列将是空的。

Where the outer array can contain any number of inner arrays. And the inner arrays can each independently contain any number of elements. And assume, for the sake of simplicity, that no array will be empty.

和我想将其转换为一个的IEnumerable<&IEnumerable的LT;对象>> 是这样的:

And I want to transform it to a IEnumerable<IEnumerable<object>> like this:

{ { A, 1, Z }, { A, 2, Z }, { A, 3, Z }, { B, 1, Z }, { B, 2, Z }, { B, 3, Z } }

其中包含从原始结构中的值的每一个组合。所以每个内阵列中的每个元素由索引映射到原始外数组中的元素/阵列

Which contains every combination of the values from the original structure. So each element in each inner array maps by index to an element/array in the original outer array.

什么是做,在C#中最简单的方法是什么?

What is the simplest way to do that in C#?

推荐答案

您可以为此使用笛卡儿积方法,通过埃里克利珀(从here):

You could use CartesianProduct method by Eric Lippert for this (taken from here):

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})); }

更多推荐

创建N * M值的所有组合

本文发布于:2023-11-29 14:11:59,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1646613.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:组合

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!