C# Find the Index of an Object in List where Object Parameter x equals Value - c#-4.0

I need to find the index value of the object in an object list where the parameter x equals a certain value.
Is there a simple way to do this? I understand the IndexOf function, but how can I apply it to the object parameter?

You can use List.FindIndex:
int index = list.FindIndex(obj => obj.X == value);
The zero-based index of the first occurrence of an element that
matches the conditions defined by match, if found; otherwise, –1.

Related

new Array(n).map((_, index) => index) cannot initialize an array

new Array(n) will create an array of n * undefined, so why can't you use new Array(n).map((_, index) => index) function to get [0, 1, 2 ..., n-1 ] What about arrays like this?
I know that new Array(n).fill(0).map((_, index) => index) is ok, but is there any essential difference between the two arrays of n * undefined and n * 0 ?
Common pits for initializing an n*m two-dimensional array:
Wrong way 1: new Array(n).fill(new Array(m).fill(0)) All arrays point to the same reference
Wrong way 2: new Array(n).map(v => new Array(m).fill(0)) "ghost array" problem
Correct way 1: new Array(n).fill(0).map(v => new Array(m).fill(0))
From the MDN Web Docs:
If the only argument passed to the Array constructor is an integer [...] this returns a new JavaScript array with its length property set to that number (Note: this implies an array of arrayLength empty slots, not slots with actual undefined values)
So like they say, it just sets the length, it doesn't set any of the values. So if the array contains no elements, then you can't map a non-existent value into a function.
Array Document
If the only argument passed to the Array constructor is an integer between 0 and 2^32 - 1 (inclusive), this returns a new JavaScript array with its length property set to that number (Note: this implies an array of arrayLength empty slots, not slots with actual undefined values — see sparse arrays). If the argument is any other number, a RangeError exception is thrown.
Array(n) or new Array(n) produces arrays with only length and no elements, so I call them "ghost arrays". Such an array cannot be traversed correctly with forEach or map (because there are no elements), but it is amazing that it has [##iterator], which can be traversed using for ... of, or expanded into an array using the spread operator (eg [...Array(10)]), and can be converted using Array.from().

Groovy from 2nd dimentional array to 1 dimentional as string with prefix

I have an 2 dimentional array:
def test = [[88,3,2],[22,33,4],[88,3,3]]
test.sort
what i need now is to create each item into string and prefix it with string "test-"
so the end result would ne one dimentional array:
def endResult = ["test-88.3.2"],["test-88.3.3"],["test-22.33.4"]
if i do:
test.each {println it.join(".")}
it prints the first part but as written i need to save it and add prefix
im new to groovy any help would be great
The each method does not produce any result - it only iterates the input collection and allows you to do something with each element (e.g. print it to the console like in the example you showed in your question.)
If you want to manipulate each element of the collection and store it as a new collection, you can use the collect method which also takes a closure as a parameter. This closure is applied to each element of the input collection, and the return value of this closure is used to return a new collection from the collect method.
Something like this should do the trick for you:
def test = [[88,3,2],[22,33,4],[88,3,3]]
def endResult = test.collect { 'test-' + it.join('.') }
println endResult // [test-88.3.2, test-22.33.4, test-88.3.3]
It's worth mentioning that the closure we passed to the collect method uses so-called implicit return - there is no return keyword, but the value it produces from 'test-' + it.join('.') is returned implicitly.

Pandas apply function - args how is it passed

df = pd.read_csv('https://raw.githubusercontent.com/selva86/datasets/master/Cars93_miss.csv')
d = {'Min.Price': np.nanmean, 'Max.Price': np.nanmedian}
df[['Min.Price', 'Max.Price']] = df[['Min.Price', 'Max.Price']].apply(lambda x, d: x.fillna(d[x.name](x)), args=(d, ))
apply(lambda x, d: x.fillna(d[x.name](x)), args=(d, )) - Am not understanding this part. I know how apply and fillna works but with this args it's confusing.
why does args have comma at the end
what does the d actually pass -
As you know, df.apply function applies a function to the DataFrame's elements. Now this function normally takes only one argument. When you need to use a multi argument function with df.apply, you can specify the other arguments with the args parameter
From the docstring (print(pd.DataFrame.apply.__doc__))
args : tuple
Positional arguments to pass to `func` in addition to the
array/series.
Here you are using two argument function with lambda x, d: therefore you need to specify the 2nd argument with args=. Here it is expecting a tuple with the arguments
To put a single element into a tuple, you need to put it into brackets with a trailing comma.
print(1)
>>>1
print((1,))
>>>(1,)
To put the variable d into a tuple, you need to do (d,)
The dictionary contains two separate functions for the two columns with column names as its keys. So x being the column passed into the lambda function, x.name gives you the column name and d[x.name] gives you the function intended for that column name. Then that function is applied to the column.
For the 'Min.Price' column the function is np.nanmean. So d[x.name](x) is evaluated to np.nanmean(x) which gives you the column mean excluding nans. Now you're using the mean value to fill the nans in the original column by doing x.fillna(d[x.name](x))
To more directly answer your question:
args is a tuple, and a single element tuple requires a trailing comma in order to properly be identified as a tuple and not just a parameterized object.
d is your dictionary, and when it's being passed at the end I believe it is your current list after having the lambda function applied to each element in the dictionary saying that if the element is NaN or N/A (ie Null/missing) then it will fill them with the given value specified. pandas.DataFrame.fillna
Hopefully that helps.

Do the Groovy every and any methods short-circuit?

Suppose the every or any method is being evaluated on a long list, and the value of the closure on the first element is enough to determine the final result (true for any, false for every). Do these methods continue processing the rest of the elements of the list or do they immediately return?
def lst = [1,2,3,4]
//Do either of these functions evaluate the 2nd, 3rd and 4th element of lst?
boolean value1 = lst.any{element -> element == 1}
boolean value2 = lst.every {element -> element == 2}
any() and every() short-circuit and return as soon as possible.
You can see this in the source for any() and every(). These methods each iterate over each object evaluating the closure and return if the condition is (for any())/is not (for every()) met.

How to write a function max_and_min that accepts a tuple containing integer elements as an argument?

Functions can only return a single value but sometimes, we may want functions to return multiple values. Tuples can come in handy in such cases. We can create a tuple containing multiple values and return the tuple instead of a single value.
Write a function max_and_min that accepts a tuple containing integer elements as an argument and returns the largest and smallest integer within the tuple. The return value should be a tuple containing the largest and smallest value, in that order.
for example: max_and_min((1, 2, 3, 4, 5)) = (5, 1)
I am told to use an iteration to loop through each value of the tuple parameter to find the maximum and minimum values. Also, I must use Python 3.x.
How do I do this? I am really clueless. Thanks for your help!
def max_and_min(values):
# Write your code here
You are looking to pass a variable number of arguments to a function. In python, you can get multiple arguments passed at invocation with the * notation:
def max_and_min(*arg):
return (max(arg), min(arg))
Note that the Python 3 min and max functions themselves accept a variable number of arguments.

Resources