比较2个数组并列出差异 - Swift

时间:2022-11-29 20:33:09

I was wondering how one would go about comparing 2 boolean arrays and listing the non matching booleans.

我想知道如何比较2个布尔数组并列出非匹配的布尔值。

I have written up a simple example of 2 arrays.

我写了一个2个数组的简单例子。

let array1 = [true, false, true, false]
let array2 = [true, true, true, true]

How would I compare array1 & array2 and display the non matching. I am trying to do this to check user results for a quiz game.

我如何比较array1和array2并显示不匹配。我试图这样做来检查测验游戏的用户结果。

Thanks!

2 个解决方案

#1


26  

Here's one implementation, but whether it is the one you are after is completely impossible to say, because you have not specified what you think the answer should be:

这是一个实现,但是它是否是你所追求的是完全不可能说,因为你没有指定你认为答案应该是:

let answer = zip(array1, array2).map {$0.0 == $0.1}

That gives you a list of Bool values, true if the answer matches the right answer, false if it does not.

这会给你一个Bool值列表,如果答案与正确答案匹配则为true,否则为false。

But let's say what you wanted was a list of the indexes of those answers that are correct. Then you could say:

但是,让我们说你想要的是那些正确答案的索引列表。然后你可以说:

let answer = zip(array1, array2).enumerated().filter() {
    $1.0 == $1.1
}.map{$0.0}

If you want a list of the indexes of those answers that are not correct, just change == to !=.

如果您想要那些不正确的答案的索引列表,只需将==更改为!=。

#2


1  

the easiest thing to do is use a Set. Sets have a symmetricDifference() method that does exactly this, so you just need to convert both arrays to a set, then convert the result back into an array.

最简单的方法是使用Set。集合有一个symmetricDifference()方法就是这样做的,所以你只需要将两个数组转换成一个集合,然后将结果转换回一个数组。

Here’s an extension to make it easier:

这是一个扩展,使其更容易:

extension Array where Element: Hashable {
    func difference(from other: [Element]) -> [Element] {
        let thisSet = Set(self)
        let otherSet = Set(other)
        return Array(thisSet.symmetricDifference(otherSet))
    } }

And here’s some example code you can use to try it out:

这里有一些示例代码可供您试用:

let names1 = ["student", "class", "teacher"]
let names2 = ["class", "Teacher", "classroom"]
let difference = names1.difference(from: names2)

That will set difference to be ["student", "classroom"], because those are the two names that only appear once in either array.

这将设置差异为[“学生”,“教室”],因为这两个名称只在一个数组中出现一次。

#1


26  

Here's one implementation, but whether it is the one you are after is completely impossible to say, because you have not specified what you think the answer should be:

这是一个实现,但是它是否是你所追求的是完全不可能说,因为你没有指定你认为答案应该是:

let answer = zip(array1, array2).map {$0.0 == $0.1}

That gives you a list of Bool values, true if the answer matches the right answer, false if it does not.

这会给你一个Bool值列表,如果答案与正确答案匹配则为true,否则为false。

But let's say what you wanted was a list of the indexes of those answers that are correct. Then you could say:

但是,让我们说你想要的是那些正确答案的索引列表。然后你可以说:

let answer = zip(array1, array2).enumerated().filter() {
    $1.0 == $1.1
}.map{$0.0}

If you want a list of the indexes of those answers that are not correct, just change == to !=.

如果您想要那些不正确的答案的索引列表,只需将==更改为!=。

#2


1  

the easiest thing to do is use a Set. Sets have a symmetricDifference() method that does exactly this, so you just need to convert both arrays to a set, then convert the result back into an array.

最简单的方法是使用Set。集合有一个symmetricDifference()方法就是这样做的,所以你只需要将两个数组转换成一个集合,然后将结果转换回一个数组。

Here’s an extension to make it easier:

这是一个扩展,使其更容易:

extension Array where Element: Hashable {
    func difference(from other: [Element]) -> [Element] {
        let thisSet = Set(self)
        let otherSet = Set(other)
        return Array(thisSet.symmetricDifference(otherSet))
    } }

And here’s some example code you can use to try it out:

这里有一些示例代码可供您试用:

let names1 = ["student", "class", "teacher"]
let names2 = ["class", "Teacher", "classroom"]
let difference = names1.difference(from: names2)

That will set difference to be ["student", "classroom"], because those are the two names that only appear once in either array.

这将设置差异为[“学生”,“教室”],因为这两个名称只在一个数组中出现一次。