什么可能是以下python代码的快速等价物?
array =[ "a", "b", "c"]
print(array[1:])
(上面的语句打印从第一个索引到数组末尾的每个元素 输出 ['b','c'])
修改
有没有办法可以使用array.count完成此操作?因为如果我说想要从第二个位置的每个元素
答案 0 :(得分:3)
您可以获得类似的快速数组的子范围:
let array =[ "a", "b", "c"]
//be sure that your array.count has more than 1 item (in this case)
let subArray1 = array[1..<array.count]
print(subArray1)
//or
let subArray2 = array[1...array.count-1]
print(subArray2)
这是Swift编程语言书中的2个笔记
“使用..创建一个省略其上限值的范围,并使用...来 制作一个包含两个值的范围。“
和
“如果您尝试使用下标语法来检索或设置值 如果索引位于数组的现有边界之外,则会触发 运行时错误。但是,您可以检查索引是否有效 使用它,通过将它与数组的count属性进行比较。除外 count为0(表示数组为空),是一个中最大的有效索引 数组将始终为count - 1,因为数组从零开始索引。“
答案 1 :(得分:2)
您可以通过以下方式实现您的目标:
1。创建自定义结构以存储开始和结束索引。如果startIndex
或endIndex
为nil
,则会认为该范围在该方向无限延伸。
struct UnboundedRange<Index> {
var startIndex, endIndex: Index?
// Providing these initialisers prevents both `startIndex` and `endIndex` being `nil`.
init(start: Index) {
self.startIndex = start
}
init(end: Index) {
self.endIndex = end
}
}
2。定义运营商以创建BoundedRange
因为必须使用初始化工具会导致一些非常难看的代码,在我的选项中。
postfix operator ... {}
prefix operator ... {}
postfix func ... <Index> (startIndex: Index) -> UnboundedRange<Index> {
return UnboundedRange(start: startIndex)
}
prefix func ... <Index> (endIndex: Index) -> UnboundedRange<Index> {
return UnboundedRange(end: endIndex)
}
一些示例用法:
1... // An UnboundedRange<Int> that extends from 1 to infinity.
...10 // An UnboundedRange<Int> that extends from minus infinity to 10.
3。扩展CollectionType
以便它可以处理UnboundedRange
。
extension CollectionType {
subscript(subrange: UnboundedRange<Index>) -> SubSequence {
let start = subrange.startIndex ?? self.startIndex
let end = subrange.endIndex?.advancedBy(1) ?? self.endIndex
return self[start..<end]
}
}
4. 要在给定示例中使用此功能:
let array = ["a", "b", "c"]
array[1...] // Returns ["b", "c"]
array[...1] // Returns ["a", "b"]
答案 2 :(得分:2)
使用Swift 4,有很多方法可以解决您的问题。根据您的需要,您可以选择以下六种模式之一。
Array
dropFirst()
方法let array = ["a", "b", "c"]
let arraySlice = array.dropFirst()
let newArray = Array(arraySlice)
print(newArray) // prints ["b", "c"]
Array
suffix(from:)
方法let array = ["a", "b", "c"]
let arraySlice = array.suffix(from: 1)
let newArray = Array(arraySlice)
print(newArray) // prints ["b", "c"]
Array
suffix(_:)
方法let array = ["a", "b", "c"]
let arraySlice = array.suffix(array.endIndex.advanced(by: -1))
// let arraySlice = array.suffix(array.count - 1) // also works
let newArray = Array(arraySlice)
print(newArray) // prints ["b", "c"]
Array
subscript(_:)
和CountableRange
let array = ["a", "b", "c"]
let range = array.startIndex.advanced(by: 1) ..< array.endIndex
// let range = 1 ..< array.count // also works
let arraySlice = array[range]
let newArray = Array(arraySlice)
print(newArray) // prints ["b", "c"]
Array
subscript(_:)
和CountableClosedRange
let array = ["a", "b", "c"]
let range = 1 ... array.count - 1 // also works
let arraySlice = array[range]
let newArray = Array(arraySlice)
print(newArray) // prints ["b", "c"]
Array
subscript(_:)
和CountablePartialRangeFrom
let array = ["a", "b", "c"]
let range = 1...
let arraySlice = array[range]
let newArray = Array(arraySlice)
print(newArray) // prints ["b", "c"]