Skip to main content
 首页 » 编程设计

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

2024年02月02日31sxdcgaq8080

假设我有一个有序的 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 
         });