简体   繁体   English

如何将数组排序为字典?

[英]How can I sort an array into a dictionary?

Let's say I have an array which contain multiple country names such as "Australia, Denmark, United Kingdom, Austria, Australia, Denmark" some of the country names appear twice. 假设我有一个包含多个国家名称的数组,例如“澳大利亚,丹麦,英国,奥地利,澳大利亚,丹麦”,其中一些国家名称出现了两次。

How can I sort them to form a dictionary based on country names. 如何将它们分类以根据国家/地区名称形成字典。 So they key would be the country name and the element would be the country. 因此,它们的键将是国家名称,而元素将是国家。

If I have two countries in my array that are the same, the key would be the country and the elements would be those two countries. 如果我在数组中有两个相同的国家,则关键将是该国家,要素将是这两个国家。

I need it so that if I add another country it will make a key for the country without having to specify keys beforehand. 我需要它,以便如果添加另一个国家/地区,它将为该国家/地区创建密钥,而无需事先指定密钥。

Each country needs to be under a key of it's country, not dependent on the occurrences of the country in the array. 每个国家/地区都必须位于其国家/地区的键之下,而不取决于阵列中该国家/地区的出现情况。

I think I've worked out a basic algorithm for it but I can't seem to put it into practice. 我想我已经为它制定了一个基本算法,但是我似乎无法将其付诸实践。

  1. While enumerating over the array 在枚举数组时
  2. check to see if a key in the dictionary matches the current string 检查字典中的键是否与当前字符串匹配
  3. If it does, add the string to the dictionary under the matching key 如果是这样,请将字符串添加到匹配键下的字典中
  4. If it doesn't create a key and place the string under the key. 如果没有创建密钥,则将字符串放置在密钥下方。

Is this algorithm correct or at least a step in the right direction? 该算法正确还是至少朝正确方向迈出了一步?

Thanks for the help. 谢谢您的帮助。

EDIT: We have an array which contains the country names "Australia, Denmark, United Kingdom, Austria, Australia, Denmark" 编辑:我们有一个包含国家名称“澳大利亚,丹麦,英国,奥地利,澳大利亚,丹麦”的数组

I need to organise this into a dictionary based on countries so as we have two of the country Denmark in the array I need to sort it so it looks like this: 我需要将其组织成一个基于国家/地区的字典,因为数组中有两个国家/地区丹麦,所以我需要对其进行排序,如下所示:

Denmark: "Denmark", "Denmark"

The key is the country name and the element is the string. 关键是国家名称,元素是字符串。

United Kingdom only occurs once so that part of the dictionary will look like this: 英国仅出现一次,因此字典的一部分将如下所示:

United Kingdom: "United Kingdom"

I hope I've made more sense. 我希望我有更多的道理。

Not sure if this is what you meant. 不知道这是否是您的意思。 It's not very clear. 不太清楚。

var dict = [String: [String]]()
let countries = ["Holland", "England", "France", "Belgium", "England"]

for country in countries {
    dict[country] = (dict[country] ?? []) + [country]
}

for (key, value) in dict {
    println("KEY: \(key) & VALUE: \(value)")
}

Output: 输出:

KEY: England & VALUE: [England, England]
KEY: Belgium & VALUE: [Belgium]
KEY: Holland & VALUE: [Holland]
KEY: France & VALUE: [France]

EDIT: 编辑:
Simplified based on Martin R's link in his comment. 根据Martin R在他的评论中的链接进行了简化。

The simplest way is to just loop over the array and check if the key exists in the dictionary. 最简单的方法是仅遍历数组并检查键是否存在于字典中。 Make each value of the dictionary an NSMutableArray. 使字典的每个值成为NSMutableArray。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM