【问题标题】:Flutter/Dart Compare multiple Lists and generate list with elements containd in all given listsFlutter/Dart 比较多个列表并生成包含所有给定列表中的元素的列表
【发布时间】:2021-01-23 14:09:39
【问题描述】:

所以这是我的问题:
假设我们有两个或更多列表

List<SomeModel> one = [SomeModel, SomeModel, SomeModel],
List<SomeModel> two = [SomeModel, SomeModel, SomeModel, SomeModel, SomeModel, SomeModel]

我想要做的是比较这两个/或更多列表并创建一个新列表
仅包含每个列表中存在的那些元素。

List<Fruits> one = [Banana, Apple, Cherry];
List<Fruits> two = [Cherry, Blueberry, Apple, Mango, Pineapple, Pear];
List<Fruits> three = [Cherry, Apple, Mango, Pineapple, Pear, Banana];

List<Fruits> getMatchingList(){
  do something...

  List<Fruits> matchingFruits = [Cherry, Apple];
  return matchingFruits
}

我真的不知道如何做到这一点,尤其是对于较大的列表或未知内容的列表。

【问题讨论】:

    标签: flutter dart


    【解决方案1】:

    如果列表的数量是固定的,你可以使用 where 函数来过滤项目:

    List<SomeModel> filteredList = one.where(item => two.contains(item))
         .where(item => three.contains(item)).toList();
    

    所以你的方法是:

    List<Fruits> getMatchingList(List<Fruits> one, List<Fruits> two, List<Fruits> three){
       return one.where(item => two.contains(item))
                .where(item => three.contains(item))
                .toList();
    }
    

    如果您需要处理越来越多的列表,另一方面,只需使用:

    List<Fruits> getMatchingList(List<List<Fruits>> listsToAnalyze){
       Map<Fruits,integer> fruitCounter = new Map();
       //expand is a method that flattern a list of lists in a single list
       listsToAnalyze.expand(element => element).toList().forEach(element => {
        //if the element exists, its counter is increased, otherwise is set to 1
        fruitCounter.update(element, (count) => count+1, ()=> 1);
       }
       //remove results with counter < size of the list of lists
       //supposing that an item can appear only once in each list, this means
       //that the value appeared in all lists
       fruitCounter.removeWhere((key,value)=> value<listsToAnalyze.lenght);
    
       return fruitCounter.keys.toList();
       
    }
    

    如果一个项目可以在一个列表中出现多次,请不要展开列表并保留List&lt;Fruit&gt; 以控制在每个循环期间是否已在列表中插入项目。记得在每个新列表中清空地图。

    List<Fruits> getMatchingList(List<List<Fruits>> listsToAnalyze){
           Map<Fruits,integer> fruitCounter = new Map();
           List<Fruits> fruitsInsertedThisRound= new List();
           //cycle on each list alone
           listsToAnalize.forEach(list => {
               fruitsInsertedThisRound.clear(); //ensure that the list is empty at each cycle
               list.forEach(el => {
               //if the element exists, its counter is increased, otherwise is set to 1
                if(!fruitsInsertedThisRound.contains(el){
                  //fruit still not inserted this round
                  fruitCounter.update(el, (count) => count+1, ()=> 1);
                  fruitsInsertedThisRound.add(el); //add the element to the list of inserted fruits
                }
             }
           }
    
           //remove results with counter < size of the list of lists
           //this means that the value appeared in all lists
           fruitCounter.removeWhere((key,value)=> value<listsToAnalyze.lenght);
    
           return fruitCounter.keys.toList();
           
        }
    

    【讨论】:

      【解决方案2】:

      通过删除重复合并两个列表

      作为单行

      a.addAll(b.takeWhile((item)=> !a.contains(item)));
      

      通过循环

      for(var item in a){
       if(!b.contains(item)){
        b.add(item);
       }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-27
        • 1970-01-01
        • 2017-03-12
        • 1970-01-01
        • 2020-11-14
        • 2016-11-17
        相关资源
        最近更新 更多