[英]to get a KeyValuePair from a List of KeyvaluePairs with the minimum value
I need to get a Kvp from a list of List<KeyValuePair<Int, Int>>
depending on the minimum value. 我需要从List<KeyValuePair<Int, Int>>
的列表中获取Kvp List<KeyValuePair<Int, Int>>
具体取决于最小值。
I have tried this: 我试过这个:
KeyValuePair<Int, Int> kvp= listOfKvps.Min(e=> e.Key);
but this return only the value, not the whole KeyValuePair
which I need. 但这只返回值,而不是我需要的整个KeyValuePair
。
var min = listOfKvps.OrderBy(kvp => kvp.Key).First();
If you want to do it with a single O(n) pass through the sequence, rather than requiring an O(n log n) ordering, then you could do it like this: 如果你想通过序列中的单个O(n)传递,而不是要求O(n log n)排序,那么你可以这样做:
var min = listOfKvps.Aggregate((agg, kvp) => (kvp.Key < agg.Key) ? kvp : agg);
(Of course, the second version is much less readable/intuitive than the first, even if it does have better theoretical performance. It would make more sense to use some sort of MinBy
method: either write your own, use the version from Marc's answer or use the version from MoreLINQ .) (当然,第二个版本的可读性/直觉性远远低于第一个版本,即使它确实具有更好的理论性能。使用某种MinBy
方法更有意义:要么自己编写,请使用Marc的答案版本或使用MoreLINQ中的版本 。)
There is no inbuilt MinBy
method, so you could either write a MinBy
extension method, or just .OrderBy(x => x.Key).First()
. 有没有内在的MinBy
方法,所以你既可以写一个MinBy
扩展方法,或者只是.OrderBy(x => x.Key).First()
A MinBy
would be O(n)
so would be more efficient - but more code to write ;p MinBy
将是O(n)
因此效率更高 - 但要编写更多代码; p
For example, you could use: 例如,您可以使用:
var kvp= listOfKvps.MinBy(e=> e.Key);
with: 有:
public static class SomeUtil {
public static TSource MinBy<TSource, TValue>(
this IEnumerable<TSource> source, Func<TSource, TValue> selector) {
using (var iter = source.GetEnumerator())
{
if (!iter.MoveNext()) throw new InvalidOperationException("no data");
var comparer = Comparer<TValue>.Default;
var minItem = iter.Current;
var minValue = selector(minItem);
while (iter.MoveNext())
{
var item = iter.Current;
var value = selector(item);
if (comparer.Compare(minValue, value) > 0)
{
minItem = item;
minValue = value;
}
}
return minItem;
}
}
}
I would suggest you use the MinBy
extension-method from MoreLinq . 我会建议你使用MinBy
从扩展法MoreLinq 。
Alternatively: 或者:
var minKey = listOfKvps.Min(kvp => kvp.Key);
var minKvp = listOfKvps.First(kvp => kvp.Key == minKey);
This is still O(n)
, although it requires 2 passes over the list. 这仍然是O(n)
,虽然它需要2次通过列表。 Sorting the list and then picking the first element is more terse, but is O(n * logn)
, which may be relevant for larger lists. 对列表进行排序然后选择第一个元素更简洁,但是O(n * logn)
,这可能与更大的列表相关。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.