array

Swift Set to Array

匿名 (未验证) 提交于 2019-12-03 01:49:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: An NSSet can be converted to Array using set.allObjects() but there is no such method in the new Set (introduced with Swift 1.2). It can still be done by converting Swift Set to NSSet and use the allObjects() method but that is not optimal. 回答1: You can create an array with all elements from a given Swift Set simply with let array = Array(someSet) This works because Set conforms to the SequenceType protocol and an Array can be initialized with a sequence. Example: let mySet = Set(["a", "b", "a"]) // Set let myArray = Array(mySet) // Array

Finding the max value of an attribute in an array of objects

匿名 (未验证) 提交于 2019-12-03 01:49:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I'm looking for a really quick, clean and efficient way to get the max "y" value in the following JSON slice: [{"x":"8/11/2009","y":0.026572007},{"x":"8/12/2009","y":0.025057454},{"x":"8/13/2009","y":0.024530916},{"x":"8/14/2009","y":0.031004457}] Is a for-loop the only way to go about it? I'm keen on somehow using Math.max . 回答1: Math.max.apply(Math,array.map(function(o){return o.y;})) 回答2: One way would be to use Array reduce.. const max = data.reduce(function(prev, current) { return (prev.y > current.y) ? prev : current }) //returns

Using lodash to check whether an array has duplicate values

匿名 (未验证) 提交于 2019-12-03 01:49:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: What do you all think would be the best (best can be interpreted as most readable or most performant, your choice) way to write a function using the lodash utilities in order to check an array for duplicate values. I want to input ['foo', 'foo', 'bar'] and have the function return true . And input ['foo', 'bar', 'baz'] and have the function return false . 回答1: You can try this code: function hasDuplicates(a) { return _.uniq(a).length !== a.length; } var a = [1,2,1,3,4,5]; var b = [1,2,3,4,5,6]; document.write(hasDuplicates(a), ','

In Scala, why do I get this “polymorphic expression cannot be instantiated to expected type”?

匿名 (未验证) 提交于 2019-12-03 01:49:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: Why does the following occur in Scala 2.9.0.1? scala> def f(xs: Seq[Either[Int,String]]) = 0 f: (xs: Seq[Either[Int,String]])Int scala> val xs = List(Left(0), Right("a")).iterator.toArray xs: Array[Product with Serializable with Either[Int,java.lang.String]] = Array(Left(0), Right(a)) scala> f(xs) res39: Int = 0 scala> f(List(Left(0), Right("a")).iterator.toArray) :9: error: polymorphic expression cannot be instantiated to expected type; found : [B >: Product with Serializable with Either[Int,java.lang.String]]Array[B] required: Seq[Either

Why isn't std::array::size static?

匿名 (未验证) 提交于 2019-12-03 01:49:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: The size of std::array is known at compile time, but the size member function isn't static. Is there any reason for that? It's slightly inconvenient not to be able to calculate the size without instantiating an object. (Well, I know about std::tuple_size specialization, but it doesn't work for classes derived from std::array .) 回答1: Since C++11 you can use std::tuple_size on std::array to obtain the size as a compile time constant. See http://en.cppreference.com/w/cpp/container/array/tuple_size 回答2: There is no good reason for that. In fact,

Why Numpy treats a+=b and a=a+b differently

匿名 (未验证) 提交于 2019-12-03 01:49:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: Is the following numpy behavior intentional or is it a bug? from numpy import * a = arange(5) a = a+2.3 print 'a = ', a # Output: a = 2.3, 3.3, 4.3, 5.3, 6.3 a = arange(5) a += 2.3 print 'a = ', a # Output: a = 2, 3, 4, 5, 6 Python version: 2.7.2, Numpy version: 1.6.1 回答1: That's intentional. The += operator preserves the type of the array. In other words, an array of integers remains an array of integers. This enables NumPy to perform the += operation using existing array storage. On the other hand, a=a+b creates a brand new array for the

GetType() on Array item?

匿名 (未验证) 提交于 2019-12-03 01:49:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I have an initialised array that may contain no items. Lets call it a , Calling GetType() on a will obviously return a type of Array. Is it possible to get the type of the items the array contains? Obviously a[0].GetType() would work, but then the array could be empty and cause a null reference exception. 回答1: Well, you can get the element type of the array : Type type = array.GetType().GetElementType(); (That's not quite the same as getting the types of the items in the array - an object[] may be entirely populated with strings, for example

How to delete/unset the properties of a javascript object? [duplicate]

匿名 (未验证) 提交于 2019-12-03 01:49:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: Possible Duplicates: How to unset a Javascript variable? How to remove a property from a javascript object I'm looking for a way to remove/unset the properties of a JS object so they'll no longer come up if I loop through the object doing for (var i in myObject) . How can this be done? 回答1: simply use delete , but be aware that you should read fully what the effects are of using this: delete object.index; //true object.index; //undefined but if I was to use like so: var x = 1; //1 delete x; //false x; //1 but if you do wish to delete

How to initialize an array in Kotlin with values?

匿名 (未验证) 提交于 2019-12-03 01:49:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: In Java an array can be initialized such as: int numbers[] = new int[] {10, 20, 30, 40, 50} How does Kotlin's array initialization look like? 回答1: You can: val numbers: IntArray = intArrayOf(10, 20, 30, 40, 50) See Kotlin - Basic Types for details. 回答2: Here's an example: fun main(args: Array ) { val arr = arrayOf(1, 2, 3); for (item in arr) { println(item); } } You can also use a playground to test language features. 回答3: Worth to mention that when using kotlin builtines (e.g. intArrayOf() , longArrayOf() , arrayOf() , etc) you are not able

Convert Python sequence to NumPy array, filling missing values

匿名 (未验证) 提交于 2019-12-03 01:49:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 由 翻译 强力驱动 问题: The implicit conversion of a Python sequence of variable-length lists into a NumPy array cause the array to be of type object . v = [[ 1 ], [ 1 , 2 ]] np . array ( v ) >>> array ([[ 1 ], [ 1 , 2 ]], dtype = object ) Trying to force another type will cause an exception: np . array ( v , dtype = np . int32 ) ValueError : setting an array element with a sequence . What is the most efficient way to get a dense NumPy array of type int32, by filling the "missing" values with a given placeholder? From my sample sequence v , I would like