arrays - 如何在飞镖列表中获取重复值的总和?
问题描述
我想获得重复值的总和并将它们放在另一个列表中.. 有可能吗?
List list = [1, 1 , 2, 3,2];
List newList = [2,4,3];
解决方案
当然,这是可能的。您可以定义一个临时列表以防止列表中出现重复项。尝试这个:
List list = [1, 1, 2, 3, 2];
List tempList = [];
List newList = [2, 4, 3];
for (var item in list) {
if (!tempList.contains(item)) {
newList.add(list.where((e) => e == item).length * item);
tempList.add(item);
}
}
推荐阅读
- arrays - 扩展 UITableViewCell 不读取结构元素
- excel - 如何设置 xlColumnClustered 样式,每个条形下方只有一种颜色和值?
- java - 使用 Play Framework 和 Logback 的 Java 内存泄漏
- sql - slick - 选项的动态过滤列表
- mysql - 如何在 XAMPP(Mac 版)中用 MySQL 替换 MariaDB
- javascript - 单击我的按钮重新加载另一个句子
- php - 如何修复 PHP 中的“没有这样的文件或目录错误”?
- python - 从二进制数组 [1, 0, 1, 0, 0, 1, 0, 1, 1, 1, 0, 1] 绘制平方信号?
- python - 计算每个关键字和每个标识符的出现次数
- unity3d - 如何检查项目中是否安装了包