获取列表>> 使用 LINQ 的 C# 中的元素位置

发布于 2024-07-26 15:27:27 字数 222 浏览 9 评论 0原文

我有一个带有数字的列表, 我想使用 LINQ 找到最小值(不是值)的位置 示例

var lst = new List<int>() { 3, 1, 0, 5 };

现在我正在寻找一个返回我的函数

输出 = 2

因为最小值位于列表中的位置 2。

I have a List with numbers,
and I'd like to find the position of the minimum (not value) using LINQ

Example:

var lst = new List<int>() { 3, 1, 0, 5 };

Now I am looking for a function returning me

output = 2

because the minimum is at position 2 in the list.

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

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

发布评论

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

评论(11

沫雨熙 2024-08-02 15:27:28
int min = 0;
bool minIsSet = false;

var result = ints
  .Select( (x, i) => new {x, i}
  .OrderBy(z => z.x)
  .Select(z => 
  {
    if (!minIsSet)
    {
      min = z.x;
      minIsSet = true;
    }
    return z;
  }
  .TakeWhile(z => z.x == min)
  .Select(z => z.i);
int min = 0;
bool minIsSet = false;

var result = ints
  .Select( (x, i) => new {x, i}
  .OrderBy(z => z.x)
  .Select(z => 
  {
    if (!minIsSet)
    {
      min = z.x;
      minIsSet = true;
    }
    return z;
  }
  .TakeWhile(z => z.x == min)
  .Select(z => z.i);
划一舟意中人 2024-08-02 15:27:28

我不一定推荐这种 CPS 风格的代码,但它可以工作并且时间复杂度为 O(n),这与使用 OrderBy:

var minIndex = list.Aggregate(
    new { i = 0, mini = -1, minv = int.MaxValue },
    (min, x) => (min.minv > x)
        ? new { i = min.i + 1, mini = min.i, minv = x }
        : new { i = min.i + 1, mini = min.mini, minv = min.minv })
    .mini;

Change > 的解决方案不同。 >= 如果您想要最后一个最小重复项,而不是第一个。

使用 .minv 获取最小值或两者都不获取同时包含索引和最小值的 2 元组。

我迫不及待地希望 .NET 在 4.0 中获得元组。

I don't necessarily recommend this CPS-style code, but it works and is O(n), unlike the solutions that use OrderBy:

var minIndex = list.Aggregate(
    new { i = 0, mini = -1, minv = int.MaxValue },
    (min, x) => (min.minv > x)
        ? new { i = min.i + 1, mini = min.i, minv = x }
        : new { i = min.i + 1, mini = min.mini, minv = min.minv })
    .mini;

Change > to >= if you want the last minimum duplicate, not the first.

Use .minv to get the minimum value or neither to get a 2-tuple with both the index and the minimum value.

I can't wait for .NET to get tuples in 4.0.

〃温暖了心ぐ 2024-08-02 15:27:28

如果在列表中查找 1 个或多个具有相同值的元素的位置:

using System.Linq;
List<int> lst = new List<int>() { 3, 1, 0, 5, 0 }; // works for int array as well

List<int> ids = lst.Select((value, index) => new { value, index })
                     .Where(x => x.value == 0)
                     .Select(x => x.index)
                     .ToList(); // returns 2, 4

If looking for the position of 1 or more elements of the same value in a list:

using System.Linq;
List<int> lst = new List<int>() { 3, 1, 0, 5, 0 }; // works for int array as well

List<int> ids = lst.Select((value, index) => new { value, index })
                     .Where(x => x.value == 0)
                     .Select(x => x.index)
                     .ToList(); // returns 2, 4
孤蝉 2024-08-02 15:27:28
List<int>.Enumerator e = l.GetEnumerator();
int p = 0, min = int.MaxValue, pos = -1;
while (e.MoveNext())
{
    if (e.Current < min)
    {
        min = e.Current;
        pos = p;
    }
    ++p;
}
List<int>.Enumerator e = l.GetEnumerator();
int p = 0, min = int.MaxValue, pos = -1;
while (e.MoveNext())
{
    if (e.Current < min)
    {
        min = e.Current;
        pos = p;
    }
    ++p;
}
无人接听 2024-08-02 15:27:27
var list = new List<int> { 3, 1, 0, 5 };
int pos = list.IndexOf(list.Min()); // returns 2
var list = new List<int> { 3, 1, 0, 5 };
int pos = list.IndexOf(list.Min()); // returns 2
岁吢 2024-08-02 15:27:27

由于您特别要求 LINQ 解决方案,而您得到的只是非 LINQ 解决方案,因此这里有一个 LINQ 解决方案:

List<int> values = new List<int> { 3, 1, 0, 5 };

int index =
   values
   .Select((n, i) => new { Value = n, Index = i })
   .OrderBy(n=>n.Value)
   .First()
   .Index;

但这并不意味着 LINQ 是解决此问题的最佳解决方案...

编辑:

使用更复杂的代码这表现得更好一些:

int index =
   values
   .Select((n, i) => new { Value = n, Index = i })
   .Aggregate((a,b) => a.Value < b.Value ? a : b)
   .Index;

为了获得最佳性能,您可以使用一个简单的循环来遍历所有项目,同时跟踪最低的项目:

int index = 0, value = values[0];
for (int i = 1; i < values.Length; i++) {
  if (values[i] < value) {
    value = values[i];
    index = i;
  }
}

As you specifically asked for a LINQ solution, and all you got was non-LINQ solutions, here's a LINQ solution:

List<int> values = new List<int> { 3, 1, 0, 5 };

int index =
   values
   .Select((n, i) => new { Value = n, Index = i })
   .OrderBy(n=>n.Value)
   .First()
   .Index;

That however doesn't mean that LINQ is the best solution for this problem...

Edit:

With a bit more complex code this performs a little better:

int index =
   values
   .Select((n, i) => new { Value = n, Index = i })
   .Aggregate((a,b) => a.Value < b.Value ? a : b)
   .Index;

To get the best performance, you would use a plain loop go get through the items, while you keep track of the lowest:

int index = 0, value = values[0];
for (int i = 1; i < values.Length; i++) {
  if (values[i] < value) {
    value = values[i];
    index = i;
  }
}
染墨丶若流云 2024-08-02 15:27:27

捕获位置的最佳方法是通过 FindIndex
此功能仅适用于 List<>

示例

int id = listMyObject.FindIndex(x => x.Id == 15); 

如果您有枚举器或数组,请使用这种方式

int id = myEnumerator.ToList().FindIndex(x => x.Id == 15); 

   int id = myArray.ToList().FindIndex(x => x.Id == 15); 

The best way to catch the position is by FindIndex
This function is available only for List<>

Example

int id = listMyObject.FindIndex(x => x.Id == 15); 

If you have enumerator or array use this way

int id = myEnumerator.ToList().FindIndex(x => x.Id == 15); 

or

   int id = myArray.ToList().FindIndex(x => x.Id == 15); 
老旧海报 2024-08-02 15:27:27

我同意 LINQ 不是解决此问题的最佳解决方案,但这里有另一种变体,即 O(n)。 它不排序,只遍历列表一次。

var list = new List<int> { 3, 1, 0, 5 };
int pos = Enumerable.Range(0, list.Count)
    .Aggregate((a, b) => (list[a] < list[b]) ? a : b); // returns 2

I agree that LINQ isn't the best solution for this problem, but here's another variation that is O(n). It doesn't sort and only traverses the list once.

var list = new List<int> { 3, 1, 0, 5 };
int pos = Enumerable.Range(0, list.Count)
    .Aggregate((a, b) => (list[a] < list[b]) ? a : b); // returns 2
無處可尋 2024-08-02 15:27:27
var data = new List<int> { 3, 1, 0, 5 };

var result = Enumerable.Range(0, data.Count).OrderBy(n => data[n]).First();
var data = new List<int> { 3, 1, 0, 5 };

var result = Enumerable.Range(0, data.Count).OrderBy(n => data[n]).First();
得不到的就毁灭 2024-08-02 15:27:27

列表可以包含多个等于最小值的元素(见下文)。

我编写的通用扩展方法 .FindEveryIndex() 适用于整数、字符串……并且非常灵活,因为您可以将条件指定为 Lambda 表达式。

另一个优点是它返回与条件匹配的所有索引的列表,而不仅仅是第一个元素。

关于您的问题:最小值可以返回为:

var lst = new List<int>() { 1, 2, 1, 3, 4, 1 };  // example list
var minimum = lst.Min();  // get the minumum value of lst
var idx = lst.FindEveryIndex(x => x == minimum);  // finds all indices matching condition
Console.WriteLine($"Output: {String.Join(',', idx.ToArray())}");  // show list of indices

它将返回索引 0、2 和 5,因为 lst1 中的最小值是 1

输出:0,2,5

示例 2:

void Main()
{   
    // working with list of integers
    var lst1 = new List<int>() { 1, 2, 1, 3, 4, 1 };
    lst1.FindEveryIndex(x => x==1).Dump("Find 1");   // finds indices: [0, 2, 5]
    lst1.FindEveryIndex(x => x==2).Dump("Find 2");   // finds index: [1]
    lst1.FindEveryIndex(x => x==9).Dump("Find 9");   // returns [-1]

    // working with list of strings
    var lst2 = new List<string>() { "A", "B", "A", "C", "D", "A"};
    lst2.FindEveryIndex(x => x=="A").Dump("Find A");   // finds indices: [0, 2, 5]
    lst2.FindEveryIndex(x => x=="B").Dump("Find B");   // finds index: [1]
    lst2.FindEveryIndex(x => x=="X").Dump("Find X");   // returns [-1]
}

扩展类:

public static class Extension
{
    // using System.Collections.Generic;
    public static IEnumerable<int> FindEveryIndex<T>(this IEnumerable<T> items, 
                                                     Predicate<T> predicate)
    {
        int index = 0; bool found = false;
        foreach (var item in items)
        {
            if (predicate(item))
            {
                found = true; yield return index;
            };
            index++;
        }
        if (!found) yield return -1;
    }
}

注意: 将两个代码片段复制到 LinqPad C# 程序中,它会立即运行。

或者,使用 DotNetFiddle 在线运行它。

A list can contain multiple elements which are equal to the minimum value (see below).

The generic extension method .FindEveryIndex() I wrote works with integers, strings, ... and is quite flexible because you can specify your condition as Lambda expression.

Another advantage is that it returns a list of all indices matching the condition, not just the first element.

Regarding your question: The minimum can be returned as:

var lst = new List<int>() { 1, 2, 1, 3, 4, 1 };  // example list
var minimum = lst.Min();  // get the minumum value of lst
var idx = lst.FindEveryIndex(x => x == minimum);  // finds all indices matching condition
Console.WriteLine(
quot;Output: {String.Join(',', idx.ToArray())}");  // show list of indices

It will return the indices 0, 2 and 5, because the minimum in lst1 is 1:

Output: 0,2,5

Example 2:

void Main()
{   
    // working with list of integers
    var lst1 = new List<int>() { 1, 2, 1, 3, 4, 1 };
    lst1.FindEveryIndex(x => x==1).Dump("Find 1");   // finds indices: [0, 2, 5]
    lst1.FindEveryIndex(x => x==2).Dump("Find 2");   // finds index: [1]
    lst1.FindEveryIndex(x => x==9).Dump("Find 9");   // returns [-1]

    // working with list of strings
    var lst2 = new List<string>() { "A", "B", "A", "C", "D", "A"};
    lst2.FindEveryIndex(x => x=="A").Dump("Find A");   // finds indices: [0, 2, 5]
    lst2.FindEveryIndex(x => x=="B").Dump("Find B");   // finds index: [1]
    lst2.FindEveryIndex(x => x=="X").Dump("Find X");   // returns [-1]
}

Extension class:

public static class Extension
{
    // using System.Collections.Generic;
    public static IEnumerable<int> FindEveryIndex<T>(this IEnumerable<T> items, 
                                                     Predicate<T> predicate)
    {
        int index = 0; bool found = false;
        foreach (var item in items)
        {
            if (predicate(item))
            {
                found = true; yield return index;
            };
            index++;
        }
        if (!found) yield return -1;
    }
}

Note: Copy the two code snippets into a LinqPad C# program and it works instantly.

Or, run it online with DotNetFiddle.

对你的占有欲 2024-08-02 15:27:27
List<int> data = new List<int>();
data.AddRange(new[] { 3, 1, 0, 5 });
Console.WriteLine(data.IndexOf(data.Min()));
List<int> data = new List<int>();
data.AddRange(new[] { 3, 1, 0, 5 });
Console.WriteLine(data.IndexOf(data.Min()));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文