【问题标题】:extract common elements from lists in dart从 dart 中的列表中提取常见元素
【发布时间】:2020-04-13 08:30:18
【问题描述】:

我有 3 个列表,例如:

  List l1 = [1, 2, 3, 55, 7, 99, 21];
  List l2 = [1, 4, 7, 65, 99, 20, 21];
  List l3 = [0, 2, 6, 7, 21, 99, 26];

我希望有共同的元素:

// [7,99,21]

这是我尝试过但无法正常工作的方法:

 List l1 = [1, 2, 3, 55, 7, 99, 21];
 List l2 = [1, 4, 7, 65, 99, 20, 21];
 List l3 = [0, 2, 6, 7, 21, 99, 26];
 List common = l1;

 l2.forEach((element) {
   l3.forEach((element2) {
    if (!common.contains(element) || !common.contains(element2)) {
    common.remove(element);
    common.remove(element2);
    }
   });
 });

print(common);

另外,列表的数量是动态的,所以我希望像这样嵌套它们,我没有递归经验,所以我不能这样做,甚至不知道它是否比嵌套循环更好。

感谢您的帮助。

【问题讨论】:

  • Set s1 = Set.from(l1); Set s2 = Set.from(l2); Set s3 = Set.from(l3); print(s1.intersection(s2).intersection(s3));
  • 或者更简单的一行代码:print([l1, l2, l3].map((l) => Set.from(l)).reduce((v, e) => v.intersection(e)));

标签: flutter dart


【解决方案1】:

您不需要嵌套循环或递归。 Dart 在Lists 上有Sets 和一个非常好的fold 方法。

main() {
  final lists = [
    [1, 2, 3, 55, 7, 99, 21],
    [1, 4, 7, 65, 99, 20, 21],
    [0, 2, 6, 7, 21, 99, 26]
  ];

  final commonElements =
      lists.fold<Set>(
        lists.first.toSet(), 
        (a, b) => a.intersection(b.toSet()));

  print(commonElements);
}

给予:

{7, 99, 21}

此外,无论lists 中包含多少个列表,都可以使用它。

【讨论】:

    【解决方案2】:

    一个解决方案:

    void main() {
      List l1 = [1, 2, 3, 55, 7, 99, 21];
      List l2 = [1, 4, 7, 65, 99, 20, 21];
      List l3 = [0, 2, 6, 7, 21, 99, 26];
    
      l1.removeWhere((item) => !l2.contains(item));
      l1.removeWhere((item) => !l3.contains(item));
    
      print(l1);
    }
    

    结果:

    [7, 99, 21]

    如果您的列表数量是动态的,那么解决方案是计算所有列表中的所有出现次数,并仅保留出现次数等于列表数量的值:

    void main() {
      List<List> lists = [
        [1, 2, 3, 55, 7, 99, 21],
        [1, 4, 7, 65, 99, 20, 21],
        [0, 2, 6, 7, 21, 99, 26]
      ];
    
      Map map = Map();
      for (List l in lists) {
        l.forEach((item) => map[item] = map.containsKey(item) ? (map[item] + 1) : 1);
      }
    
      var commonValues = map.keys.where((key) => map[key] == lists.length);
    
      print(commonValues);
    }
    

    结果:

    (7, 99, 21)

    【讨论】:

      【解决方案3】:

      在项目中复制和粘贴的功能:

      List<T> intersection<T>(Iterable<Iterable<T>> iterables) {
        return iterables
          .map((e) => e.toSet())
          .reduce((a, b) => a.intersection(b))
          .toList();
      }
      
      final commonElements = intersection(lists);
      

      我想在 Richard Ambler 的回答下将此作为评论留下,但它失去了格式。

      【讨论】:

        【解决方案4】:

        如果你想使用嵌套循环。

         void main() {
          List l1 = [1, 2, 3, 55, 7, 99, 21];
          List l2 = [1, 4, 7, 65, 99, 20, 21];
          List l3 = [0, 2, 6, 7, 21, 99, 26];
        
          List result = [];
        
          for (final e1 in l1) {
            for (final e2 in l2) {
              for (final e3 in l3) {
                if (e1 == e2 && e1 == e3) {
                  result.add(e1);
                }
              }
            }
          }
        
          print(result);
        }
        

        【讨论】:

          【解决方案5】:

          或者你可以这样做:

            List l1 = [1, 2, 3, 55, 7, 99, 21];
            List l2 = [1, 4, 7, 65, 99, 20, 21];
            List l3 = [0, 2, 6, 7, 21, 99, 26];
            
            List uniqueItemsList = l1
              .toSet()
              .where((x) => l2.toSet().contains(x))
              .where((x) => l3.toSet().contains(x))
              .toList();
            
            print(uniqueItemsList); //[7, 99, 21]
          

          【讨论】:

            猜你喜欢
            • 2021-04-10
            • 1970-01-01
            • 1970-01-01
            • 2018-07-08
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2017-06-14
            相关资源
            最近更新 更多