C# 如何动态地将列表添加到列表中,然后将项目添加到列表列表中新添加的列表中?

C# 如何动态地将列表添加到列表中,然后将项目添加到列表列表中新添加的列表中?,c#,list,while-loop,nested-lists,C#,List,While Loop,Nested Lists,假设我有一个水果列表,并组织在一个列表中。该列表是有组织的,因此首先“person”一词会出现在列表上,并且该人之后的所有项目都是属于他们篮子中的水果。然后,列出的下一个人标记了一个解析时刻,为一个新人开始一个新的水果列表。最后,我想把所有这些人的水果清单汇编成一个清单。水果的数量和人数不详。然而,可以出现的水果类型是已知的 下面是一个示例列表,如果上面的内容没有意义: Person Apple Apple Cherry Apple Orange Person Grape Lemon Apple

假设我有一个水果列表,并组织在一个列表中。该列表是有组织的,因此首先“person”一词会出现在列表上,并且该人之后的所有项目都是属于他们篮子中的水果。然后,列出的下一个人标记了一个解析时刻,为一个新人开始一个新的水果列表。最后,我想把所有这些人的水果清单汇编成一个清单。水果的数量和人数不详。然而,可以出现的水果类型是已知的

下面是一个示例列表,如果上面的内容没有意义:

Person
Apple
Apple
Cherry
Apple
Orange
Person
Grape
Lemon
Apple
Apple
仅提供水果:苹果、樱桃、橙子、葡萄、柠檬

下面是我对代码的尝试,我在我认为应该添加列表的地方使用了注释,但我不确定语法应该是什么(这就是我想要帮助的):


我建议使用以下数据结构来表示您的数据:

Dictionary<String, List<String>> MyData = new Dictionary<String, List<String>>();
Dictionary MyData=newdictionary();

您可以这样做:

  static class Program
  {
    static IEnumerable<KeyValuePair<string, List<string>>> SliceBy(this IEnumerable<string> data, string delimiter)
    {
      string key = null;
      List<string> values = null;

      foreach (var item in data)
      {
        if (item == delimiter)
        {
          if (key != null)
          {
            yield return new KeyValuePair<string, List<string>>(key, values);
          }
          key = item;
          values = new List<string>();
        }
        else
        {
          values.Add(item);
        }
      }

      if (key != null)
        yield return new KeyValuePair<string, List<string>>(key, values);
    }

    static void Main(string[] args)
    {
      var personFruits = new[] { "Person", "Apple", "Apple", "Cherry", "Apple", "Orange", "Person", "Grape", "Lemon", "Apple", "Apple", "Person", "Grape", "Lemon", "Apple", "Apple" };
      var result = personFruits.SliceBy("Person");

      foreach (var person in result)
      {
        Console.WriteLine(person.Key);
        foreach (var fruit in person.Value)
        {
          Console.WriteLine(fruit);
        }

        Console.WriteLine();
      }

    }
  }
静态类程序
{
静态IEnumerable SliceBy(此IEnumerable数据,字符串分隔符)
{
字符串键=null;
列表值=空;
foreach(数据中的var项)
{
如果(项==分隔符)
{
if(key!=null)
{
返回新的KeyValuePair(键,值);
}
键=项目;
值=新列表();
}
其他的
{
增加(项目);
}
}
if(key!=null)
返回新的KeyValuePair(键,值);
}
静态void Main(字符串[]参数)
{
var personFruits=新[]{“人”、“苹果”、“苹果”、“樱桃”、“苹果”、“橙子”、“人”、“葡萄”、“柠檬”、“苹果”、“苹果”、“人”、“葡萄”、“柠檬”、“苹果”、“苹果”、“苹果”};
var结果=personFruits.SliceBy(“个人”);
foreach(结果中的var人员)
{
控制台。写线(人。键);
foreach(var fruit in person.Value)
{
控制台。书写线(水果);
}
Console.WriteLine();
}
}
}

例如,您知道您可以拥有一本
词典,但不容易看到您最终想要实现的目标。使用一个列表为多个人存储项目听起来不是个好主意。oh c#有字典吗??非常感谢有许多可能的数据结构可以帮助您。例如,您可以创建一个
Person
类,其中包含
字符串名
列表水果
。然后你可以有一个
列表
来保存这些人和他们的水果。@RezaAghaei这其实很聪明,但字典的想法看起来很酷,因为我以前从未在c#中做过。有可能编一本清单词典吗?我想不是。事实上,我可能会把清单放在我的课堂上。再次感谢你的建议。:)如果你想使用字典,@LeeTaylor提到的
字典
,正是你所需要的。关键点是人,每个关键点的值是一个
列表
,该列表为结果。
  static class Program
  {
    static IEnumerable<KeyValuePair<string, List<string>>> SliceBy(this IEnumerable<string> data, string delimiter)
    {
      string key = null;
      List<string> values = null;

      foreach (var item in data)
      {
        if (item == delimiter)
        {
          if (key != null)
          {
            yield return new KeyValuePair<string, List<string>>(key, values);
          }
          key = item;
          values = new List<string>();
        }
        else
        {
          values.Add(item);
        }
      }

      if (key != null)
        yield return new KeyValuePair<string, List<string>>(key, values);
    }

    static void Main(string[] args)
    {
      var personFruits = new[] { "Person", "Apple", "Apple", "Cherry", "Apple", "Orange", "Person", "Grape", "Lemon", "Apple", "Apple", "Person", "Grape", "Lemon", "Apple", "Apple" };
      var result = personFruits.SliceBy("Person");

      foreach (var person in result)
      {
        Console.WriteLine(person.Key);
        foreach (var fruit in person.Value)
        {
          Console.WriteLine(fruit);
        }

        Console.WriteLine();
      }

    }
  }