gpt4 book ai didi

c# - 选择具有组最小值的组的所有元素

转载 作者:太空宇宙 更新时间:2023-11-03 12:41:03 25 4
gpt4 key购买 nike

这是表格

 Tuple<dynamic, dynamic> pair = new Tuple<dynamic, dynamic>() = 
{({ Symbol = a, Close = 1.0, Date = 2/1/2016, Vol = 0 }, { Symbol = b, Close = 1.6, Date = 2/1/2016, Vol = 0 })}
{({ Symbol = a, Close = 1.0, Date = 2/1/2016, Vol = 0 }, { Symbol = c, Close = 2.0, Date = 2/1/2016 , Vol = 0 })}
{({ Symbol = b, Close = 1.6, Date = 2/1/2016, Vol = 0 }, { Symbol = c, Close = 2.0, Date = 2/1/2016, Vol = 0 })}
{({ Symbol = a, Close = 1.1, Date = 2/2/2016, Vol = 0 }, { Symbol = b, Close = 1.2, Date = 2/2/2016, Vol = 0 })}
{({ Symbol = a, Close = 1.1, Date = 2/2/2016, Vol = 0 }, { Symbol = c, Close = 2.1, Date = 2/2/2016, Vol = 0 })}
{({ Symbol = b, Close = 1.2, Date = 2/2/2016, Vol = 0 }, { Symbol = c, Close = 2.1, Date = 2/2/2016, Vol = 0 })}
{({ Symbol = a, Close = 1.4, Date = 2/3/2016, Vol = 0 }, { Symbol = b, Close = 1.4, Date = 2/3/2016, Vol = 0 })}
{({ Symbol = a, Close = 1.4, Date = 2/3/2016, Vol = 0 }, { Symbol = c, Close = 1.4, Date = 2/3/2016, Vol = 0 })}
{({ Symbol = b, Close = 1.4, Date = 2/3/2016, Vol = 0 }, { Symbol = c, Close = 1.4, Date = 2/3/2016, Vol = 0 })}
{({ Symbol = a, Close = 1.7, Date = 2/4/2016, Vol = 0.214285714285714 }, { Symbol = b, Close = 1.8, Date = 2/4/2016, Vol = 0.218217890235992 })}
{({ Symbol = a, Close = 1.7, Date = 2/4/2016, Vol = 0.214285714285714 }, { Symbol = c, Close = 2.7, Date = 2/4/2016, Vol = 0.464743364189122 })}
{({ Symbol = b, Close = 1.8, Date = 2/4/2016, Vol = 0.218217890235992 }, { Symbol = c, Close = 2.7, Date = 2/4/2016, Vol = 0.464743364189122 })}
{({ Symbol = a, Close = 1.8, Date = 2/5/2016, Vol = 0.122450941145067 }, { Symbol = b, Close = 1.2, Date = 2/5/2016, Vol = 0.169725025739105 })}
{({ Symbol = a, Close = 1.8, Date = 2/5/2016, Vol = 0.122450941145067 }, { Symbol = c, Close = 1.2, Date = 2/5/2016, Vol = 0.301649178342484 })}
{({ Symbol = b, Close = 1.2, Date = 2/5/2016, Vol = 0.169725025739105 }, { Symbol = c, Close = 1.2, Date = 2/5/2016, Vol = 0.301649178342484 })}
......
......

我想在 '2/3/2016' 之后的每一天选择 Vol(min|Vol1 - Vol2|) 的最小摘要对(前一个日期可以是默认值)预期形式如下:

  {({ Symbol = a, Close = 1.7, Date = 2/4/2016, Vol = 0.214285714285714 }, { Symbol = b, Close = 1.8, Date = 2/4/2016, Vol = 0.218217890235992 }), Volspread = XXXX}
{({ Symbol = b, Close = 1.2, Date = 2/5/2016, Vol = 0.169725025739105 }, { Symbol = c, Close = 1.2, Date = 2/5/2016, Vol = 0.301649178342484 }), Volspread = XXXX}
.......
.......

这里 Volspread 分别是最小绝对值。

最佳答案

用于选择每组中具有min 差异的项目:

var result = pairs
.Where(pair => pair.Item1.Date > new DateTime(2016, 2, 3))
.Select(pair => new { Item = pair, Diff = Math.Abs(pair.Item1.Vol - pair.Item2.Vol) })
.GroupBy(pair => pair.Item.Item1.Date)
.SelectMany(group => group.Where(item => item.Diff.Equals(group.Min(i => i.Diff))).Select(item => item.Item))
.ToList();

以及测试数据:

