How to get average value per object in a list with Dart

huangapple go评论64阅读模式
英文:

How to get average value per object in a list with Dart

问题

我有一个包含X和Y轴数值的图表,存储在一个对象列表中。这些对象由以下类型的类表示:

class CartesianChartData {
  CartesianChartData(this.x, this.y);

  final String x;
  final double? y;
}

因此,最后,假设我有一个这种类型的列表:

List<CartesianChartData> myList = [
      CartesianChartData('A', 25),
      CartesianChartData('A', 50),
      CartesianChartData('B', 25),
      CartesianChartData('B', 75),
      CartesianChartData('C', 100),
      CartesianChartData('D', 25),
      CartesianChartData('E', 0),
];

如你所见,我有AB的重复值。如何以更高效的方式计算这些值之间的平均值并在列表中去除重复项?

我想要的结果类似于这样:

List<CartesianChartData> myList = [
      CartesianChartData('A', 37.5),
      CartesianChartData('B', 50),
      CartesianChartData('C', 100),
      CartesianChartData('D', 25),
      CartesianChartData('E', 0),
];
英文:

I have a chart that contains X and Y axis values stored in a list of object. This objects are represented by a class of this type:

class CartesianChartData {
  CartesianChartData(this.x, this.y);

  final String x;
  final double? y;
}

So, at the end lets say that I have a list of this type:

List<CartesianChartData> myList = [
      CartesianChartData('A', 25),
      CartesianChartData('A', 50),
      CartesianChartData('B', 25),
      CartesianChartData('B', 75),
      CartesianChartData('C', 100),
      CartesianChartData('D', 25),
      CartesianChartData('E', 0),
];

As you can see, I have duplicates of A and B. How can I calculate in the more efficient way the average value between this values and remove duplicates in the list?

I wanna have as result something like this:

List<CartesianChartData> myList = [
      CartesianChartData('A', 37.5),
      CartesianChartData('B', 50),
      CartesianChartData('C', 100),
      CartesianChartData('D', 25),
      CartesianChartData('E', 0),
];

答案1

得分: 1

void main(List arguments) {
final myList = [
CartesianChartData('A', 25),
CartesianChartData('A', 50),
CartesianChartData('B', 25),
CartesianChartData('B', 75),
CartesianChartData('C', 100),
CartesianChartData('D', 25),
];

// 计算每个 x 值的 y 值的平均值
// 这需要为每个 x 值维护总和和计数
final sums = <String, double>{};
final counts = <String, int>{};
for (final item in myList) {
sums.update(item.x, (value) => value + item.y!, ifAbsent: () => item.y!);
counts.update(item.x, (value) => value + 1, ifAbsent: () => 1);
}
for (final entry in sums.entries) {
sums[entry.key] = entry.value / counts[entry.key]!;
print('${entry.key}: ${sums[entry.key]}');
}
}

class CartesianChartData {
CartesianChartData(this.x, this.y);

final String x;
final double? y;
}

produces

A: 37.5
B: 50.0
C: 100.0
D: 25.0

英文:
void main(List&lt;String&gt; arguments) {
  final myList = [
    CartesianChartData(&#39;A&#39;, 25),
    CartesianChartData(&#39;A&#39;, 50),
    CartesianChartData(&#39;B&#39;, 25),
    CartesianChartData(&#39;B&#39;, 75),
    CartesianChartData(&#39;C&#39;, 100),
    CartesianChartData(&#39;D&#39;, 25),
  ];

  // compute the average of the y values for each x value
  // this requires maintaining a sum and a count for each x value
  final sums = &lt;String, double&gt;{};
  final counts = &lt;String, int&gt;{};
  for (final item in myList) {
    sums.update(item.x, (value) =&gt; value + item.y!, ifAbsent: () =&gt; item.y!);
    counts.update(item.x, (value) =&gt; value + 1, ifAbsent: () =&gt; 1);
  }
  for (final entry in sums.entries) {
    sums[entry.key] = entry.value / counts[entry.key]!;
    print(&#39;${entry.key}: ${sums[entry.key]}&#39;);
  }
}

class CartesianChartData {
  CartesianChartData(this.x, this.y);

  final String x;
  final double? y;
}

produces

A: 37.5
B: 50.0
C: 100.0
D: 25.0

huangapple
  • 本文由 发表于 2023年4月11日 06:29:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/75981196.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定