C# Array.IndexOfFindAll

(1) I am trying to replace items values, without a loop only with a python in function, of an array.

(2) I am trying to get, without a loop only with a predicate, all items containing a certain value.

What do I need to change to do (1) and (2) (probably everything) :thinking:

question9.gh (5.0 KB)

Can you explain better what you want to do?
Only one value in your lists is equal to 1 or 2 (there is a single “2”).
How should the replacing work?

Also, you are in c# context, not python.
In c# there is this:

List[int] ints;
foreach(int i in ints){
Print(i.ToString())
}
1 Like

An array of 100 times 0.
An list x of certain item numbers
An list y of certain item numbers.

(1)
I want to replace all array items with 1 if the item is in list x, with something similar like ‘in’ as with python.
I want to replace all array items with 2 if the item is in list y, with something similar like ‘in’ as with python.

(2)
I want to make a new array containing all item numbers of the array when the value is 2.

Did I explain it better :thinking:

… what? :sweat_smile:

Sorta.

Try this:

using System.Linq;

  private void RunScript(List<int> x, List<int> y, ref object A)
  {
    List<int> ints = Enumerable.Repeat(0, 100).ToList();
    List<int> ints_copy = new List<int>(ints);
    for(int i = 0;i < ints.Count();i++){
      if(x.Contains(ints[i])) ints_copy[i] = 1;
      else if(y.Contains(ints[i])) ints_copy[i] = 2;
    }
    A = ints_copy;
  }

question9.gh (6.1 KB)

1 Like

Do you also know about an Array.IndexOfFindAll
by which I mean,

So I now have an array with 1’s and 2’s in it but mostly 0’s, I now want all indices of the array which hold an 2.

When doing Array.IndexOf it only gives me the first index number with 2. But I want all indices with 2.

I am trying to find a way doing that without a loop. Is that possible?
Or doing it with an predicate x=> x==2 give index number; ToArray() or ToList().

I think this? “in” in Python is Contains() in C#. there is no as cool as “in” equivalent.

List < int> X = new List<int>(){4,7};
List < int> Y = new List<int>(){2,3};
var vals = from val in Enumerable.Range(0, 10) select X.Contains(val) ? 1 : Y.Contains(val) ? 2 : val;
A = vals;

condition ?A:B returna A if the condition is true and B if the condition is not true.
It can be nested deep such like
condition1?A:condition2?B:C

1 Like

A = Enumerable.Range(0, L.Count).Where(i => L[i] == 2);

indexes.gh (6.4 KB)

2 Likes

A C# big secret is that Where() takes an index as the second parameter.

I wish (val,I) in vals where val==1 select i; also works but it does not.

List <int> vals = new List<int>{0,2,1,2,1,0,0,2,1};
var X2 = vals.Where((Val, i) => (Val == 1)).Select((Val, i) => i);
var Y2 = vals.Where((Val, i) => (Val == 2)).Select((Val, i) => i);
1 Like

This one works for me also with arrays, thank you :slight_smile:

Hmm… I thought this should work too but I am doing something wrong :thinking:
Do you might know what I am doing wrong :thinking:

question9a.gh (4.5 KB)

To make it more complex, to learn different ways of doing things, what am I doing wrong here :thinking:

// Making an array existing out of 0's
int[] arr = Enumerable.Repeat(0, 100).ToArray();


// Changing the array.
int[] itms1 = new int[]{0,1,2,12,14,20};
int[] itms2 = new int[]{42,43,55,80,91};
foreach (int i in itms1){
  arr[i] = 1;
}
foreach (int i in itms2){
  arr[i] = 2;
}


// Making a list of itms based on conditions.
int[] itms3 = new int[]{0,1,12,42};
List<int> non0itms = new List<int>();
foreach(Enumerable.Range(0,arr.Length)){
  if (arr[i] == 1 or arr[i] == 2 and itms3.Contains[i]){
    non0itms.Add(i);
  }
}

question9b.gh (8.2 KB)

I think variable names should not start with a number…

1 Like

I see :slight_smile:
About the array it says that the System.Array does not have a ‘Where’ method.

To do something similar, continue using predicates, do you might know how I can do that :thinking:

Hi, just add

using System.Linq;

Where, Select etc should be added to Array; :smiley:

1 Like

Done :sweat_smile:

I have had this in a previous attempts, and it gives me 0 and 1 while it must be 8 and 11 :thinking: so, do you might know how I have to write the predicate differently :thinking:

question9c.gh (4.1 KB)

Oops, it was my mistake.
The first Where gives a list with two items, so the second Select loops over only two items.

Maybe Zip?

int[] arr = new int[8]{0,1,0,1,0,1,0,1};
A=arr.Zip(Enumerable.Range(0,arr.Length),(val,i)=>(new Tuple<int,int>(val,i))).Where((val)=>(val.Item1==1)).Select((val)=>(val.Item2));
1 Like

Now that I can rewrite it to my favorite syntax.

int[] arr = new int[8]{0,1,0,1,0,1,0,1};
A=from val in arr.Zip(Enumerable.Range(0,arr.Length),(val,i)=>(new Tuple<int,int>(val,i))) where val.Item1==1 select val.Item2;
1 Like

About your favorite syntax :thinking: I am just curious in different comparable code. What do I have to change it to let it work :thinking:

question9d.gh (9.1 KB)

The forum doesn’t show square brackets correctly when it’s not wrapped in a code environment.
arr is an array. so the type is not int but

int[]

Hope this works this time.

1 Like

We are all horses running in a who wrote the longest single line code race :stuck_out_tongue_winking_eye:

1 Like