从数组或列表中获取最小值和最大值< keyvaluepair> [英] Getting min and max from an array or list <keyvaluepair>
问题描述
我有一个基本上是键/值对的多维数组。我想根据键求和所有值,然后从中选择最小值和最大值。
我尝试制作一个List< keyvaluepair>和一个字典做到这一点我非常接近,但它不会让我排序双打。我的列表和数组是正确创建的,没有问题。我想显示值的最小值/最大值,并在同一行显示键
public 静态 void PrintTable()
{
字符串文本;
var totals = new 列表< KeyValuePair< string,double>>();
for ( int i = 0 ; i < topics.Length; i ++)
{
Console.Write(topics [i] + );
double num = 0 ;
for ( int j = 0 ; j < 10 ; j ++)
{
Console.Write (respon [i,j] + );
num + = answers [i,j] *(j + 1 );
totals.Add( new KeyValuePair< string,double>(topics [i],num));
}
average = num / userCounter;
Console.Write( \t + average.ToString( ##。00));
}
// 显示最高点和最低点。
var addedSums = totals.GroupBy(k = > k.Key).ToDictionary(a = > a.Key,b = > b.Sum(c = > c.Value));
foreach ( var 对 addedSums.Values.OrderByDescending())
{
Console.WriteLine( 最高分: + pair);
}
我的尝试:
使用数组/列表/元组/ dictionarie / keyvaluepairs以及更多linq和许多其他方法来尝试这个我认为它会很简单
列表< KeyValuePair< string,double>> totals = new List< KeyValuePair< string,double>>()
{
new KeyValuePair< string,double>(Key1,10),
new KeyValuePair< string,double> (Key2,15),
new KeyValuePair< string,double>(Key3,5),
new KeyValuePair< string,double>(Key4,13)
} ;
double min = 0; double max = 0;
string maxKey =,minKey =;
foreach(KeyValuePair< string,double> kvp in totals)
{
if(max< kvp.Value)
{
maxKey = kvp.Key;
max = kvp.Value;
}
}
min = max;
foreach(KeyValuePair< string,double> kvp in totals)
{
if(min> kvp.Value)
{
minKey = kvp.Key;
min = kvp.Value;
}
}
textBox1.Text + = string.Format(maxKey:{0} max:{1} minkey:{2} min:{3} \ n,maxKey ,max,minKey,min);
**注意:我已将输出写入文本框而不是控制台
如果发现有用,请标记答案。
您好,
只需更新以下代码即可。在这里检查非零值。 [来自前一代码中的第18行]
列表< KeyValuePair< string,double>> totals = new List< KeyValuePair< string,double>>()
{
new KeyValuePair< string,double>(Key1,10),
new KeyValuePair< string,double>(Key3,0),
new KeyValuePair< string,double>(Key2,15),
new KeyValuePair< string,double>(Key4,13),
new KeyValuePair< string,double>(Key5,3)
};
double min = 0; double max = 0;
string maxKey =,minKey =;
foreach(KeyValuePair< string,double> kvp in totals)
{
//textBox1.Text + = string.Format(Key:{0} Value:{1} \ n ,kvp.Key,kvp.Value);
if(max< kvp.Value)
{
maxKey = kvp.Key;
max = kvp.Value;
}
}
min = max;
foreach(KeyValuePair< string,double> kvp in totals)
{
if(kvp.Value!= 0)//(kvp.Value> 0)
{
if(min> kvp.Value)
{
minKey = kvp.Key;
min = kvp.Value;
}
}
}
textBox1.Text + = string.Format(maxKey:{0} max:{1} minkey:{2} min:{3} \\ \\ n,maxKey,max,minKey,min);
>
**注意:如果发现有用,请标记为答案。
I have a multidimensional array with basically a key / value pair. I would like to sum all the values according to the key then pick the min and max values from this.
I tried making a List<keyvaluepair> and a dictionary to do this I got very close but it wouldn’t let me sort through a list of doubles. My lists and arrays are created correctly without issues. I would like to display the min/max of value and also display the key in the same line
public static void PrintTable()
{
string text;
var totals = new List<KeyValuePair<string, double>>();
for(int i = 0; i < topics.Length; i++)
{
Console.Write(topics[i] + " ");
double num = 0;
for(int j = 0; j < 10; j++)
{
Console.Write(responses[i, j] + " ");
num += responses[i, j] * (j + 1);
totals.Add(new KeyValuePair<string, double>( topics[i], num));
}
average = num / userCounter;
Console.Write("\t" + average.ToString("##.00"));
}
// Display highest points and lowest points.
var addedSums = totals.GroupBy(k => k.Key).ToDictionary(a => a.Key, b => b.Sum(c => c.Value));
foreach(var pair in addedSums.Values.OrderByDescending())
{
Console.WriteLine("Highest points: " + pair);
}
What I have tried:
using arrays/lists/tuples/dictionarie/keyvaluepairs and more linq and many other ways to try this i thought it would of been simple
List<KeyValuePair<string, double>> totals = new List<KeyValuePair<string, double>>() { new KeyValuePair<string, double>("Key1", 10), new KeyValuePair<string, double>("Key2", 15), new KeyValuePair<string, double>("Key3", 5), new KeyValuePair<string, double>("Key4", 13) }; double min = 0; double max = 0; string maxKey = "", minKey = ""; foreach (KeyValuePair<string, double> kvp in totals) { if (max < kvp.Value) { maxKey = kvp.Key; max = kvp.Value; } } min = max; foreach (KeyValuePair<string, double> kvp in totals) { if (min > kvp.Value) { minKey = kvp.Key; min = kvp.Value; } } textBox1.Text += string.Format("maxKey:{0} max: {1} minkey:{2} min: {3} \n", maxKey, max, minKey, min);
**Note: I have written output to textbox instead of Console
Mark answer if found useful.
Hi,
just update below code. cheking for non zero value here. [from line num 18 in previouse code]
List<KeyValuePair<string, double>> totals = new List<KeyValuePair<string, double>>() { new KeyValuePair<string, double>("Key1", 10), new KeyValuePair<string, double>("Key3", 0), new KeyValuePair<string, double>("Key2", 15), new KeyValuePair<string, double>("Key4", 13), new KeyValuePair<string, double>("Key5",3) }; double min = 0; double max = 0; string maxKey = "", minKey = ""; foreach (KeyValuePair<string, double> kvp in totals) { //textBox1.Text +=string.Format("Key: {0} Value: {1} \n", kvp.Key, kvp.Value); if (max < kvp.Value) { maxKey = kvp.Key; max = kvp.Value; } } min = max; foreach (KeyValuePair<string, double> kvp in totals) { if (kvp.Value != 0) //(kvp.Value > 0) { if (min > kvp.Value) { minKey = kvp.Key; min = kvp.Value; } } } textBox1.Text += string.Format("maxKey:{0} max: {1} minkey:{2} min: {3} \n", maxKey, max, minKey, min);>
**Note: Mark as Answer if found useful.
这篇关于从数组或列表中获取最小值和最大值< keyvaluepair>的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!