array

How can I add new array elements at the beginning of an array in JavaScript?

匿名 (未验证) 提交于 2019-12-03 02:08:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 由 翻译 强力驱动 问题: I have a need to add or prepend elements at the beginning of an array. For example, if my array looks like below: [ 23 , 45 , 12 , 67 ] And the response from my AJAX call is 34 , I want the updated array to be like the following: [ 34 , 23 , 45 , 12 , 67 ] Currently I am planning to do it like this: var newArray = []; newArray . push ( response ); for ( var i = 0 ; i < theArray . length ; i ++) { newArray . push ( theArray [ i ]); } theArray = newArray ; delete newArray ; Is there any better way to do this? Does JavaScript have any

What is a jagged array?

匿名 (未验证) 提交于 2019-12-03 02:08:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: What is a jagged array (in c#)? Any examples and when should one use it.... 回答1: A jagged array is an array of arrays. string[][] arrays = new string[5][]; That's a collection of five different string arrays, each could be a different length (they could also be the same length, but the point is there is no guarantee that they are). arrays[0] = new string[5]; arrays[1] = new string[100]; ... This is different from a 2D array where it is rectangular, meaning each row has the same number of columns. string[,] array = new string[3,5]; 回答2: A

Weighted standard deviation in NumPy?

匿名 (未验证) 提交于 2019-12-03 02:08:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: numpy.average() has a weights option, but numpy.std() does not. Does anyone have suggestions for a workaround? 回答1: How about the following short "manual calculation"? def weighted_avg_and_std(values, weights): """ Return the weighted average and standard deviation. values, weights -- Numpy ndarrays with the same shape. """ average = numpy.average(values, weights=weights) # Fast and numerically precise: variance = numpy.average((values-average)**2, weights=weights) return (average, math.sqrt(variance)) 回答2: There is a class in statsmodels to

How can I check whether an array is null / empty?

匿名 (未验证) 提交于 2019-12-03 02:08:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 由 翻译 强力驱动 问题: I have int array which has no elements and I'm trying to check whether it's empty. For example, why is the if-statement in the below code never true? int [] k = new int [ 3 ]; if ( k == null ) { System . out . println ( k . length ); } 回答1: There's a key difference between a null array and an empty array. This is a test for null . int arr [] = null ; if ( arr == null ) { System . out . println ( "array is null" ); } "Empty" here has no official meaning. I'm choosing to define empty as having 0 elements: arr = new int [ 0 ]; if (

How to round floats to integers while preserving their sum?

匿名 (未验证) 提交于 2019-12-03 02:08:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: Let's say I have an array of floating point numbers, in sorted (let's say ascending) order, whose sum is known to be an integer N . I want to "round" these numbers to integers while leaving their sum unchanged. In other words, I'm looking for an algorithm that converts the array of floating-point numbers (call it fn ) to an array of integers (call it in ) such that: the two arrays have the same length the sum of the array of integers is N the difference between each floating-point number fn[i] and its corresponding integer in[i] is less than

How to push both value and key into array

匿名 (未验证) 提交于 2019-12-03 02:08:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: Take a look at this code: $GET = array(); $key = 'one=1'; $rule = explode('=', $key); /* array_push($GET, $rule[0] => $rule[1]); */ I'm looking for something like this so that: print_r($GET); /* output: $GET[one => 1, two => 2, ...] */ Is there a function to do this? (because array_push won't work this way) 回答1: Nope, there is no array_push() equivalent for associative arrays because there is no way determine the next key. You'll have to use $arrayname[indexname] = $value; 回答2: Pushing a value into an array automatically creates a numeric

Should my PHP functions accept an array of arguments or should I explicitly request arguments?

匿名 (未验证) 提交于 2019-12-03 02:08:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 由 翻译 强力驱动 问题: In a PHP web application I'm working on, I see functions defined in two possible ways. Approach 1: function myfunc ( $arg1 , $arg2 , $arg3 ) Approach 2: // where $array_params has the structure array('arg1'=>$val1, 'arg2'=>$val2, 'arg3'=>$val3) function myfunc ( $array_params ) When should I use one approach over another? It seems that if system requirements keep changing, and therefore the number of arguments for myfunc keep changing, approach 1 may require a lot of maintenance. 回答1: If the system is changing so often that using

What is the equivalent of memset in C#?

匿名 (未验证) 提交于 2019-12-03 02:08:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I need to fill a byte[] with a single non-zero value. How can I do this in C# without looping through each byte in the array? Update: The comments seem to have split this into two questions - Is there a Framework method to fill a byte[] that might be akin to memset What is the most efficient way to do it when we are dealing with a very large array? I totally agree that using a simple loop works just fine, as Eric and others have pointed out. The point of the question was to see if I could learn something new about C# :) I think Juliet's

Use numpy array in shared memory for multiprocessing

匿名 (未验证) 提交于 2019-12-03 02:08:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I would like to use a numpy array in shared memory for use with the multiprocessing module. The difficulty is using it like a numpy array, and not just as a ctypes array. from multiprocessing import Process, Array import scipy def f(a): a[0] = -a[0] if __name__ == '__main__': # Create the array N = int(10) unshared_arr = scipy.rand(N) arr = Array('d', unshared_arr) print "Originally, the first two elements of arr = %s"%(arr[:2]) # Create, start, and finish the child processes p = Process(target=f, args=(arr,)) p.start() p.join() # Printing

How to remove element from an array in JavaScript?

匿名 (未验证) 提交于 2019-12-03 02:08:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 由 翻译 强力驱动 问题: var arr = [ 1 , 2 , 3 , 5 , 6 ]; I want to remove the 1st element of the array so that it becomes: var arr = [ 2 , 3 , 5 , 6 ]; To extend this question, what if I want to remove the 2nd element of the array so that it becomes: var arr = [ 1 , 3 , 5 , 6 ]; 回答1: For a more flexible solution, use the splice() function. It allows you to remove any item in an Array based on Index Value: var indexToRemove = 0 ; var numberToRemove = 1 ; arr . splice ( indexToRemove , numberToRemove ); 回答2: shift() is ideal for your situation. shift()