Is there a syntactical way in Swift to access the same indexed element within arrays within a 2D array, akin to accessing a column in a table, e.g.
Swift中是否有一种语法方法来访问二维数组中的相同索引元素,类似于访问表中的列,例如。
let A = [ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ] ]
A[ 2 ]
yields [ 7, 8, 9 ]. Is there a way to do something like
产量[7,8,9]。有什么方法可以做这样的事情吗
A[][ 2 ]
yields [ 3, 6, 9 ]?
收益率[3,6,9]?
I know I can do:
我知道我能做到:
var B = [ Int ]()
for c in A {
B.append( c[ 2 ] )
}
But I was wondering if there was another, perhaps syntactical, way.
但是我想知道是否还有另外一个,也许是句法的。
1 个解决方案
#1
1
Try this:
试试这个:
A.map { (elem) -> Int in
return elem[2]
}
Or the short version (credits to @Ian):
或短片(转载@Ian):
A.map { $0[2] }
#1
1
Try this:
试试这个:
A.map { (elem) -> Int in
return elem[2]
}
Or the short version (credits to @Ian):
或短片(转载@Ian):
A.map { $0[2] }