使用 LINQ 获取列表中的所有对

发布于 2024-12-02 05:30:45 字数 269 浏览 1 评论 0原文

如何获取列表中所有可能的项目对(顺序不相关)?

例如,如果我有,

var list = { 1, 2, 3, 4 };

我想得到这些元组:

var pairs = {
   new Tuple(1, 2), new Tuple(1, 3), new Tuple(1, 4),
   new Tuple(2, 3), new Tuple(2, 4)
   new Tuple(3, 4)
}

How do I get all possible pairs of items in a list (order not relevant)?

E.g. if I have

var list = { 1, 2, 3, 4 };

I would like to get these tuples:

var pairs = {
   new Tuple(1, 2), new Tuple(1, 3), new Tuple(1, 4),
   new Tuple(2, 3), new Tuple(2, 4)
   new Tuple(3, 4)
}

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

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

发布评论

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

评论(3

行至春深 2024-12-09 05:30:45

对 cgeers 答案进行稍微重新表述,以获得所需的元组而不是数组:(

var combinations = from item1 in list
                   from item2 in list
                   where item1 < item2
                   select Tuple.Create(item1, item2);

如果需要,请使用 ToListToArray。)

以非查询表达式形式(稍微重新排序 ) ):

var combinations = list.SelectMany(x => list, (x, y) => Tuple.Create(x, y))
                       .Where(tuple => tuple.Item1 < tuple.Item2);

这两个实际上都会考虑 n2 值而不是 n2/2 值,尽管它们最终会得到正确的答案。另一种选择是:

var combinations = list.SelectMany((x, i) => list.Skip(i + 1), (x, y) => Tuple.Create(x, y));

...但这使用 Skip ,它可能未得到优化。老实说,这可能并不重要 - 我会选择最适合您使用的一个。

Slight reformulation of cgeers answer to get you the tuples you want instead of arrays:

var combinations = from item1 in list
                   from item2 in list
                   where item1 < item2
                   select Tuple.Create(item1, item2);

(Use ToList or ToArray if you want.)

In non-query-expression form (reordered somewhat):

var combinations = list.SelectMany(x => list, (x, y) => Tuple.Create(x, y))
                       .Where(tuple => tuple.Item1 < tuple.Item2);

Both of these will actually consider n2 values instead of n2/2 values, although they'll end up with the correct answer. An alternative would be:

var combinations = list.SelectMany((x, i) => list.Skip(i + 1), (x, y) => Tuple.Create(x, y));

... but this uses Skip which may also not be optimized. It probably doesn't matter, to be honest - I'd pick whichever one is most appropriate for your usage.

哎呦我呸! 2024-12-09 05:30:45

计算笛卡尔积以确定所有可能的组合。

例如:

var combinations = from item in list
                   from item2 in list
                   where item < item2
                   select new[] { item, item2 };

您可以在此处找到有关使用 LINQ 计算笛卡尔积的更多信息:

http://blogs.msdn.com/b/ericlippert/archive/2010/06/28/computing-a-cartesian-product-with-linq.aspx

然后您可以将其转换元组对象的集合。

var pairs = new List<Tuple<int, int>>();
foreach (var pair in combinations)
{
    var tuple = new Tuple<int, int>(pair[0], pair[1]);
    pairs.Add(tuple);
}

或者简而言之:

var combinations = (from item in list
                    from item2 in list
                    where item < item2
                    select new Tuple<int, int>(item, item2)).ToList();

Calculate the Cartesian product to determine all the possible combinations.

For example:

var combinations = from item in list
                   from item2 in list
                   where item < item2
                   select new[] { item, item2 };

You can find more information about calculating a cartesian product using LINQ here:

http://blogs.msdn.com/b/ericlippert/archive/2010/06/28/computing-a-cartesian-product-with-linq.aspx

You can then convert it to a collection of Tuple objects.

var pairs = new List<Tuple<int, int>>();
foreach (var pair in combinations)
{
    var tuple = new Tuple<int, int>(pair[0], pair[1]);
    pairs.Add(tuple);
}

Or in short:

var combinations = (from item in list
                    from item2 in list
                    where item < item2
                    select new Tuple<int, int>(item, item2)).ToList();
乄_柒ぐ汐 2024-12-09 05:30:45

你可以这样解决:

 var list = new[] { 1, 2, 3, 4 };

 var pairs = from l1 in list
             from l2 in list.Except(new[] { l1 })
             where l1 < l2
             select new { l1, l2 };

 foreach (var pair in pairs)
 {
    Console.WriteLine(pair.l1 + ", " + pair.l2);
 }

You could solve it like this:

 var list = new[] { 1, 2, 3, 4 };

 var pairs = from l1 in list
             from l2 in list.Except(new[] { l1 })
             where l1 < l2
             select new { l1, l2 };

 foreach (var pair in pairs)
 {
    Console.WriteLine(pair.l1 + ", " + pair.l2);
 }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文