【问题标题】:c# dictionary return multiple min maxc# 字典返回多个最小值最大值
【发布时间】:2016-04-14 07:53:03
【问题描述】:

如果字典中有超过 1 个,我如何从字典中获取超过 1 个的最小值/最大值?我知道您必须转换 .ToList 才能使用 min/max,但是当我这样做时,它只会给我它遇到的第一个满足 min/max 要求的值。

class Program
{
    Dictionary<string, int> myDictionary = new Dictionary<string, int>();

    static void Main(string[] args)
    {
        Program minMaxAge = new Program();
        minMaxAge.MinMaxAge();

        Console.ReadLine();
    }
    public Program()
    {            
        myDictionary.Add("Darius", 35);
        myDictionary.Add("Caitlin", 25);
        myDictionary.Add("Xin", 55);
        myDictionary.Add("Alistar", 25);
    }
    public void MinMaxAge()
    {
        // Have to convert to list or array in order to get min/max
        var ageRange = myDictionary.ToList(); 
        // Created easier to read Keys and Values       
        var minAge = ageRange.Min(myDictionary => myDictionary.Value);            
        var minName = myDictionary.FirstOrDefault(x => x.Value == minAge).Key;

        var maxAge = ageRange.Max(myDictionary => myDictionary.Value);
        var maxName = myDictionary.FirstOrDefault(x => x.Value == maxAge).Key;

        Console.WriteLine("The youngest age is {0} and that is {1}.", minAge, minName);
        Console.WriteLine("The youngest age is {0} and that is {1}.", maxAge, maxName);
    }
}

【问题讨论】:

  • int 的形式获取最小年龄,并使用它从字典中进行选择。重复最大年龄。

标签: c# list dictionary max min


【解决方案1】:

由于您想获取与max 值匹配的所有项目,您可以使用Where 而不是FirstOrDefault

var minAge = ageRange.Min(myDictionary => myDictionary.Value);            
var minNames = myDictionary.Where(x => x.Value == minAge).Select(p => p.Key);

现在您可以像这样打印所有名称

foreach (string name in minNames) {
    Console.WriteLine(name);
}

或者像这样构造一个包含所有名称的string

string allMinNames = string.Join(" ", minNames);

【讨论】:

  • 谢谢,这正是我想要的。
【解决方案2】:

尝试关注dictionary.Where(e =&gt; e.Value == maxAge).Select(e =&gt; e.key)

【讨论】:

    【解决方案3】:

    您可以通过对用于 MinMaxAge 方法中的名称的 LINQ 指令进行一些小的更改来获得所需的内容。看看这个:

    public void MinMaxAge()
    {
        // Have to convert to list or array in order to get min/max
        var ageRange = myDictionary.ToList();
    
        // Created easier to read Keys and Values       
        var minAge = ageRange.Min(myDictionary => myDictionary.Value);
        var minNames = myDictionary.Where(x => x.Value == minAge)
            .Select(x => x.Key)
            .Aggregate((current, next) => current + ", " + next);
    
        var maxAge = ageRange.Max(myDictionary => myDictionary.Value);
        var maxNames = myDictionary.Where(x => x.Value == maxAge)
            .Select(x => x.Key)
            .Aggregate((current, next) => current + ", " + next);
    
        Console.WriteLine("The youngest age is {0} and that is {1}.", minAge, minNames);
        Console.WriteLine("The youngest age is {0} and that is {1}.", maxAge, maxNames);
    }
    

    问候。

    【讨论】:

      【解决方案4】:

      您的问题的另一种解决方案如下:

      var max = myDictionary.Where(s => s.Value == myDictionary.Max(kvp => kvp.Value));
      var min = myDictionary.Where(s => s.Value == myDictionary.Min(kvp => kvp.Value));
      

      这样您就不需要将您的字典转换为列表,并且还消除了从其他发布的解决方案中将最大和最小年龄保存在自己的变量中的额外步骤。

      【讨论】:

      • 这是一个非常昂贵的解决方案,集合中的每个项目都会调用一次聚合,从而为您提供 n^2 时间复杂度
      猜你喜欢
      • 2016-05-19
      • 1970-01-01
      • 2019-04-22
      • 2019-03-20
      • 1970-01-01
      • 2023-03-29
      • 2012-07-21
      • 1970-01-01
      • 2016-05-19
      相关资源
      最近更新 更多