繁体   English   中英

给定相邻元素之间的最小差值,对float的C#集合进行过滤

[英]Filter C# collection of float given a minimum difference value between adjacent elements

假设我有一个有序的float列表(升序)。

我想从中删除下一个元素与其自身之间的差异小于给定阈值的每个元素。

我需要这样的东西:

List<float> orderedList;

IEnumerable<float> query = orderedList.Where(currentNum , nextNum => nextNum - currentNum < threshold);

那可能吗? 如果是,怎么办?

试试看:

var filteredElements = new List<float>();
float ? prev = null;
orderedList.ToList().ForEach((e)=>{ 
              if (prev.HasValue)
              {
                  if (e-prev >= threshold)
                       filteredElements.Add(prev.Value);
              } 
              prev = e
         });

试试这个代替:

var ls1 = ls.Where((item, index) => 
                   item - ls[ls.Count == index + 1 ? index : index + 1] >= 0);

希望这会有所帮助!

尝试这个 -

List<float> orderedList = new List<float>() { 12, 14, 34, 45 };
List<float> itemsToRemove = orderedList.Where((item, index) =>
                            index < orderedList.Count - 1 &&
                            orderedList[index + 1] - item < threshhold).ToList();

这似乎起作用。 (尽管您的问题可能有一些潜在的误解。)

var input = new List<float>() { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 12, 14, 15, 18, 21, 24, 27, 29, 35, 40, 46, 59 };
var output = input.Zip(input.Skip(1).Concat(new[]{float.MaxValue}), (a, b) => new { a, b }).Where(x => x.b - x.a > 2).Select(x => x.a);

这将产生以下输出:

15, 18, 21, 24, 29, 35, 40, 46, 59

这具有使用任何IEnumerable的优点。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM