array

file_get_contents behind a proxy?

匿名 (未验证) 提交于 2019-12-03 02:11:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: At work we have to use a proxy to basically access port 80 for example, we have our own custom logins for each user. My temporary workaround is using curl to basically login as myself through a proxy and access the external data I need. Is there some sort of advanced php setting I can set so that internally whenever it tries to invoke something like file_get_contents() it always goes through a proxy? I'm on Windows ATM so it'd be a pain to recompile if that's the only way. The reason my workaround is temporary is because I need a solution

NumPy: Return 0 with divide by zero

匿名 (未验证) 提交于 2019-12-03 02:11:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I'm trying to perform an element wise divide in python, but if a zero is encountered, I need the quotient to just be zero. For example: array1 = np.array([0, 1, 2]) array2 = np.array([0, 1, 1]) array1 / array2 # should be np.array([0, 1, 2]) I could always just use a for-loop through my data, but to really utilize numpy's optimizations, I need the divide function to return 0 upon divide by zero errors instead of ignoring the error. Unless I'm missing something, it doesn't seem numpy.seterr() can return values upon errors. Does anyone have

How do I access structure fields dynamically?

匿名 (未验证) 提交于 2019-12-03 02:11:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I have a structure with many fields which are vectors of different lengths. I would like to access the fields within a loop, in order. I tried getfield as follows but MATLAB doesn't like that. How can I do this? S = struct('A', [1 2], 'B',[3 4 5]); SNames = fieldnames(S); for loopIndex = 1:2 field = getfield(S, SNames(loopIndex)); %do stuff w/ field end ??? Index exceeds matrix dimensions I'm using structures in the first place because an array would have trouble with the different field lengths. Is there a better alternative to that? 回答1:

Array.push() if does not exist?

匿名 (未验证) 提交于 2019-12-03 02:11:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: How can I push into an array if neither values exist? Here is my array: [ { name: "tom", text: "tasty" }, { name: "tom", text: "tasty" }, { name: "tom", text: "tasty" }, { name: "tom", text: "tasty" }, { name: "tom", text: "tasty" } ] If I tried to push again into the array with either name: "tom" or text: "tasty" , I don't want anything to happen... but if neither of those are there then I want it to .push() How can I do this? 回答1: You could extend the Array prototype with a custom method: // check if an element exists in array using a

forEach on array of undefined created by Array constructor

匿名 (未验证) 提交于 2019-12-03 02:11:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I am just wondering why it is not possible to make forEach on array of undefined. Code: var arr = new Array(5); // [undefined x 5] //ES5 forEach arr.forEach(function(elem, index, array) { console.log(index); }); //underscore each _.each(arr, function(elem, index, array) { console.log(index); }); Both examples do not execute function. Now to make foreach, I have to make: var arr = [0,0,0,0,0]; Then make forEach on it. I am trying to make an array with specified size and loop through it, avoiding for loop. I think that it is clearer using

Create an empty object in JavaScript with {} or new Object()?

匿名 (未验证) 提交于 2019-12-03 02:11:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: There are two different ways to create an empty object in JavaScript: var objectA = {} var objectB = new Object() Is there any difference in how the script engine handles them? Is there any reason to use one over the other? Similarly it is also possible to create an empty array using different syntax: var arrayA = [] var arrayB = new Array() 回答1: Objects There is no benefit to using new Object(); -- whereas {}; can make your code more compact, and more readable. For defining empty objects they're technically the same. The {} syntax is

Javascript swap array elements

匿名 (未验证) 提交于 2019-12-03 02:11:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: Is there any simpler way to swap two elements in an array? var a = list[x], b = list[y]; list[y] = a; list[x] = b; 回答1: You only need one temporary variable. var b = list[y]; list[y] = list[x]; list[x] = b; 回答2: If you want a single expression, using native javascript, remember that the return value from a splice operation contains the element(s) that was removed. var A = [1, 2, 3, 4, 5, 6, 7, 8, 9], x= 0, y= 1; A[x] = A.splice(y, 1, A[x])[0]; alert(A); // alerts "2,1,3,4,5,6,7,8,9" Edit: The [0] is necessary at the end of the expression as

JSON.stringify doesn't work with normal Javascript array

匿名 (未验证) 提交于 2019-12-03 02:11:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I must be missing something here, but the following code ( Fiddle ) returns an empty string: var test = new Array(); test['a'] = 'test'; test['b'] = 'test b'; var json = JSON.stringify(test); alert(json); What is the correct way of JSON'ing this array? 回答1: Normal JavaScript arrays are designed to hold data with numeric indexes. You can stuff named keys on to them (and this can be useful when you want to store metadata about an array which holds normal, ordered, numerically indexed data), but that isn't what they are designed for. The JSON

varargs and the '…' argument

匿名 (未验证) 提交于 2019-12-03 02:11:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: Consider the method declaration: String.format(String, Object ...) The Object ... argument is just a reference to an array of Object s. Is there a way to use this method with a reference to an actual Object array? If I pass in an Object array to the ... argument - will the resultant argument value be a two-dimensional array - because an Object[] is itself an Object : Object[] params = ....; // Make the array (for example based on user-input) String s = String.format("%S has %.2f euros", params); So the first component of the array (Which is

Find the most popular element in int[] array

匿名 (未验证) 提交于 2019-12-03 02:11:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: int[] a = new int[10]{1,2,3,4,5,6,7,7,7,7}; how can I write a method and return 7? I want to keep it native without the help of lists, maps or other helpers. Only arrays[]. 回答1: public int getPopularElement(int[] a) { int count = 1, tempCount; int popular = a[0]; int temp = 0; for (int i = 0; i count) { popular = temp; count = tempCount; } } return popular; } 回答2: Try this answer. First, the data: int[] a = {1,2,3,4,5,6,7,7,7,7}; Here, we build a map counting the number of times each number appears: Map map = new HashMap (); for (int i : a)