Добавление целочисленных значений из нескольких списков из словаря

1

Я пытаюсь работать над проектом в С# для своего курса по компьютерному программированию и застрял в части программирования.

То, что я хочу сделать, это добавить целые числа, хранящиеся в нескольких списках вместе, поместить их в новый список, а затем вывести среднее значение (со списком, хранящимся в словаре). Например, если бы у меня было:

List<int> list1 = new List<int>():  
List<int> list2 = new List<int>():

list1.Add(2);  
list1.Add(3);

list2.Add(1);  
list2.Add(7);

Я бы хотел, чтобы новый список имел сумму значений int в своих конкретных местах, поэтому в позиции 0 в новом списке значение равно 3, а в местоположении 1 - 10.

Или в той же теме моего кода, если Сьюзен набрала 89 очков в первом тесте и 50 на втором, а Джонни набрал 100 очков в своем первом тесте и 89 на втором, что такое средний класс для теста 1 и 2? (С Сьюзан и Джонни были ключи, и каждый из их тестов был элементом в своих списках)

Я не знаю, усложняет ли это что-то или нет, но у меня есть список, который хранится в словаре, с строкой в качестве ключа (поскольку это было проще организовать). Вот мой код класса:

List <int> testInfo;
Dictionary<string, List<int>> dict;
string name;
int testGrade;
int count = 0;
int count2 = 0;
int nameCount = 0;
int gradeCount = 0;

public StudentInfo() 
{

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

public void LoadGrades(string name, int testGrade) //adds the inputted values into a list
{
        this.name = name;
        this.testGrade = testGrade;
        testInfo = new List<int>();    

    if (dict.TryGetValue(name, out testInfo))
    {

        testInfo.Add(testGrade);
    }

    else
    {
        testInfo = new List<int>();  //creates a new list if a new key is entered
        testInfo.Add(testGrade);
        dict[name] = testInfo;
    }


}


public void DisplayGrades() //Displays the data entered so far
{
    Console.WriteLine("\nThis is the data you have entered so far.");
    Console.WriteLine("\n{0,-5} {1,20}", "Name", "Grade");
    Console.WriteLine("------------------------------");

    foreach (KeyValuePair<string, List<int>> pair in dict)
    {               
        foreach (int i in pair.Value)
        {
            Console.WriteLine("{0,-5} {1,20}%", pair.Key, i.ToString());
        }

    }
}

public void StuAvg() //Displays the average of each list for its specific key
{
    double average;
    Console.WriteLine("\nThese are the students averages.");

    Console.WriteLine("\n{0,-5} {1,20}", "Name", "Average");
    Console.WriteLine("------------------------------");

    foreach (KeyValuePair<string, List<int>> pair in dict)
    {
        average = Math.Round(pair.Value.Average(), 2);
        Console.WriteLine("{0,-5} {1,20}%", pair.Key, average.ToString());

    }



}

public void TestAvg() //Displays the average for each individual test
{
    List <int> testAvg = new List<int>();
    Console.WriteLine("\nThese are the averages for each test.");

    Console.WriteLine("\n{0,-5} {1,20}", "Test", "Average");
    Console.WriteLine("------------------------------");

        foreach (List<int> i in dict.Values)
        {

          //Adds values in each list together

        }
    }

И это моя основная программа:

StudentInfo s = new StudentInfo();
            string name;
            string anoStudent;
            int grade;
            int numTests;
            int count = 1;
            int count2 = 1;

            do
            {
                Console.Write("\nWhat is student {0}s name?: ", count++);
                name = Console.ReadLine();

                Console.Write("\nHow many tests did they take? (Maximum of 8): ");
                numTests = int.Parse(Console.ReadLine());

                while (numTests > 8 || numTests == 0)
                {
                    Console.WriteLine("\nA student can only take up to 8 tests, and has to take at least one to be registered.");
                    Console.Write("Please enter again: ");
                    numTests = int.Parse(Console.ReadLine());
                }


                for (int x = 0; x < numTests; x++)
                {
                    Console.Write("What was their mark for test {0}: ", count2++);
                    grade = int.Parse(Console.ReadLine());

                    while (grade > 100 || grade < 0)
                    {
                        Console.WriteLine("\nA student can't have more than 100 percent or less than 0 percent on any given test.");
                        Console.Write("Please enter again: ");
                        grade = int.Parse(Console.ReadLine());

                    }

                    s.LoadGrades(name, grade); //load input into function

                }


                Console.Write("\nWould you like to add another student? (Yes or No): ");
                anoStudent = Console.ReadLine();

                numTests = 0; //reset variables
                count2 = 1;

            } while (anoStudent.ToLower() == "yes");

            s.DisplayGrades();
            s.StuAvg();
            s.TestAvg();
            Console.ReadKey(true);

Извиняюсь за длину, но любая помощь будет очень признательна :)

