xcode 基于另一个数组从数组中删除对象

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29612968/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 06:59:05  来源:igfitidea点击:

Removing objects from an array based on another array

arraysswiftxcode

提问by Henry Brown

I have two arrays like this:

我有两个这样的数组:

var arrayA = ["Mike", "James", "Stacey", "Steve"]
var arrayB = ["Steve", "Gemma", "James", "Lucy"]

As you can see, Jamesand Stevematch and I want to be able to remove them from arrayA. How would I write this?

正如你所看到的,JamesSteve比赛,我希望能够从删除它们arrayA。我会怎么写这个?

采纳答案by matt

Like this:

像这样:

var arrayA = ["Mike", "James", "Stacey", "Steve"]
var arrayB = ["Steve", "Gemma", "James", "Lucy"]
for word in arrayB {
    if let ix = find(arrayA, word) {
        arrayA.removeAtIndex(ix)
    }
}
// now arrayA is ["Mike", "Stacey"]

回答by Federico Zanetello

@francesco-vadicamo's answer in Swift 2/3/4+

@francesco-vadicamo 在Swift 中的回答2/3/4+

 arrayA = arrayA.filter { !arrayB.contains(
var setA = Set(arrayA)
var setB = Set(arrayB)

// Return a set with all values contained in both A and B
let intersection = setA.intersect(setB) 

// Return a set with all values in A which are not contained in B
let diff = setA.subtract(setB)
) }

回答by Antonio

The easiest way is by using the new Setcontainer (added in Swift 1.2 / Xcode 6.3):

最简单的方法是使用新Set容器(在 Swift 1.2 / Xcode 6.3 中添加):

arrayA = Array(intersection)

If you want to reassign the resulting set to arrayA, simply create a new instance using the copy constructor and assign it to arrayA:

如果要将结果集重新分配给arrayA,只需使用复制构造函数创建一个新实例并将其分配给arrayA

let res = arrayA.filter { !contains(arrayB, 
var arrayA = ["Mike", "James", "Stacey", "Steve"]
var arrayB = ["Steve", "Gemma", "James", "Lucy"]
for word in arrayB {
    if let ix = arrayA.index(of: word) {
        arrayA.remove(at: ix)
    }
}
) }

The downside is that you have to create 2 new data sets. Note that intersectdoesn't mutate the instance it is invoked in, it just returns a new set.

缺点是您必须创建 2 个新数据集。请注意,intersect它不会改变调用它的实例,它只会返回一个新集合。

There are similar methods to add, subtract, etc., you can take a look at them

还有类似的加减法等方法,大家可以看看

回答by Francesco Vadicamo

I agree with Antonio's answer, however for small array subtractions you can also use a filter closure like this:

我同意安东尼奥的回答,但是对于小数组减法,您也可以使用这样的过滤器闭包:

func -<T:RangeReplaceableCollectionType where T.Generator.Element:Equatable>( lhs:T, rhs:T ) -> T {

    var lhs = lhs
    for element in rhs {
        if let index = lhs.indexOf(element) { lhs.removeAtIndex(index) }
    }

    return lhs
}

回答by Ruiz

matt and freytag's solutions are the ONLY ones that account for duplicates and should be receiving more +1s than the other answers.

matt 和 freytag 的解决方案是唯一考虑重复的解决方案,并且应该比其他答案获得更多的 +1。

Here is an updated version of matt's answer for Swift 3.0:

这是马特对 Swift 3.0 的回答的更新版本:

arrayA - arrayB

回答by freytag

Original answer

原答案

This can also be implemented as a minus func:

这也可以实现为减函数:

func -<T: RangeReplaceableCollection>(lhs: T, rhs: T) -> T where T.Iterator.Element: Equatable {

    var lhs = lhs
    for element in rhs {
        if let index = lhs.firstIndex(of: element) { lhs.remove(at: index) }
    }

    return lhs
}

Now you can use

现在你可以使用

// Swift 3.x/4.x
func - <Element: Hashable>(lhs: [Element], rhs: [Element]) -> [Element]
{
    return Array(Set<Element>(lhs).subtracting(Set<Element>(rhs)))
}


Updated implementation for Swift 5

Swift 5 的更新实现

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(
let arrayResult = numbers.filter { element in
    return !indexesToRemove.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(
let arrayResult = numbers.filter { element in
    return !indexesToRemove.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(
/* poormans sub for Arrays */

extension Array where Element: Equatable {

    static func -=(lhs: inout Array, rhs: Array) {

        rhs.forEach {
            if let indexOfhit = lhs.firstIndex(of: ##代码##) {
                lhs.remove(at: indexOfhit)
            }
        }
    }

    static func -(lhs: Array, rhs: Array) -> Array {

        return lhs.filter { return !rhs.contains(##代码##) }
    }
}
) } print(arrayRemainingLetters) //result - ["b", "c", "d", "f", "i"]
.element } print(arrayRemainingAnimals) //result - ["dogs", "chimps", "cow"]
.offset) } .map { ##代码##.element } print(numbers) //result - [0, 1, 2, 4, 6, 7, 9, 10, 11]
.offset) } .map { ##代码##.element } print(arrayRemainingAnimals) //result - ["dogs", "chimps", "cow"]

回答by AmitaiB

Using the Array → Set → Arraymethod mentioned by Antonio, and with the convenience of an operator, as freytag pointed out, I've been very satisfied using this:

使用Array → Set → ArrayAntonio 提到的方法,并借助操作员的便利,正如 freytag 指出的那样,我对使用此方法感到非常满意:

##代码##

回答by Krunal

Remove elements using indexes array:

使用索引数组删除元素:

  1. Array of Strings and indexes

    ##代码##
  2. Array of Integers and indexes

    ##代码##
  1. 字符串和索引数组

    ##代码##
  2. 整数和索引数组

    ##代码##



Remove elements using element value of another array



使用另一个数组的元素值删除元素

  1. Arrays of integers

    ##代码##
  2. Arrays of strings

    ##代码##
  1. 整数数组

    ##代码##
  2. 字符串数组

    ##代码##

回答by JGS

For smaller arrays I use:

对于较小的数组,我使用:

##代码##