Which is equivalent to C # Python min / max

Which is equivalent to C # of the following Python min / max code:

pairs = [ (2,"dog"), (1, "cat"), (3, "dragon"), (1, "tiger") ] # Returns the PAIR (not the number) that minimizes on pair[0] min_pair = min(pairs, key=lambda pair:pair[0]) # this will return (1, 'cat'), NOT 1 

C # Enumerable.Min seems to be very close. But according to his MSDN document, it always returns the minimum VALUE value (and not the source object). Did I miss something?

EDIT


Pay attention . I'm not inclined to this, sorting first, since sorting (O (nlogn)) is computationally harder than finding a minimum (O (n)).

Also pay attention . The dictionary is also not suitable. It cannot handle cases when there are duplicate keys - (1, "cat") and (1, "tiger").

More importantly, the dictionary cannot handle cases where the elements being processed are a complex class. For example, finding a minimum over a list of animal objects using age as a key:

 class Animal { public string name; public int age; } 
+4
source share
4 answers

BCL doesn't have a MinBy function, but it's easy to write yourself.

 public static T MinBy<T, C>(this IEnumerable<T> items, Func<T, C> projection) where C : IComparable<C> { return items.Aggregate((acc, e) => projection(acc).CompareTo(projection(e)) <= 0 ? acc : e); } 

You can write a more complex MinBy than me to avoid re-evaluating the projection. Anyway, if you have a MinBy function, you can easily solve the problem:

 var pairs = new[] {Tuple.Create(2,"dog"), Tuple.Create(1, "cat"), Tuple.Create(3, "dragon"), Tuple.Create(1, "tiger")}; var min_pair = pairs.MinBy(e => e.Item1); 
+3
source

EDIT

 var minage = collection.Min( x => x.Age ); //for maxage replace Min by Max var minAgeAnimals = collection.where(x=> x.age == minage); foreach(Animal animal in minAgeAnimals ) Console.Writeline ( animal.Age.ToString() + " : " + animal.Name); 

Prev The answer to the editing question

Using a dictation object in C # and doing something like this does the same thing you want

 int minimumKey = touchDictionary.Keys.Min(); string value = ""; touchDictionary.TryGetValue(minimumKey, out value)) Console.Writeline ( "min key pair is:-" + minimumKey.ToString() + " : " + value); 

or

With linq, it will be easy for you

 var dictionary = new Dictionary<int, string> {{1, "one"}, {2, "two"}, {3, "three"}, {4, "four"} }; var maxKey = dictionary.Max(x => x.Key); var minkey = dictionary.Min(x => x.Key); 
0
source

Using

 Dictionary<int, string> pairs = new Dictionary<int, string>() { {2,"dog"}, {1, "cat"}, {3, "dragon"} }; var min = pairs.OrderBy(x => x.Key).FirstOrDefault(); 

OR

 int min = pairs.Keys.Min(); Dictionary<int, string> result = new Dictionary<int, string>() { {min, pairs[min]} }; 
0
source

I would use

 var min = pairs.OrderBy(x => x.FirstValue).FirstOrDefault(); 

and although I agree that sorting is harder than finding the minimum value, note that this is not sorting the entire set. It finds the first (or default) element in an ordered enumeration over a set that iterates lazily.

If you

 var min = pairs.OrderBy(x => x.FirstValue).ToList().FirstOrDefault(); 

then I would agree - you sort your pairs and then take the first one. But LINQ is smarter than that, and you won’t sort the set. You will transfer the first from a potentially ordered but not yet completed collection.


In addition to the fact that Dictionary cannot use a complex collection, say, an Animal list - how would you sort by Animal ? You can never sort a complex object. Instead, you need to use the age of the animal as a key. The dictionary can do this very easily - in fact, the key of the Dictionary will never be the same as the value of the dictionary, otherwise what's the point?

 var animals = new List<Animal>(); // get some animals... var animalictionary = animals.ToDictionary(a => a.Age); // assuming the animals have distinct ages, else var animalLookup = animals.ToLookup(a => a.Age); foreach (var animalGroup in animalLookup) { var age = animalGroup.Key; Console.WriteLine("All these animals are " + age); foreach (Animal animal in animalGroup) { Console.WriteLine(animal.name); } } 
0
source

Source: https://habr.com/ru/post/1414815/


All Articles