在C#中查找多个列表中共同项的最快方法

5

给定以下内容:

List<List<Option>> optionLists;

什么是快速确定出现在所有N个列表中的Option对象子集的方法?通过某些字符串属性(如option1.Value == option2.Value)来确定相等性。
因此,我们应该得到一个,其中每个项只出现一次。
11个回答

9

好的,这将找到在每个列表中出现值的选项对象列表。

var x = from list in optionLists
        from option in list
        where optionLists.All(l => l.Any(o => o.Value == option.Value))
        orderby option.Value
        select option;

它不执行“distinct”选择,因此将返回多个Option对象,其中一些具有相同的Value。


3

这里有一个更加高效的实现:

static SortedDictionary<T,bool>.KeyCollection FindCommon<T> (List<List<T>> items)
{
  SortedDictionary<T, bool>
    current_common = new SortedDictionary<T, bool> (),
    common = new SortedDictionary<T, bool> ();

  foreach (List<T> list in items)
  {
    if (current_common.Count == 0)
    {
      foreach (T item in list)
      {
        common [item] = true;
      }
    }
    else
    {
      foreach (T item in list)
      {
        if (current_common.ContainsKey(item))
          common[item] = true;
        else
          common[item] = false;
      }
    }

    if (common.Count == 0)
    {
      current_common.Clear ();
      break;
    }

    SortedDictionary<T, bool>
      swap = current_common;

    current_common = common;
    common = swap;
    common.Clear ();
  }

  return current_common.Keys;
}    

它的工作原理是创建一个包含已处理过的所有列表共有项目的集合,然后将每个列表与此集合进行比较,创建当前列表和迄今为止的共同项列表的临时集合。有效地实现了O(n.m)算法,其中n是列表的数量,m是列表中项目的数量。

以下是使用它的示例:

static void Main (string [] args)
{
  Random
    random = new Random();

  List<List<int>>
    items = new List<List<int>>();

  for (int i = 0 ; i < 10 ; ++i)
  {
    List<int>
      list = new List<int> ();

    items.Add (list);

    for (int j = 0 ; j < 100 ; ++j)
    {
      list.Add (random.Next (70));
    }
  }

  SortedDictionary<int, bool>.KeyCollection
    common = FindCommon (items);

  foreach (List<int> list in items)
  {
    list.Sort ();
  }

  for (int i = 0 ; i < 100 ; ++i)
  {
    for (int j = 0 ; j < 10 ; ++j)
    {
      System.Diagnostics.Trace.Write (String.Format ("{0,-4:D} ", items [j] [i]));
    }

    System.Diagnostics.Trace.WriteLine ("");
  }

  foreach (int item in common)
  {
    System.Diagnostics.Trace.WriteLine (String.Format ("{0,-4:D} ", item));
  }
}

这很好,我会将 return current_common.Keys; 替换为 return new List<T>(current_common.Keys);,这样对我的需求来说就完美了,代码如下:public static List<T> FindCommon<T>(params List<T>[] lists) - SSpoke
1
算了,结果这总是返回最后一个列表,我想不出如何在没有“SortedDictionary”的情况下使用它,只是普通的“Dictionary”,因为我不需要值本身改变顺序。 - SSpoke
@SSpoke 你是对的。它仅返回最后一个列表,而不是所有列表的交集。 - birdus

3

Matt的回答的基础上,由于我们只对所有列表都有的选项感兴趣,因此我们可以简单地检查其他列表与第一个列表共享的任何选项:

var sharedOptions =
    from option in optionLists.First( ).Distinct( )
    where optionLists.Skip( 1 ).All( l => l.Contains( option ) )
    select option;

如果选项列表不能包含重复条目,则Distinct调用是不必要的。如果列表的大小差异很大,最好迭代最短列表中的选项,而不是任何一个列表都可能是First。可以使用排序或哈希集合来改进Contains调用的查找时间,尽管对于适度数量的项目,这应该不会有太大的区别。


3

最快速的写法 :)

var subset = optionLists.Aggregate((x, y) => x.Intersect(y))

1

使用HashSet怎么样?这样你可以在O(n)的时间内完成,其中n是所有列表中项目的数量,我认为这是最快的方法。

你只需要遍历每个列表并将找到的值插入到HashSet中。当你插入一个已经存在的键时,.add方法会返回false,否则返回true


0
在搜索了互联网并没有找到我喜欢的(或者可行的)东西之后,我睡了一晚上,然后想出了这个方法。我的SearchResult类似于你的Option。它有一个EmployeeId,这是我需要在所有列表中共同的东西。我返回所有在每个列表中都有EmployeeId的记录。它不太花哨,但很简单易懂,正是我所喜欢的。对于小列表(我的情况),它应该表现得很好——任何人都可以理解!
private List<SearchResult> GetFinalSearchResults(IEnumerable<IEnumerable<SearchResult>> lists)
{
    Dictionary<int, SearchResult> oldList = new Dictionary<int, SearchResult>();
    Dictionary<int, SearchResult> newList = new Dictionary<int, SearchResult>();

    oldList = lists.First().ToDictionary(x => x.EmployeeId, x => x);

    foreach (List<SearchResult> list in lists.Skip(1))
    {
        foreach (SearchResult emp in list)
        {
            if (oldList.Keys.Contains(emp.EmployeeId))
            {
                newList.Add(emp.EmployeeId, emp);
            }
        }

        oldList = new Dictionary<int, SearchResult>(newList);
        newList.Clear();
    }

    return oldList.Values.ToList();
}

