英文:
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),
];
如你所见,我有A
和B
的重复值。如何以更高效的方式计算这些值之间的平均值并在列表中去除重复项?
我想要的结果类似于这样:
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
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<String> arguments) {
final myList = [
CartesianChartData('A', 25),
CartesianChartData('A', 50),
CartesianChartData('B', 25),
CartesianChartData('B', 75),
CartesianChartData('C', 100),
CartesianChartData('D', 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 = <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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论