gpt4 book ai didi

ios - Swift 排名字典

转载 作者:搜寻专家 更新时间:2023-11-01 06:25:23 25 4
gpt4 key购买 nike

我能够对字符串和整数字典进行排名。但我的解决方案看起来并不聪明和“敏捷”

问题是当更多的团队与“团队 2”和“团队 4”具有相同的积分和相同的等级时

ex dic 输入

var dict:[String:Int] = ["team1":79,"team2":5, "team3":18, "team4":5, "team5": 82, "team6":1]

输出

[(team: "team5", rank: 1), (team: "team1", rank: 2), (team: "team3", rank: 3), (team: "team2", rank: 4), (team: "team4", rank: 4), (team: "team6", rank: 5)]

代码:

var ris = [(team:String,rank:Int)]()
var pos = 1

let sorted = dict.sorted(by:{$0.value > $1.value})
print(sorted)
for (i, element) in sorted.enumerated() {

if i == 0 || element.value == sorted[i-1].value {

}
else {
pos += 1
}
ris.append((team:element.key,rank:pos))
}
let ranking = ris.sorted(by:{$0.rank < $1.rank})
print(ranking)

打印:

[(team: "team5", rank: 1), (team: "team1", rank: 2), (team: "team3", rank: 3), (team: "team2", rank: 4), (team: "team4", rank: 4), (team: "team6", rank: 5)]

好的,它可以工作,但我确信我错过了一些更好的东西,使用一些排序的闭包, map flapmaps 等

有人吗?

最佳答案

您可以通过先在字典中对分数相同的团队进行分组来稍微简化一下。然后对字典进行排序(根据分数递减),枚举它(以获得偏移量)并构建排名列表:

let dict:[String:Int] = ["team1":79, "team2":5, "team3":18, "team4":5, "team5": 82, "team6": 1]

let ranking = Dictionary(grouping: dict, by: { $0.value })
.sorted(by: { $0.key > $1.key })
.enumerated()
.flatMap { (offset, elem) in
elem.value.map { (team: $0.key, rank: offset + 1 )}
}

print(ranking)
// [(team: "team5", rank: 1), (team: "team1", rank: 2),
// (team: "team3", rank: 3), (team: "team2", rank: 4),
// (team: "team4", rank: 4), (team: "team6", rank: 5)]]

详细解释:

Dictionary(grouping: dict, by: { $0.value })

创建一个字典,其键是团队分数,其值是具有该分数的团队的数组。

.sorted(by: { $0.key > $1.key })

按键降序对字典进行排序,结果是一个元组数组:

 [(key: 82, value: [(key: "team5", value: 82)]),
(key: 79, value: [(key: "team1", value: 79)]),
(key: 18, value: [(key: "team3", value: 18)]),
(key: 5, value: [(key: "team2", value: 5), (key: "team4", value: 5)]),
(key: 1, value: [(key: "team6", value: 1)])]

然后

.enumerated()

从这个数组中创建一个(偏移量,元素)对的惰性序列:

  (offset: 0, element: (key: 82, value: [(key: "team5", value: 82)])),
(offset: 1, element: (key: 79, value: [(key: "team1", value: 79)])),
(offset: 2, element: (key: 18, value: [(key: "team3", value: 18)])),
(offset: 3, element: (key: 5, value: [(key: "team2", value: 5), (key: "team4", value: 5)])),
(offset: 4, element: (key: 1, value: [(key: "team6", value: 1)]))

最后flatMap在每个 (offset, element) 对上调用闭包并连接结果。在闭包内,

 elem.value.map { (team: $0.key, rank: offset + 1 )}

映射一个(偏移量,元素)对和一个(团队,等级)元组数组。例如,

  (offset: 3, element: (key: 5, value: [(key: "team2", value: 5), (key: "team4", value: 5)]))

映射到

 [(team: "team2", rank: 4), (team: "team4", rank: 4)]

flatMap()连接这些数组,给出最终的 ranking数组。


这是最初发布的解决方案,它为示例数据生成排名 1、2、3、4、4、6(而不是 1、2、3、4、4、5):

let dict:[String:Int] = ["team1":79, "team2":5, "team3":18, "team4":5, "team5": 82, "team6": 1]

var ranking = [(team:String,rank:Int)]()
for (_, list) in Dictionary(grouping: dict, by: { $0.value })
.sorted(by: { $0.key > $1.key }) {
let pos = ranking.count + 1
ranking.append(contentsOf: list.map { ($0.key, pos )})
}

print(ranking)
// [(team: "team5", rank: 1), (team: "team1", rank: 2),
// (team: "team3", rank: 3), (team: "team4", rank: 4),
// (team: "team2", rank: 4), (team: "team6", rank: 6)]

关于ios - Swift 排名字典,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56513736/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com