【问题标题】:creating a sublist of a list under some condition C#在某些条件下创建列表的子列表 C#
【发布时间】:2015-12-08 18:02:08
【问题描述】:

我想在某些条件下创建列表的子列表,但不知道如何。这是一个例子:

假设我们有从 1 到 5 的数字,每个数字都有一个子数组/数组。

1: 1 5 7 5 5 3 4 9
2: 0 1 2 3 4 6 3 4
3: 9 4 6 7 0 0 3 1
4: 4 6 3 7 8 0 0 1
5: 8 0 3 1 0 2 4 6

: 之后的数字我将保存在一个数组中以便快速访问。

现在我想首先创建一个大小为 5(数字 1 到 5)的列表,并在此条件下为每个数字创建一个子列表:

if(list[i] > (arr1[j] + 1))
{
   //then save it in a sublist of the index i
}

我想要的输出是这样的:

List
    [1]
       [5]
       [7]
       [5]
       [5]
       [2]
       [4]
       [9]
    [2]
       [4]
       [6]
       [4]
    .
    .
    .
    [5]
       [8]

我可以创建第一个列表

List<int> List1 = new List<int>();
for (int i = 0; i < 5; i++)
{
    List1.Add(i);
}

但是我怎样才能创建子列表呢?

更新:我试过了

List<Tuple <int,int>> List1 = new List<Tuple <int,int>>();

但这无济于事。

【问题讨论】:

  • “我可以通过...创建第一个列表” 不,因为 List1.Add(j) 无法编译。循环变量是i 而不是j。这个问题不是很清楚,至少对我来说不是。
  • @TimSchmelter 我更正了。这是索引中的错字;)
  • 我认为他想过滤掉小于它们所在索引的数字
  • 为什么第二行的答案是4 6 4,而不是3 4 6 3 4
  • @Saber:那么为什么最后一行8 6而不仅仅是8(5 + 1 == 6)6 不应该出现

标签: c# list


【解决方案1】:

你可以试试Linq

  List<int[]> source = new List<int[]>() {
    new int[] { 1, 5, 7, 5, 5, 3, 4, 9},
    new int[] {0, 1, 2, 3, 4, 6, 3, 4},
    new int[] {9, 4, 6, 7, 0, 0, 3, 1},
    new int[] {4, 6, 3, 7, 8, 0, 0, 1},
    new int[] {8, 0, 3, 1, 0, 2, 4, 6},
  };

  var result = source
    .Select((array, index) => array
      .Where(item => item > index + 2) // +2 since index is zero-based
      .ToArray()); // ToArray is not necessary here, but convenient for further work

  // Test

  String report = String.Join(Environment.NewLine, 
    result.Select(item => String.Join(", ", item)));

  Console.Write(report);

输出是

   5, 7, 5, 5, 3, 4, 9
   4, 6, 4
   9, 6, 7
   6, 7, 8
   8

编辑:对于任意索引号,我建议使用dictionary键作为索引:

  Dictionary<int, int[]> source = new Dictionary<int, int[]>() {
    {1, new int[] { 1, 5, 7, 5, 5, 3, 4, 9}},
    {2, new int[] { 0, 1, 2, 3, 4, 6, 3, 4}},
    {3, new int[] { 9, 4, 6, 7, 0, 0, 3, 1}},
    {4, new int[] { 4, 6, 3, 7, 8, 0, 0, 1}},
    {5, new int[] { 8, 0, 3, 1, 0, 2, 4, 6}},
  };

  var result = source
    .Select(pair => pair.Value
       .Where(item => item > pair.Key + 1)
       .ToArray());

【讨论】:

  • 索引号不按顺序怎么办?
  • @csharpwinphonexam:如果索引号实际上是 任意 数字,那么我建议使用 Dictionary&lt;int, int[]&gt; 其中 keyindex
  • 这就是我一直在想的 - List>> 对这个要求来说似乎太复杂了。
【解决方案2】:

试试这个

List<Tuple <int,List<int>>> list = new List<Tuple <int,List<int>>>();

//populate the list 

if(list[i].Item1 > (arr1[j] + 1))
{
    list[i].Item2.Add(arr1[j]); 
}

据我了解,输入是这样的

1: 1 5 7 5 5 3 4 9
2: 0 1 2 3 4 6 3 4
3: 9 4 6 7 0 0 3 1
4: 4 6 3 7 8 0 0 1
5: 8 0 3 1 0 2 4 6

输出应该是这样的:

1: 5 7 5 5 3 4 9
2: 4 6 4
3: 9 6 7
4: 6 7 8
5: 8

所以基于索引小于或等于索引的项目应该被忽略

【讨论】:

  • 你能告诉我 OP 正在尝试什么以及为什么它不起作用吗?
  • 他正在尝试将列表添加到特定索引
  • @TimSchmelter 更新了答案,以便您更好地理解问题
  • if 条件发生变化然后我有与List 无关的完全不同的条件时是否有效?我的意思是让我在If 中添加其他内容,然后我只使用list[i].Item2.Add(**);
  • 我不明白你想做什么
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-08-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多