List<Tuple<dynamic, dynamic>> pairs = new List<Tuple<dynamic, dynamic>>
{
new Tuple<dynamic,dynamic>( new { Symbol = "a", Close = 1.0, Date = new DateTime(2016,2,1), Vol = 0 }, new { Symbol = "b", Close = 1.6, Date = new DateTime(2016,2,1), Vol = 0 }),
new Tuple<dynamic,dynamic>( new { Symbol = "a", Close = 1.0, Date = new DateTime(2016,2,1), Vol = 0 }, new { Symbol = "c", Close = 2.0, Date = new DateTime(2016, 2, 1), Vol = 0 }),
new Tuple<dynamic,dynamic>( new { Symbol = "b", Close = 1.6, Date = new DateTime(2016,2,1), Vol = 0 }, new { Symbol = "c", Close = 2.0, Date = new DateTime(2016, 2, 1), Vol = 0 }),
new Tuple<dynamic,dynamic>( new { Symbol = "a", Close = 1.1, Date = new DateTime(2016,2,2), Vol = 0 }, new { Symbol = "b", Close = 1.2, Date = new DateTime(2016,2,2), Vol = 0 }),
new Tuple<dynamic,dynamic>( new { Symbol = "a", Close = 1.1, Date = new DateTime(2016,2,2), Vol = 0 }, new { Symbol = "c", Close = 2.1, Date = new DateTime(2016,2,2), Vol = 0 }),
new Tuple<dynamic,dynamic>( new { Symbol = "b", Close = 1.2, Date = new DateTime(2016,2,2), Vol = 0 }, new { Symbol = "c", Close = 2.1, Date = new DateTime(2016,2,2), Vol = 0 }),
new Tuple<dynamic,dynamic>( new { Symbol = "a", Close = 1.4, Date = new DateTime(2016,2,3), Vol = 0 }, new { Symbol = "b", Close = 1.4, Date = new DateTime(2016,2,3), Vol = 0 }),
new Tuple<dynamic,dynamic>( new { Symbol = "a", Close = 1.4, Date = new DateTime(2016,2,3), Vol = 0 }, new { Symbol = "c", Close = 1.4, Date = new DateTime(2016,2,3), Vol = 0 }),
new Tuple<dynamic,dynamic>( new { Symbol = "b", Close = 1.4, Date = new DateTime(2016,2,3), Vol = 0 }, new { Symbol = "c", Close = 1.4, Date = new DateTime(2016,2,3), Vol = 0 }),
new Tuple<dynamic,dynamic>( new { Symbol = "a", Close = 1.7, Date = new DateTime(2016,2,4), Vol = 0.214285714285714 }, new { Symbol = "b", Close = 1.8, Date = new DateTime(2016,2,4), Vol = 0.218217890235992 }),
new Tuple<dynamic,dynamic>( new { Symbol = "a", Close = 1.7, Date = new DateTime(2016,2,4), Vol = 0.214285714285714 }, new { Symbol = "c", Close = 2.7, Date = new DateTime(2016,2,4), Vol = 0.464743364189122 }),
new Tuple<dynamic,dynamic>( new { Symbol = "b", Close = 1.8, Date = new DateTime(2016,2,4), Vol = 0.218217890235992 }, new { Symbol = "c", Close = 2.7, Date = new DateTime(2016,2,4), Vol = 0.464743364189122 }),
new Tuple<dynamic,dynamic>( new { Symbol = "a", Close = 1.8, Date = new DateTime(2016,2,5), Vol = 0.122450941145067 }, new { Symbol = "b", Close = 1.2, Date = new DateTime(2016,2,5), Vol = 0.169725025739105 }),
new Tuple<dynamic,dynamic>( new { Symbol = "a", Close = 1.8, Date = new DateTime(2016,2,5), Vol = 0.122450941145067 }, new { Symbol = "c", Close = 1.2, Date = new DateTime(2016,2,5), Vol = 0.301649178342484 }),
new Tuple<dynamic,dynamic>( new { Symbol = "b", Close = 1.2, Date = new DateTime(2016,2,5), Vol = 0.169725025739105 }, new { Symbol = "c", Close = 1.2, Date = new DateTime(2016,2,5), Vol = 0.301649178342484 }),
};

编辑前:

你可以这样做:

//Remove irrelevant records and calculate diff
var result = pairs
.Where(item => item.Item1.Date > new DateTime(2016, 2, 3))
.Select(item => new { Item = item, Diff = Math.Abs(item.Item1.Vol - item.Item2.Vol) });

//Find min diff
var minDiff = result.Min(item => item.Diff);

//Get only records with min diff
result = result.Where(item => item.Diff.Equals(minDiff)).ToList();

另一种选择是使用 MoreLinq 的 nuget 库来实现 MinBy

关于c# - 选择具有组最小值的组的所有元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38969815/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com