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

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

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

问题

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

  1. class CartesianChartData {
  2. CartesianChartData(this.x, this.y);
  3. final String x;
  4. final double? y;
  5. }

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

  1. List<CartesianChartData> myList = [
  2. CartesianChartData('A', 25),
  3. CartesianChartData('A', 50),
  4. CartesianChartData('B', 25),
  5. CartesianChartData('B', 75),
  6. CartesianChartData('C', 100),
  7. CartesianChartData('D', 25),
  8. CartesianChartData('E', 0),
  9. ];

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

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

  1. List<CartesianChartData> myList = [
  2. CartesianChartData('A', 37.5),
  3. CartesianChartData('B', 50),
  4. CartesianChartData('C', 100),
  5. CartesianChartData('D', 25),
  6. CartesianChartData('E', 0),
  7. ];
英文:

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:

  1. class CartesianChartData {
  2. CartesianChartData(this.x, this.y);
  3. final String x;
  4. final double? y;
  5. }

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

  1. List<CartesianChartData> myList = [
  2. CartesianChartData('A', 25),
  3. CartesianChartData('A', 50),
  4. CartesianChartData('B', 25),
  5. CartesianChartData('B', 75),
  6. CartesianChartData('C', 100),
  7. CartesianChartData('D', 25),
  8. CartesianChartData('E', 0),
  9. ];

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:

  1. List<CartesianChartData> myList = [
  2. CartesianChartData('A', 37.5),
  3. CartesianChartData('B', 50),
  4. CartesianChartData('C', 100),
  5. CartesianChartData('D', 25),
  6. CartesianChartData('E', 0),
  7. ];

答案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

英文:
  1. void main(List&lt;String&gt; arguments) {
  2. final myList = [
  3. CartesianChartData(&#39;A&#39;, 25),
  4. CartesianChartData(&#39;A&#39;, 50),
  5. CartesianChartData(&#39;B&#39;, 25),
  6. CartesianChartData(&#39;B&#39;, 75),
  7. CartesianChartData(&#39;C&#39;, 100),
  8. CartesianChartData(&#39;D&#39;, 25),
  9. ];
  10. // compute the average of the y values for each x value
  11. // this requires maintaining a sum and a count for each x value
  12. final sums = &lt;String, double&gt;{};
  13. final counts = &lt;String, int&gt;{};
  14. for (final item in myList) {
  15. sums.update(item.x, (value) =&gt; value + item.y!, ifAbsent: () =&gt; item.y!);
  16. counts.update(item.x, (value) =&gt; value + 1, ifAbsent: () =&gt; 1);
  17. }
  18. for (final entry in sums.entries) {
  19. sums[entry.key] = entry.value / counts[entry.key]!;
  20. print(&#39;${entry.key}: ${sums[entry.key]}&#39;);
  21. }
  22. }
  23. class CartesianChartData {
  24. CartesianChartData(this.x, this.y);
  25. final String x;
  26. final double? y;
  27. }

produces

  1. A: 37.5
  2. B: 50.0
  3. C: 100.0
  4. 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:

确定