多个清单的组合

编程入门 行业动态 更新时间:2024-10-24 02:26:02
本文介绍了多个清单的组合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我不能完全确定组合"一词的正确性,但是我需要从一个或多个列表"中构建一个组合列表.每个列表将包含不同数量的元素,例如

I'm not completely sure that the term 'Combination' is correct, however I have a requirement to build a list of combination form one or more List. Each list will contain a varying number of elements, e.g.

List<string> lBag1 = ["1_0, 1_1, 1_3"] List<string> lBag2 = ["11_0, 11_1, 11_8"] List<string> lBag3 = ["3_0"]

我需要的是列表形式从1到n的所有组合,每个列表中的元素不超过一个,例如

What I need is all combination of the Lists form 1 to n elements with no more than one element from each list, e.g.

"1_0" "1_1" "1_3" "11_0" "11_1" "11_8" "3_0" "1_0 11_0" "1_0 11_1" "1_0 11_8" "1_0 3_0" ... "1_3 11_8 3_0"

顺序并不重要,因此"1_0 11_0"被视为与"11_0 1_0"相同.

Order is not important, so "1_0 11_0" is considered the same as "11_0 1_0".

任何帮助将不胜感激

推荐答案

这两个扩展方法将使您可以将多个枚举链接在一起,从而计算出所需的组合.

These two extension methods will let you chain together several enumerations, calculating the combinations you want.

每个组合都是一个枚举,而不是连接的字符串.

Each combination is an enumeration, rather than a concatenated string.

// This method takes two sequences of T, and returns // - each element of the first sequence, // wrapped in its own one-element sequence // - each element of the second sequence, // wrapped in its own one-element sequence // - each pair of elements (one from each sequence), // as a two-element sequence. // e.g. { 1 }.CrossWith({ 2 }) returns { { 1 }, { 2 }, { 1, 2 } } public static IEnumerable<IEnumerable<T>> CrossWith<T>( this IEnumerable<T> source1, IEnumerable<T> source2) { foreach(T s1 in source1) yield return new[] { s1 }; foreach(T s2 in source2) yield return new[] { s2 }; foreach(T s1 in source1) foreach(T s2 in source2) yield return new[] { s1, s2 }; } // This method takes a sequence of sequences of T and a sequence of T, // and returns // - each sequence from the first sequence // - each element of the second sequence, // wrapped in its own one-element sequence // - each pair, with the element from the second sequence appended to the // sequence from the first sequence. // e.g. { { 1, 2 } }.CrossWith({ 3 }) returns // { { 1, 2 }, { 3 }, { 1, 2, 3 } } public static IEnumerable<IEnumerable<T>> CrossWith<T>( this IEnumerable<IEnumerable<T>> source1, IEnumerable<T> source2) { foreach(IEnumerable<T> s1 in source1) yield return s1; foreach(T s2 in source2) yield return new[] { s2 }; foreach(IEnumerable<T> s1 in source1) foreach(T s2 in source2) yield return s1.Concat(new[] { s2 }).ToArray(); } var cross = lBag1.CrossWith(lBag2).CrossWith(lBag3); // { "1_0" }, { "1_1" }, { "1_3" } ... // ... { "1_0", "11_0" }, ...

或者,还有经典的Eric Lippert 博客文章也做了类似的事情.(结果相似,方法截然不同.)

Alternatively, there is this classic Eric Lippert blog post that does a similar thing. (Similar result, very different method.)

更多推荐

多个清单的组合

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

发布评论

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

>www.elefans.com

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