Swift 3 数组,使用 .remove(at: i) 一次删除多个项目
Swift 3 Array, remove more than one item at once, with .remove(at: i)
是否可以同时从数组中删除多个项目,使用索引位置按照 .remove(at: i) 有点像:
伪代码:
myArray.remove(at: 3, 5, 8, 12)
如果是这样,这样做的语法是什么?
更新:
我正在尝试这个,它起作用了,但下面答案中的扩展更具可读性和合理性,并且实现了与伪代码完全相同的目标。
创建了一个 "positions" 的数组:[3, 5, 8, 12]
let sorted = positions.sorted(by: { < [=12=] })
for index in sorted
{
myArray.remove(at: index)
}
如果索引是连续的,使用removeSubrange
方法是可能的。
例如,如果您想删除索引 3 到 5 处的项目:
myArray.removeSubrange(ClosedRange(uncheckedBounds: (lower: 3, upper: 5)))
对于非连续索引,我建议将索引较大的项目移至较小的项目。除了代码可以更短之外,我想不出在一行中删除项目 "at the same time" 没有任何好处。您可以使用扩展方法来做到这一点:
extension Array {
mutating func remove(at indexes: [Int]) {
for index in indexes.sorted(by: >) {
remove(at: index)
}
}
}
然后:
myArray.remove(at: [3, 5, 8, 12])
更新:使用上述解决方案,您需要确保索引数组不包含重复索引。或者您可以避免重复,如下所示:
extension Array {
mutating func remove(at indexes: [Int]) {
var lastIndex: Int? = nil
for index in indexes.sorted(by: >) {
guard lastIndex != index else {
continue
}
remove(at: index)
lastIndex = index
}
}
}
var myArray = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]
myArray.remove(at: [5, 3, 5, 12]) // duplicated index 5
// result: [0, 1, 2, 4, 6, 7, 8, 9, 10, 11, 13] only 3 elements are removed
使用数组元素的索引删除元素:
字符串和索引数组
let animals = ["cats", "dogs", "chimps", "moose", "squarrel", "cow"]
let indexAnimals = [0, 3, 4]
let arrayRemainingAnimals = animals
.enumerated()
.filter { !indexAnimals.contains([=10=].offset) }
.map { [=10=].element }
print(arrayRemainingAnimals)
//result - ["dogs", "chimps", "cow"]
整数和索引数组
var numbers = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
let indexesToRemove = [3, 5, 8, 12]
numbers = numbers
.enumerated()
.filter { !indexesToRemove.contains([=11=].offset) }
.map { [=11=].element }
print(numbers)
//result - [0, 1, 2, 4, 6, 7, 9, 10, 11]
使用另一个数组的元素值删除元素
整数数组
var numbers = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
let elementsTobeRemoved = [3, 5, 8, 12]
let arrayResult = numbers.filter { element in
return !elementsTobeRemoved.contains(element)
}
print(arrayResult)
//result - [0, 1, 2, 4, 6, 7, 9, 10, 11]
字符串数组
let arrayLetters = ["a", "b", "c", "d", "e", "f", "g", "h", "i"]
let arrayRemoveLetters = ["a", "e", "g", "h"]
let arrayRemainingLetters = arrayLetters.filter {
!arrayRemoveLetters.contains([=13=])
}
print(arrayRemainingLetters)
//result - ["b", "c", "d", "f", "i"]
根据 NSMutableArray
API 我建议将索引实现为 IndexSet
。
你只需要倒序即可。
extension Array {
mutating func remove(at indexes: IndexSet) {
indexes.reversed().forEach{ self.remove(at: [=10=]) }
}
}
另请参阅this answer提供更有效的算法。
您可以创建一组要删除的索引。
var array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
let indexSet = [3, 5, 8, 12]
indexSet.reversed().forEach{ array.remove(at: [=10=]) }
print(array)
Output: [0, 1, 2, 4, 6, 7, 9, 10, 11]
如果索引是连续的则使用removeSubrange
array.removeSubrange(1...3) /// Will remove the elements from 1, 2 and 3 positions.
简单明了的解决方案,只需Array
扩展名:
extension Array {
mutating func remove(at indices: [Int]) {
Set(indices)
.sorted(by: >)
.forEach { rmIndex in
self.remove(at: rmIndex)
}
}
}
Set(indices)
- 确保唯一性
.sorted(by: >)
- 函数从最后到第一个删除元素,因此在删除期间我们确保索引是正确的
Swift 4
extension Array {
mutating func remove(at indexs: [Int]) {
guard !isEmpty else { return }
let newIndexs = Set(indexs).sorted(by: >)
newIndexs.forEach {
guard [=10=] < count, [=10=] >= 0 else { return }
remove(at: [=10=])
}
}
}
var arr = ["a", "b", "c", "d", "e", "f"]
arr.remove(at: [2, 3, 1, 4])
result: ["a", "f"]
是否可以同时从数组中删除多个项目,使用索引位置按照 .remove(at: i) 有点像:
伪代码:
myArray.remove(at: 3, 5, 8, 12)
如果是这样,这样做的语法是什么?
更新:
我正在尝试这个,它起作用了,但下面答案中的扩展更具可读性和合理性,并且实现了与伪代码完全相同的目标。
创建了一个 "positions" 的数组:[3, 5, 8, 12]
let sorted = positions.sorted(by: { < [=12=] })
for index in sorted
{
myArray.remove(at: index)
}
如果索引是连续的,使用removeSubrange
方法是可能的。
例如,如果您想删除索引 3 到 5 处的项目:
myArray.removeSubrange(ClosedRange(uncheckedBounds: (lower: 3, upper: 5)))
对于非连续索引,我建议将索引较大的项目移至较小的项目。除了代码可以更短之外,我想不出在一行中删除项目 "at the same time" 没有任何好处。您可以使用扩展方法来做到这一点:
extension Array {
mutating func remove(at indexes: [Int]) {
for index in indexes.sorted(by: >) {
remove(at: index)
}
}
}
然后:
myArray.remove(at: [3, 5, 8, 12])
更新:使用上述解决方案,您需要确保索引数组不包含重复索引。或者您可以避免重复,如下所示:
extension Array {
mutating func remove(at indexes: [Int]) {
var lastIndex: Int? = nil
for index in indexes.sorted(by: >) {
guard lastIndex != index else {
continue
}
remove(at: index)
lastIndex = index
}
}
}
var myArray = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]
myArray.remove(at: [5, 3, 5, 12]) // duplicated index 5
// result: [0, 1, 2, 4, 6, 7, 8, 9, 10, 11, 13] only 3 elements are removed
使用数组元素的索引删除元素:
字符串和索引数组
let animals = ["cats", "dogs", "chimps", "moose", "squarrel", "cow"] let indexAnimals = [0, 3, 4] let arrayRemainingAnimals = animals .enumerated() .filter { !indexAnimals.contains([=10=].offset) } .map { [=10=].element } print(arrayRemainingAnimals) //result - ["dogs", "chimps", "cow"]
整数和索引数组
var numbers = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12] let indexesToRemove = [3, 5, 8, 12] numbers = numbers .enumerated() .filter { !indexesToRemove.contains([=11=].offset) } .map { [=11=].element } print(numbers) //result - [0, 1, 2, 4, 6, 7, 9, 10, 11]
使用另一个数组的元素值删除元素
整数数组
var numbers = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12] let elementsTobeRemoved = [3, 5, 8, 12] let arrayResult = numbers.filter { element in return !elementsTobeRemoved.contains(element) } print(arrayResult) //result - [0, 1, 2, 4, 6, 7, 9, 10, 11]
字符串数组
let arrayLetters = ["a", "b", "c", "d", "e", "f", "g", "h", "i"] let arrayRemoveLetters = ["a", "e", "g", "h"] let arrayRemainingLetters = arrayLetters.filter { !arrayRemoveLetters.contains([=13=]) } print(arrayRemainingLetters) //result - ["b", "c", "d", "f", "i"]
根据 NSMutableArray
API 我建议将索引实现为 IndexSet
。
你只需要倒序即可。
extension Array {
mutating func remove(at indexes: IndexSet) {
indexes.reversed().forEach{ self.remove(at: [=10=]) }
}
}
另请参阅this answer提供更有效的算法。
您可以创建一组要删除的索引。
var array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
let indexSet = [3, 5, 8, 12]
indexSet.reversed().forEach{ array.remove(at: [=10=]) }
print(array)
Output: [0, 1, 2, 4, 6, 7, 9, 10, 11]
如果索引是连续的则使用removeSubrange
array.removeSubrange(1...3) /// Will remove the elements from 1, 2 and 3 positions.
简单明了的解决方案,只需Array
扩展名:
extension Array {
mutating func remove(at indices: [Int]) {
Set(indices)
.sorted(by: >)
.forEach { rmIndex in
self.remove(at: rmIndex)
}
}
}
Set(indices)
- 确保唯一性.sorted(by: >)
- 函数从最后到第一个删除元素,因此在删除期间我们确保索引是正确的
Swift 4
extension Array {
mutating func remove(at indexs: [Int]) {
guard !isEmpty else { return }
let newIndexs = Set(indexs).sorted(by: >)
newIndexs.forEach {
guard [=10=] < count, [=10=] >= 0 else { return }
remove(at: [=10=])
}
}
}
var arr = ["a", "b", "c", "d", "e", "f"]
arr.remove(at: [2, 3, 1, 4])
result: ["a", "f"]