有没有一种简单的方法来对 NSDictionary 进行洗牌?

huangapple go评论56阅读模式
英文:

Is there an easy way to shuffle an NSDictionary?

问题

以下是翻译好的内容:

我正在创建一个问答应用,我有一个NSDictionary,其中键是问题,值为true或false(答案)。我想要从这个NSDictionary中随机选择5个值。如何对NSDictionary进行随机洗牌以随机选择值?

这是NSDictionary:

let questions: NSDictionary = [
    "习惯是一种被重复多次的行为,以至于变得自动化。": true,
    "在对话中大多数时间都谈论自己是影响他人的好方法。": false,
    "在《高效能人士的七个习惯》中,影响圈包括个体可控制的所有事物。": true,
    "在马尔科姆·格拉德威尔的《异类》中,作者指出,你练习的小时数比天赋更重要。": true,
    "拿破仑·希尔的《思考致富》是基于任何人如果拥有正确的心态就可以取得成功的理念。": true
];
英文:

I am creating a quiz app and I have an NSDictionary with the keys as questions and values as true or false (the answers). I want to randomly select 5 values from this NSDictionary. How can I shuffle the NSDictionary to select values at random?

Here's the NSDictionary

let questions: NSDictionary = [
    "A habit is a behavior that has been repeated enough times to become automatic.": true,
    "Spending a conversation talking mostly about yourself is a good way to influence people.": false,
    "In the 7 Habits of Highly Effective People, the Circle of Influence is all of the things inside an individual's control.": true,
    "In The Outliers by Malcolm Gladwell, the author states that the number of hours you practice is more important than your natural talent": true,
    "Think and Grow Rich by Napoleon Hill is based on the idea that anyone can achieve success if they have the right mindset.": true
];

答案1

得分: 5

使用 Swift 中的 Dictionary 而不是 NSDictionary,此代码实现了您所要求的功能 - 随机选择 5 个问题(您的示例只有 5 个问题,所以它们都被选择了):

let questions: Dictionary = [
    "A habit is a behavior that has been repeated enough times to become automatic.": true,
    "Spending a conversation talking mostly about yourself is a good way to influence people.": false,
    "In the 7 Habits of Highly Effective People, the Circle of Influence is all of the things inside an individual's control.": true,
    "In The Outliers by Malcolm Gladwell, the author states that the number of hours you practice is more important than your natural talent": true,
    "Think and Grow Rich by Napoleon Hill is based on the idea that anyone can achieve success if they have the right mindset.": true
]

let randomQuestions = questions.keys.shuffled().prefix(5)

for question in randomQuestions {
    // 可以安全地强制解包答案,因为我们知道 `question` 存在于键中
    let answer = questions[question]
    print("Q: \(question), A: \(answer)")
}

正如其他人评论的那样,对于这个问题,使用字典可能不是最佳的数据结构。我认为更好的解决方案是使用 Question 数组,以获得更好的类型安全性和更可读的代码:

struct Question {
    let title: String
    let answer: Bool
}

let questions = [
    Question(title: "A habit is a behavior that has been repeated enough times to become automatic.", answer: true),
    Question(title: "Spending a conversation talking mostly about yourself is a good way to influence people.", answer: false),
    Question(title: "In the 7 Habits of Highly Effective People, the Circle of Influence is all of the things inside an individual's control.", answer: true),
    Question(title: "In The Outliers by Malcolm Gladwell, the author states that the number of hours you practice is more important than your natural talent", answer: true),
    Question(title: "Think and Grow Rich by Napoleon Hill is based on the idea that anyone can achieve success if they have the right mindset.", answer: true)
]

let randomQuestions = questions.shuffled().prefix(5)

for question in randomQuestions {
    print("Q: \(question.title), A: \(question.answer)")
}
英文:

Using a Swift Dictionary instead of NSDictionary this code does what you have asked - randomly chooses 5 questions (your example only has 5 questions so they are all chosen):

let questions: Dictionary = [
    "A habit is a behavior that has been repeated enough times to become automatic.": true,
    "Spending a conversation talking mostly about yourself is a good way to influence people.": false,
    "In the 7 Habits of Highly Effective People, the Circle of Influence is all of the things inside an individual's control.": true,
    "In The Outliers by Malcolm Gladwell, the author states that the number of hours you practice is more important than your natural talent": true,
    "Think and Grow Rich by Napoleon Hill is based on the idea that anyone can achieve success if they have the right mindset.": true
]

let randomQuestions = questions.keys.shuffled().prefix(5)

for question in randomQuestions {
    // It is safe to force unwrap the answer because we know that `question` exists in the keys
    let answer = questions[question]
    print("Q: \(question), A: \(answer)")
}

As other people have commented, a dictionary is possibly not the best data structure to use for this problem. I think a better solution would be to use an array of Questions for proper type safety and more readable code:

struct Question {
    let title: String
    let answer: Bool
}

let questions = [
    Question(title: "A habit is a behavior that has been repeated enough times to become automatic.", answer: true),
    Question(title: "Spending a conversation talking mostly about yourself is a good way to influence people.", answer: false),
    Question(title: "In the 7 Habits of Highly Effective People, the Circle of Influence is all of the things inside an individual's control.", answer: true),
    Question(title: "In The Outliers by Malcolm Gladwell, the author states that the number of hours you practice is more important than your natural talent", answer: true),
    Question(title: "Think and Grow Rich by Napoleon Hill is based on the idea that anyone can achieve success if they have the right mindset.", answer: true)
]

let randomQuestions = questions.shuffled().prefix(5)

for question in randomQuestions {
    print("Q: \(question.title), A: \(question.answer)")
}

huangapple
  • 本文由 发表于 2023年4月7日 00:21:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/75951676.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定