xcode Swift 3 Array,使用 .remove(at: i) 一次删除多个项目
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/39974838/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Swift 3 Array, remove more than one item at once, with .remove(at: i)
提问by Confused
Is it possible to remove more than one item from an array, at the same time, using index locations as per .remove(at: i) kind of like:
是否可以使用 .remove(at: i) 中的索引位置同时从数组中删除多个项目,类似于:
Pseudo code:
伪代码:
myArray.remove(at: 3, 5, 8, 12)
And if so, what's the syntax for doing this?
如果是这样,这样做的语法是什么?
UPDATE:
更新:
I was trying this, it worked, but the extension in the answer below is much more readable, and sensible, and achieves the goal of one that's exactly as the pseudo code.
我正在尝试这个,它奏效了,但下面答案中的扩展更具可读性,更明智,并且实现了与伪代码完全相同的目标。
an array of "positions" is created: [3, 5, 8, 12]
创建了一个“位置”数组:[3, 5, 8, 12]
let sorted = positions.sorted(by: { < myArray.removeSubrange(ClosedRange(uncheckedBounds: (lower: 3, upper: 5)))
})
for index in sorted
{
myArray.remove(at: index)
}
回答by Thanh Pham
It's possible if the indexes are continuous using removeSubrange
method.
For example, if you would like to remove items at index 3 to 5:
如果索引是连续的使用removeSubrange
方法是可能的。例如,如果您想删除索引 3 到 5 处的项目:
extension Array {
mutating func remove(at indexes: [Int]) {
for index in indexes.sorted(by: >) {
remove(at: index)
}
}
}
For non-continuous indexes, I would recommend remove items with larger index to smaller one. There is no benefit I could think of of removing items "at the same time" in one-liner except the code could be shorter. You can do so with an extension method:
对于非连续索引,我建议将索引较大的项目删除为较小的项目。除了代码可以更短之外,我无法想到在单行中“同时”删除项目没有任何好处。您可以使用扩展方法来做到这一点:
myArray.remove(at: [3, 5, 8, 12])
Then:
然后:
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
UPDATE: using the solution above, you would need to ensure the indexes array does not contain duplicated indexes. Or you can avoid the duplicates as below:
更新:使用上面的解决方案,您需要确保索引数组不包含重复的索引。或者您可以避免重复,如下所示:
let animals = ["cats", "dogs", "chimps", "moose", "squarrel", "cow"]
let indexAnimals = [0, 3, 4]
let arrayRemainingAnimals = animals
.enumerated()
.filter { !indexAnimals.contains(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(let animals = ["cats", "dogs", "chimps", "moose", "squarrel", "cow"]
let indexAnimals = [0, 3, 4]
let arrayRemainingAnimals = animals
.enumerated()
.filter { !indexAnimals.contains(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(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]
.offset) }
.map { let arrayLetters = ["a", "b", "c", "d", "e", "f", "g", "h", "i"]
let arrayRemoveLetters = ["a", "e", "g", "h"]
let arrayRemainingLetters = arrayLetters.filter {
!arrayRemoveLetters.contains(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]
)
}
print(arrayRemainingLetters)
//result - ["b", "c", "d", "f", "i"]
.element }
print(numbers)
//result - [0, 1, 2, 4, 6, 7, 9, 10, 11]
.offset) }
.map { let arrayLetters = ["a", "b", "c", "d", "e", "f", "g", "h", "i"]
let arrayRemoveLetters = ["a", "e", "g", "h"]
let arrayRemainingLetters = arrayLetters.filter {
!arrayRemoveLetters.contains(extension Array {
mutating func remove(at indices: [Int]) {
Set(indices)
.sorted(by: >)
.forEach { rmIndex in
self.remove(at: rmIndex)
}
}
}
)
}
print(arrayRemainingLetters)
//result - ["b", "c", "d", "f", "i"]
.element }
print(arrayRemainingAnimals)
//result - ["dogs", "chimps", "cow"]
.offset) }
.map { extension Array {
mutating func remove(at indexs: [Int]) {
guard !isEmpty else { return }
let newIndexs = Set(indexs).sorted(by: >)
newIndexs.forEach {
guard var array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
let indexSet = [3, 5, 8, 12]
indexSet.reversed().forEach{ array.remove(at: array.removeSubrange(1...3) /// Will remove the elements from 1, 2 and 3 positions.
) }
print(array)
< count, extension Array {
mutating func remove(at indexes: IndexSet) {
indexes.reversed().forEach{ self.remove(at: ##代码##) }
}
}
>= 0 else { return }
remove(at: ##代码##)
}
}
}
var arr = ["a", "b", "c", "d", "e", "f"]
arr.remove(at: [2, 3, 1, 4])
result: ["a", "f"]
.element }
print(numbers)
//result - [0, 1, 2, 4, 6, 7, 9, 10, 11]
.offset) }
.map { ##代码##.element }
print(arrayRemainingAnimals)
//result - ["dogs", "chimps", "cow"]
回答by Krunal
Remove elements using indexes of an array elements:
使用数组元素的索引删除元素:
Array of Strings and indexes
##代码##Array of Integers and indexes
##代码##
字符串和索引数组
##代码##整数和索引数组
##代码##
Remove elements using element value of another array
使用另一个数组的元素值删除元素
Arrays of integers
##代码##Arrays of strings
##代码##
整数数组
##代码##字符串数组
##代码##
回答by Kamil Harasimowicz
Simple and clear solution, just Array
extension:
简单明了的解决方案,只是Array
扩展:
Set(indices)
- ensures uniqueness.sorted(by: >)
- function removes elements from last to first, so during removal we are sure that indexes are proper
Set(indices)
- 确保唯一性.sorted(by: >)
- 函数从最后一个到第一个删除元素,因此在删除过程中我们确保索引是正确的
回答by linh luu phuoc
Swift 4
斯威夫特 4
##代码##回答by TheTiger
You can make a set of indexes you want to remove.
您可以制作一组要删除的索引。
##代码##Output: [0, 1, 2, 4, 6, 7, 9, 10, 11]
输出:[0, 1, 2, 4, 6, 7, 9, 10, 11]
In case indexes are continuous then use removeSubrange
如果索引是连续的,则使用 removeSubrange
回答by vadian
According to the NSMutableArray
API I recommend to implement the indexes as IndexSet
.
根据NSMutableArray
API,我建议将索引实现为IndexSet
.
You just need to inverse the order.
你只需要颠倒顺序。
##代码##Please see also this answerproviding a more efficient algorithm.
另请参阅此答案,提供更有效的算法。