如何获取最近数字的列表索引?

发布于 2024-11-06 07:17:08 字数 224 浏览 1 评论 0原文

如何获取可以找到最接近数字的列表索引?

List<int> list = new List<int> { 2, 5, 7, 10 };
int number = 9;

int closest = list.Aggregate((x,y) => 
Math.Abs(x-number) < Math.Abs(y-number) ? x : y);

How to get the list index where you can find the closest number?

List<int> list = new List<int> { 2, 5, 7, 10 };
int number = 9;

int closest = list.Aggregate((x,y) => 
Math.Abs(x-number) < Math.Abs(y-number) ? x : y);

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

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

发布评论

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

评论(5

手心的海 2024-11-13 07:17:08

如果你想要最接近的数字的索引,这可以解决问题:

int index = list.IndexOf(closest);

If you want the index of the closest number this will do the trick:

int index = list.IndexOf(closest);
污味仙女 2024-11-13 07:17:08

您可以将每个项目的索引包含在匿名类中并将其传递给聚合函数,并在查询结束时可用:

var closest = list
    .Select( (x, index) => new {Item = x, Index = index}) // Save the item index and item in an anonymous class
    .Aggregate((x,y) => Math.Abs(x.Item-number) < Math.Abs(y.Item-number) ? x : y);

var index = closest.Index;

You can include the index of each item in an anonymous class and pass it through to the aggregate function, and be available at the end of the query:

var closest = list
    .Select( (x, index) => new {Item = x, Index = index}) // Save the item index and item in an anonymous class
    .Aggregate((x,y) => Math.Abs(x.Item-number) < Math.Abs(y.Item-number) ? x : y);

var index = closest.Index;
寄居人 2024-11-13 07:17:08

只需枚举索引并选择具有最小增量的索引,就像执行常规循环一样。

const int value = 9;
var list = new List<int> { 2, 5, 7, 10 };
var minIndex = Enumerable.Range(1, list.Count - 1)
    .Aggregate(0, (seed, index) =>
        Math.Abs(list[index] - value) < Math.Abs(list[seed] - value)
            ? index
            : seed);

Just enumerate over indices and select the index with the smallest delta as you would if you did a regular loop.

const int value = 9;
var list = new List<int> { 2, 5, 7, 10 };
var minIndex = Enumerable.Range(1, list.Count - 1)
    .Aggregate(0, (seed, index) =>
        Math.Abs(list[index] - value) < Math.Abs(list[seed] - value)
            ? index
            : seed);
眼泪也成诗 2024-11-13 07:17:08

对您已有的内容进行非常小的更改,因为您似乎已经有了答案:

        List<int> list = new List<int> { 2, 5, 7, 10 }; int number = 9;

        int closest = list.Aggregate((x, y) => Math.Abs(x - number) < Math.Abs(y - number) ? x : y);

Very minor changes to what you already have, since you seem to have the answer already:

        List<int> list = new List<int> { 2, 5, 7, 10 }; int number = 9;

        int closest = list.Aggregate((x, y) => Math.Abs(x - number) < Math.Abs(y - number) ? x : y);
惟欲睡 2024-11-13 07:17:08

最接近的数字是差异最小的数字:

int closest = list.OrderBy(n => Math.Abs(number - n)).First();

The closest number is the one where the difference is the smallest:

int closest = list.OrderBy(n => Math.Abs(number - n)).First();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文