C# Numphy and nested lists

Numphy and nested lists :slight_smile:

I found the following about nested lists but I think it has to be simpler. Also, what is Numphy under :thinking: do you might know :thinking:

question10.gh (2.3 KB)

List<List<int>>
This looks weird :thinking:

List<List<List<List<int>>>>
More weird :thinking:

Should you than go for dictonary :thinking:

Can it not be something like:
List<int> numbersA = new List<int>{{0,1,2},{0,1,2},{0,1,2}}

It does not work :thinking:

I still try to find a numphy equivalent of python for C#.

It does look weird, but that’s the way it works in .NET. Alternate ways to approach this would be to use arrays int[][][] or create you own type which can both store collections of values or collections of itself.

Let’s see if I can come up with something while typing on this tablet.

public sealed class NestedList<T>
{
  private readonly List<T> _items;
  private readonly List<NestedList<T>> _lists;

  public NestedList(params T[] items)
  {
    _items = new List<T>(items);
  }
  public NestedList(params NestedList<T> lists)
  {
    _lists = new List<NestedList<T>>(lists);
  }

  public bool ContainsItems { get { return _items != null; } }
  public bool ContainsLists { get { return _lists != null; } }

  public void Add(T item) { _items.Add(item); }
  public void Add(NestedList<T> list) { _lists.Add(list); }

  public int ItemCount { 
    get 
    {
      if (_items == null) return 0;
      return _items.Count;
    } 
  }
  public int ListCount { 
    get {
      if (_lists == null) return 0;
      return _lists.Count;
    } 
  }

  public T ItemAt(int index) { return _items[index]; }
  public NestedList<T> ListAt(int index) { return _lists[index]; }

  // and so on and so forth. 
}
1 Like

Found this.

Found this.

It seems what I am doing is trying to make a new class similar to the class DataTree. But I do not know if the class DataTree is the conventional way to work with date within C#. Also, can you make branch-depths like {0;0;0;0;0;0} :thinking:

However, to continue learning all aspects of C#, what I am doing wrong it the code you sent me :thinking:
Small array above in the code is unrelated to that of the code below.

question10a.gh (4.2 KB)

DataTree<T> is a Grasshopper class. It is not something you will find in C# elsewhere.

Furthermore DataTree<T> is a simplified version of the GH_Structure<T> class which is used internally in Grasshopper to store all data. The problem with GH_Structure<T> is that it demands T implements the IGH_Goo interface. This was an unwelcome constraint within the context of C# and VB script components.

You can certainly use it though, and the paths used to identify the various lists in the tree can be set up to mimic a nesting structure.

1 Like

Thank you David. By the way, my previous post, do you see why I get that message of line 79 (image)?

In case this helps…

  private void RunScript(object x, object y, ref object A)
  {

    List<List<int>> numbers = new List<List<int>>{new List<int>(){0,1,2},new List<int>(){0,1,2},new List<int>(){0,1,2}};
    
    List<List<int>> numbers2 = ListMe(ListMe(0, 1, 2), ListMe(0, 1, 2), ListMe(0, 1, 2)); 
    
    int[][] numbers3 = new int[][]{new []{0, 1, 2}, new []{0, 1, 2}, new []{0, 1, 2}};
  
  }

  // <Custom additional code> 

  public List<T> ListMe<T>(params T[] items)
  {
     var list = new List<T>();
     list.AddRange(items);
     return list;
  }

Ah, that’s supposed to be

public NestedList(params NestedList<T>[] lists)

I forgot the array brackets for the argument, they are required when your function argument has the params qualifier.

1 Like

C# has multidimensional array, which is different from nested array.

int[,] arr=new int[3,2]{{1,2,3},{1,2,3}};

Perhaps this is similar to numpy something.