0
/// <summary>
    /// The method FindCommonItems, returns a list of all the COMMON ITEMS in the lists contained in the listOfLists.
    /// The method expects lists containing NO DUPLICATE ITEMS.
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="allSets"></param>
    /// <returns></returns>
    public static List<T> FindCommonItems<T>(IEnumerable<List<T>> allSets)
    {
        Dictionary<T, int> map = new Dictionary<T, int>();
        int listCount = 0; // Number of lists.
        foreach (IEnumerable<T> currentSet in allSets)
        {
            int itemsCount = currentSet.ToList().Count;
            HashSet<T> uniqueItems = new HashSet<T>();
            bool duplicateItemEncountered = false;
            listCount++;
            foreach (T item in currentSet)
            {
                if (!uniqueItems.Add(item))
                {
                    duplicateItemEncountered = true;
                }                        
                if (map.ContainsKey(item))
                {
                    map[item]++;
                } 
                else
                {
                    map.Add(item, 1);
                }
            }
            if (duplicateItemEncountered)
            {
                uniqueItems.Clear();
                List<T> duplicateItems = new List<T>();
                StringBuilder currentSetItems = new StringBuilder();
                List<T> currentSetAsList = new List<T>(currentSet);
                for (int i = 0; i < itemsCount; i++)
                {
                    T currentItem = currentSetAsList[i];
                    if (!uniqueItems.Add(currentItem))
                    {
                        duplicateItems.Add(currentItem);
                    }
                    currentSetItems.Append(currentItem);
                    if (i < itemsCount - 1)
                    {
                        currentSetItems.Append(", ");
                    }
                }
                StringBuilder duplicateItemsNamesEnumeration = new StringBuilder();
                int j = 0;
                foreach (T item in duplicateItems)
                {
                    duplicateItemsNamesEnumeration.Append(item.ToString());
                    if (j < uniqueItems.Count - 1)
                    {
                        duplicateItemsNamesEnumeration.Append(", ");
                    }
                }
                throw new Exception("The list " + currentSetItems.ToString() + " contains the following duplicate items: " + duplicateItemsNamesEnumeration.ToString());
            }
        }
        List<T> result= new List<T>();
        foreach (KeyValuePair<T, int> itemAndItsCount in map)
        {
            if (itemAndItsCount.Value == listCount) // Items whose occurrence count is equal to the number of lists are common to all the lists.
            {
                result.Add(itemAndItsCount.Key);
            }
        }

        return result;
    }

0
@Skizz 这个方法不正确。它返回了不是所有列表中都存在的项。 这是已经修正过的方法:
/// <summary>.
    /// The method FindAllCommonItemsInAllTheLists, returns a HashSet that contains all the common items in the lists contained in the listOfLists,
    /// regardless of the order of the items in the various lists.
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="listOfLists"></param>
    /// <returns></returns>
    public static HashSet<T> FindAllCommonItemsInAllTheLists<T>(List<List<T>> listOfLists)
    {
        if (listOfLists == null || listOfLists.Count == 0)
        {
            return null;
        }
        HashSet<T> currentCommon = new HashSet<T>();
        HashSet<T> common = new HashSet<T>();

        foreach (List<T> currentList in listOfLists)
        {
            if (currentCommon.Count == 0)
            {
                foreach (T item in currentList)
                {
                    common.Add(item);
                }
            }
            else
            {
                foreach (T item in currentList)
                {
                    if (currentCommon.Contains(item))
                    {
                        common.Add(item);
                    }
                }
            }
            if (common.Count == 0)
            {
                currentCommon.Clear();
                break;
            }
            currentCommon.Clear(); // Empty currentCommon for a new iteration.
            foreach (T item in common) /* Copy all the items contained in common to currentCommon. 
                                        *            currentCommon = common; 
                                        * does not work because thus currentCommon and common would point at the same object and 
                                        * the next statement: 
                                        *            common.Clear();
                                        * will also clear currentCommon.
                                        */
            {
                if (!currentCommon.Contains(item))
                {
                    currentCommon.Add(item);
                }
            }
            common.Clear();
        }

        return currentCommon;
    }

0

您可以通过计算所有列表中所有项的出现次数来实现此目标 - 那些出现次数等于列表数量的项是所有列表共有的:

    static List<T> FindCommon<T>(IEnumerable<List<T>> lists)
    {
        Dictionary<T, int> map = new Dictionary<T, int>();
        int listCount = 0; // number of lists

        foreach (IEnumerable<T> list in lists)
        {
            listCount++;
            foreach (T item in list)
            {
                // Item encountered, increment count
                int currCount;
                if (!map.TryGetValue(item, out currCount))
                    currCount = 0;

                currCount++;
                map[item] = currCount;
            }
        }

        List<T> result= new List<T>();
        foreach (KeyValuePair<T,int> kvp in map)
        {
            // Items whose occurrence count is equal to the number of lists are common to all the lists
            if (kvp.Value == listCount)
                result.Add(kvp.Key);
        }

        return result;
    }

这个失败了..我有3个不同的列表,只有1个共同值,但它说我有3个..共同值,因为2个列表共享了2个共同值,而另一个列表只共享了1个共同值,你不能仅仅把所有的值加起来然后检查有多少个列表,这样是行不通的..但我确实喜欢这段代码。 - SSpoke
@logicnp 我刚被引导到这篇文章,我检查了这个方法。它不能正确地工作(它返回不属于所有包含在列表中的IEnumerable-s的项目)。我冒昧改正了它,我将在这里发布它: - user2102327

0

先排序,然后类似于归并排序。

基本上你需要这样做:

  1. 从每个列表中检索第一个项目
  2. 比较项目,如果相等,则输出
  3. 如果任何项目在排序方面位于其他项目之前,则从相应的列表中检索新项目以替换它,否则从所有列表中检索新项目以替换它们
  4. 只要还有项目,就回到步骤2。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接