РЕДАКТИРОВАТЬ
Я пересмотрел предложенный Николасом метод non-LINQ, который позволит ему вычислять средние значения из нескольких списков, обеспечивая, таким образом, усреднение только местоположений списка, которые были добавлены вместе, так что неизмененные местоположения не затрагиваются. Я также изменил исходные целые списки на двойные списки для точности.

Я думал, вы, ребята, захотите увидеть готовый код, так что здесь функция для него :)

 public void TestAvg() //Calculate the class average for each test
{
    List<int> counter = new List<int>(); //A list to hold the amount of students that took each test
    List<double> results = new List<double>(); //Create a new list to hold the final test averages
    double testAvg;

    Console.WriteLine("\nThis is the class average for each test.");

    Console.WriteLine("\n{0,-0} {1,20}", "Test", "Average");
    Console.WriteLine("--------------------------------------------------");

        foreach (List<double> l in dict.Values) //For every list in dict.Values
        {

            count2++; //Create a counter 

            for (int x = 0; x < l.Count(); x++ ) //Until x reaches the amount of elemets in l (list)
            {
                double value = l[x];   //Create a variable that will hold the value of l at location x                 

                if (x < results.Count) //if x is less than the amount of elements in results
                {
                    results[x] += value; //the value at x is equal to the value at x plus the variable value
                    counter[x] += 1;               
                }

                else
                {
                    results.Add(value); //Otherwise value is instead just added to the list, instead of being sumemd into the previous locations       
                    counter.Add(1);   
                }
            } 
        }

        for (int i = 0; i < results.Count; i++)
        {
            results[i] = results[i] / counter[i]; //Take the values at x in results and divide it by the amount of students that took each test (find the average)    
        }     

    foreach (double d in results) //For every double value in the list
    {                           
                testAvg = Math.Round(d, 2); //Round it to 2 decimal places
                Console.WriteLine("{0,-0} {1,20}%", count++ + 1, testAvg); //Output results                                                                             
    }          
}
  • 2
    Это совсем не плохой вопрос для вашего первого обращения к SO, но очень важно, чтобы вы объяснили нам, в какой момент вы застряли. What I want to do is add integers stored in multiple lists together, put that into a new list, then output the average (with the lists being stored in a dictionary). Какую часть этого вы не можете сделать? Разбейте его, вы можете понять это сами!
  • 0
    Какой именно вопрос? Я надеюсь, вы не ищете кого-то, чтобы сделать ваш проект.
Показать ещё 1 комментарий
Теги:
list
dictionary

2 ответа

2
Лучший ответ

Если у вас есть весь список, добавленный в ваш словарь, вы можете сделать следующее:

var query = dict.Values.Select(r => r.Select((number, i) => new { Index = i, Number = number }))
    .SelectMany(r => r)
    .GroupBy(r => r.Index)
    .Select(grp => new
    {
        Index = grp.Key,
        Value = grp.Sum(t => t.Number)

    });

Предположим, у вас есть списки вроде:

List<int> list1 = new List<int>();
List<int> list2 = new List<int>();

list1.Add(2);
list1.Add(3);

list2.Add(1);
list2.Add(7);
Dictionary<string, List<int>> dict = new Dictionary<string, List<int>>();
dict.Add("1", list1);
dict.Add("2", list2);

то после выполнения запроса LINQ (первый фрагмент кода) вы можете сделать:

foreach (var item in query)
{
    Console.WriteLine("Index: {0}, Sum: {1}", item.Index, item.Value);
}

и вы получите:

Index: 0, Sum: 3
Index: 1, Sum: 10
  • 1
    +1. Работает, а не копирует / вставляет :)
  • 0
    Вау спасибо! Это было быстрее, чем я, спасибо :) А это Линк, не так ли? Я еще не слишком знаком с этим, не слишком ли трудно для вас объяснить это мне? Я бы предпочел понять, что я кодирую ^^
Показать ещё 1 комментарий
1

Здесь простой метод не LINQ для суммирования любого количества списков в том виде, как вы описываете:

static List<int> SumOfLists( params List<int>[] lists )
{
  return SumOfLists( (IEnumerable<List<int>>) lists ) ;
}

static List<int> SumOfLists( IEnumerable<List<int>> lists )
{
  List<int> result = new List<int>() ;

  foreach ( List<int> list in lists )
  {

    for( int j = 0 ; j < list.Count ; ++j )
    {
      int value = list[j] ;

      if ( j < result.Count )
      {
        result[j] += value ;
      }
      else
      {
        result.Add( value ) ;
      }

    } // end for-j

  } // end for-each

  return result ;
}
  • 0
    Большое спасибо, я могу понять этот синтаксис намного проще :)

Ещё вопросы

Сообщество Overcoder
Наверх
Меню