首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何比较数组和嵌套数组,并打印匹配的对,而不是匹配的值列表?(代码有助于解释)

问如何比较数组和嵌套数组,并打印匹配的对,而不是匹配的值列表?(代码有助于解释)
EN

Stack Overflow用户
提问于 2020-04-11 18:55:49
回答 2查看 223关注 0票数 1

我不太清楚该如何把它写成文字,但是代码应该会有所帮助。

代码语言:javascript
复制
var People = [

      .init(name: "Sam", friends: ["James", "Tom", "Rick"]),
      .init(name: "Tom", friends: ["Callum", "Steve", "Sam"]),
      .init(name: "Adam", friends: ["Harry", "Diane", "William"]),
      .init(name: "William", friends: ["Rodger", "Adam", "Bill"]),
      .init(name: "Guy", friends: ["Zack", "Frank", "Cody"])
]

我想要生成一个警报

代码语言:javascript
复制
" Sam is in the same social circle as Tom " / " Tom is in the same social circle as Sam "

和

代码语言:javascript
复制
" Adam is in the same social circle as William " / " William is in the same social circle as Adam "

和

代码语言:javascript
复制
" Guy has no common social circles "

我尝试的方法是首先创建一个名称数组: String,然后对朋友执行同样的操作,使用以下函数将[ String ]转换为String.

代码语言:javascript
复制
func loadNamesArray() -> [String] {
    return self.People.map { $0.name }
}

func loadFriendsArray() -> [String] {
    let arrayOfFriends = self.People.map { $0.friends }
    let friends = arrayOfFriends.flatMap { $0 }
    return friends
}

然后我交叉比较了这两个数组,以提醒使用此函数的普通用户.

代码语言:javascript
复制
func loadCommonInteractingArray() -> [String] {
    let output = loadNamesArray().filter{ loadFriendsArray().contains($0) }

    return output
}

但这只会返回"Sam“、"Tom”、"Adam“、"William",我不知道从哪里可以得到我想要的结果,即识别匹配对,而不是仅仅列出那些匹配的。

谢谢

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2020-04-11 19:28:02

代码语言:javascript
复制
struct Person {
    let name: String
    let friends: [String]
}

代码语言:javascript
复制
var people: [Person] = [
      .init(name: "Sam", friends: ["James", "Tom", "Rick"]),
      .init(name: "Tom", friends: ["Callum", "Steve", "Sam"]),
      .init(name: "Adam", friends: ["Harry", "Diane", "William"]),
      .init(name: "William", friends: ["Rodger", "Adam", "Bill"])]

func loadCommonInteracting(in people: [Person]) -> [String] {
    var connections: [String] = []
    people.forEach { person in
        let friends = people.filter{ $0.friends.contains(person.name) }
        for friend in friends {
            connections.append("\(person.name) is in the same social circle as \(friend.name)")
        }
        
    }
    return connections
}

代码语言:javascript
复制
let connections = loadCommonInteracting(in: people)
for connection in connections {
    print(connection)
}

这个会打印出来

Sam和汤姆在同一个社交圈里

汤姆和萨姆在同一个社交圈里。

亚当和威廉在同一个社交圈里。

威廉和亚当在同一个社交圈里。

票数 2
EN

Stack Overflow用户

发布于 2020-04-11 19:22:04

据我所知,您正在尝试查找任何其他人的friends数组中包含的每个人。

代码语言:javascript
复制
for person in people {
    let others = people.filter {$0 != person}

    for other in others {
        if other.friends.contains(person.name) {
            print("\(person.name) is in the same social circle as \(other.name)")
        }
    }
}

People模型类必须符合Equatable才能完成上述操作。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61162103

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档