Arrays 如何比较swift中的两个词典,然后在新词典中添加任何相似之处?
我知道这似乎是一个令人困惑的问题,所以这里是我的场景。。。我有两个字典,我需要将它们组织到一个字典中,其中来自最初两个字典的键共享来自起始两个字典的相同键的相同值。例如:Arrays 如何比较swift中的两个词典,然后在新词典中添加任何相似之处?,arrays,swift,uitableview,sorting,dictionary,Arrays,Swift,Uitableview,Sorting,Dictionary,我知道这似乎是一个令人困惑的问题,所以这里是我的场景。。。我有两个字典,我需要将它们组织到一个字典中,其中来自最初两个字典的键共享来自起始两个字典的相同键的相同值。例如: var dict1 = [1: "fruit", 2: "vegetable", 3: "meat"] var dict2 = [2: "carrot", 3: "steak", 1: "apple", 3: "pork"] var newDict = [1: ["fruit": ["apple"]], 2: ["veget
var dict1 = [1: "fruit", 2: "vegetable", 3: "meat"]
var dict2 = [2: "carrot", 3: "steak", 1: "apple", 3: "pork"]
var newDict = [1: ["fruit": ["apple"]], 2: ["vegetable": ["carrot"]], 3: ["meat": ["steak, pork"]]]
因此,每个类别(水果、蔬菜、肉类)都有一个唯一的id(Int),并且在该类别中有一个适合该类别的值数组。最终的结果是,我希望使用该类别的as节头和该节数据的值数组,用该数据填充一个表视图
非常感谢您的帮助。谢谢:)这可能对你有帮助
var dict1 = [(0, ["sender": "user1", "time": NSDate(), "mId": "as2f2ASf"]), (1, ["sender": "user1", "time": NSDate(), "mId": "Sjf82FsJ"])]
var dict2 = [(0, ["sender": "user2", "time": NSDate(), "mId": "J2fAS92D"]), (1, ["sender": "user2", "time": NSDate(), "mId": "Sjf82FsJ"])]
var dict3 = [[0: ["sender": "user1", "time": NSDate(), "mId": "as2f2ASf"]], [1: ["sender": "user1", "time": NSDate(), "mId": "Sjf82FsJ"]]]for (_,value1) in dict1 {
if let mld1 = value1["mId"] {
for(_,value2) in dict2 {
if let mld2 = value2["mId"] {
if mld1 == mld2 {
println("Found diff")
}
}
}
}
}
正如我在评论中所说,你的第二本字典有重复的键,所以它是无效的 假设您通过用一个新的密钥替换重复密钥来解决重复密钥问题,这一次是唯一的,这里有一个快速算法几乎可以实现您想要的
let dict1 = [1: "fruit", 2: "vegetable", 3: "meat"]
let dict2 = [2: "carrot", 3: "steak", 1: "apple", 4: "pork"]
var newDict = [Int:[String:[String]]]()
for category in dict1 {
if let existingCat = newDict[category.key] {
} else {
let newCat = [category.value: [dict2[category.key]!]]
newDict[category.key] = newCat
}
}
print (newDict)
但是,它不会给出您想要的产品列表,因此我已将您的输入数据类型更改为更合适的类型(我不是说它是最佳的,但它是有效的):
最后,newDict打印:
[2: ["vegetable": ["carrot"]], 3: ["meat": ["steak", "pork"]], 1: ["fruit": ["apple"]]]
如果您将词典更正为:
let dict1 = [1: "fruit", 2: "vegetable", 3: "meat"]
var dict2 = [2: ["carrot"], 1: ["apple"], 3: ["pork", "steak"]]
你可以用
let result = dict1.keys.reduce([Int: [String: [String]]]()) { (result, key) in
var result = result
result.updateValue([dict1[key]!: dict2[key] ?? []], forKey: key)
return result
}
这将导致[2:[“蔬菜”:[“胡萝卜”]、3:[“肉”:[“猪肉”、“牛排”]、1:[“水果”:[“苹果”]]
您的代码无法编译
正如@Bogdan Farca在评论中指出的,由于重复键,您的第二本词典无法编译
let dict2 = [2: "carrot", 3: "steak", 1: "apple", 3: "pork"]
表示dict2
信息的更好方法是使用食品名称作为键
和类别ID作为值
let dict2 = ["carrot" : 2,"steak": 2,"apple": 1, "pork":3]
我假设食物的名字是独一无二的
更好的名字
为了使代码更具可读性,我们还应该使用更好的名称
let categories = [1: "fruit", 2: "vegetable", 3: "meat"]
let foods = ["carrot" : 2, "steak": 3, "apple": 1, "pork":3]
使用模型
我们终于可以集中精力解决问题了。
您希望像这样的输出[Int:Dictionary]
它是字典/数组的复杂组合,为什么不简单地使用一个模型值呢
struct FoodCategory {
let categoryID: Int
let categoryName: String
let foods: [String]
}
现在你可以写了
let foodCategories = categories.map { cat -> FoodCategory in
let foodNames = foods.filter { $0.value == cat.key }.map { $0.0 }
return FoodCategory(categoryID: cat.key, categoryName: cat.value, foods: foodNames )
}
这就是结果
[
FoodCategory(categoryID: 2, categoryName: "vegetable", foods: ["carrot"]),
FoodCategory(categoryID: 3, categoryName: "meat", foods: ["pork", "steak"]),
FoodCategory(categoryID: 1, categoryName: "fruit", foods: ["apple"])
]
您的初始数据有问题,
dict2
不是有效的字典,因为您有重复的密钥。但我没有发现任何错误?谢谢,我会尝试一下。我可能只是想得太多了@亲爱的,如果int键有意义,如果它代表了什么,为什么不呢。如果它只是OP问题中的一个索引,那么它就没有意义了。主要问题是我正在转换一些JSON来创建一个食品数据库,JSON的结构是这样的,比如说有5个类别,这些类别将有唯一的id(Int),然后你会有一种食物,它应该与以类似方式构造的类别相关联,这样每个食物都有一个唯一的id(int)这将等于一个食品类别id。我从两个独立的文件中提取JSON,并将食品存储到一个字典中,然后使用适当的id将类别存储到另一个字典中。你真的帮了大忙。非常感谢:)
[
FoodCategory(categoryID: 2, categoryName: "vegetable", foods: ["carrot"]),
FoodCategory(categoryID: 3, categoryName: "meat", foods: ["pork", "steak"]),
FoodCategory(categoryID: 1, categoryName: "fruit", foods: ["apple"])
]