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.
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
To make it more complex, to learn different ways of doing things, what am I doing wrong here
// 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);
}
}
I have had this in a previous attempts, and it gives me 0 and 1 while it must be 8 and 11 so, do you might know how I have to write the predicate differently
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;