2015-11-05 32 views
0

我有一个命名为dict_id_names词典:如果值在列表形式中,如何从字典中获取键?

Dictionary<int,List<string>> dict_id_names = new Dictionary<int,List<string>(); 

假设字典有3键 - 值对:

  • id = 1:列表包含名 “罗宾”, “拉胡”, “亚当”, “阿赫塔尔”,

  • id = 2:列表包含名 “太阳报”, “星期一”, “亚当”,

  • id = 3:列表中包含名称“A”,“B”,“C”

现在我的问题是,如果我只有名字“亚当”那我怎么才能得到相应的键/键为1在上面的例子中有2个来自字典?

+1

是否需要将反向查找会是〜O(1)像正常字典?请注意,以下所有答案都涉及字典的全面扫描。 –

回答

5

你可以使用LINQ:

var keyValsWithAdamValue = dict_id_names.Where(kv => kv.Value.Contains("Adam")); 

foreach(var kv in keyValsWithAdamValue) 
    Console.WriteLine("{0}|{1}", kv.Key, String.Join(",", kv.Value)); 

如果你只是想在ID是你可以选择它们,并使用ToList/ToArray创建集合:

List<int> idsWithAdamInList = dict_id_names 
    .Where(kv => kv.Value.Contains("Adam")) 
    .Select(kv => kv.Key) 
    .ToList(); 

注意,这种方法是像在字典上循环。如果您正在枚举字典,则不会从字典的快速查找性能中受益。它不是为此目的而设计的。但是如果性能在这种情况下并不那么重要,那么它就是简单易读的代码并且非常完美。

1
string name = "Adam"; 
foreach(int key in dict_id_names.Keys) 
{ 
    List<string> valueList = dict_id_names[key]; 

    if(valueList.Contains(name); 
     Console.WriteLine(id); 
} 

这应该有所帮助。

+0

是的,我以前试过,但有没有其他的方式或方法,没有必要全面扫描字典? –

3

您可以使用下面的LINQ查询:

int[] ids = dict_id_names 
        .Where(pair => pair.Value.Contains("Adam")) 
        .Select(pair => pair.Key) 
        .ToArray(); 

Console.WriteLine(String.Join(',', ids)); // 1,2 

这将导致阵列[1, 2],因为这两种词典条目在它的字符串列表包含Adam

0

喜欢的东西 -

var dict_id_names= new Dictionary<int,List<string>>(); 
dict_id_names.Add(1, new List<string> { "Robin", "Rahul", "Adam", "Akhtar" }); 
var id = dict_id_names.Where(a => a.Value.Contains("Adam")).FirstOrDefault().Key; 
0

我只是想提供一个不同的角度,以进行比较。

假设您经常进行反向查找,并且希望它比O(N)操作更好。

你可以使用两个字典而不是一个来实现。为了简化示例,我将使用微软的预发布版MultiValueDictionary(您可以通过NuGet获取)。

这种方法产生的O(1)查找:

using System; 
using System.Collections.Generic; 
using System.Linq; 

namespace Demo 
{ 
    internal class Program 
    { 
     public static void Main() 
     { 
      var names = new Names(); 

      names.Add(1, "Robin"); 
      names.Add(1, "Rahul"); 
      names.Add(1, "Adam"); 
      names.Add(1, "Akhtar"); 

      names.Add(2, "Sun"); 
      names.Add(2, "Mon"); 
      names.Add(2, "Adam"); 

      names.Add(3, "a"); 
      names.Add(3, "a"); 
      names.Add(3, "c"); 

      Console.WriteLine("IDs for Adam:"); 

      foreach (int id in names.IdsOf("Adam")) 
       Console.WriteLine(id); 
     } 

     public sealed class Names 
     { 
      readonly MultiValueDictionary<int, string> names = new MultiValueDictionary<int, string>(); 
      readonly MultiValueDictionary<string, int> lookup = new MultiValueDictionary<string, int>(); 

      public void Add(int id, string name) 
      { 
       names.Add(id, name); 
       lookup.Add(name, id); 
      } 

      public IEnumerable<int> IdsOf(string name) 
      { 
       IReadOnlyCollection<int> result; 

       if (lookup.TryGetValue(name, out result)) 
        return result; 
       else 
        return Enumerable.Empty<int>(); 
      } 

      public IEnumerable<string> NamesOf(int id) 
      { 
       IReadOnlyCollection<string> result; 

       if (names.TryGetValue(id, out result)) 
        return result; 
       else 
        return Enumerable.Empty<string>(); 
      } 
     } 
    } 
} 
相关问题