[英]How do i compare two dictionaries in swift and then append any similarities to a new dictionary?
我知道這可能看起來像一個令人困惑的問題,所以這是我的場景...我有兩個字典,我需要將它們組織成一個字典,其中來自兩個最初的2個字典的鍵共享來自相同鍵的相同值從2開頭的詞典開始。 例:
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),並且在該類別中是適合該類別的一系列值。 最終的結果是,我希望使用類別作為節標題和該節數據的值數組來填充包含此數據的表視圖。
任何幫助深表感謝。 謝謝 :)
正如我在評論中所說,你的第二本字典有重復的鍵,所以它不是Swift的有效。
假設您通過用新的替換副本來解決重復鍵問題,這次是唯一的,這是一個快速算法,幾乎達到了你想要的。
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)
它不會給你你想要的產品列表,所以我把你的輸入數據類型更改為更合適的東西(我不是說它是最優的,但是它有效):
let input1 = [(1, "fruit"), (2, "vegetable"), (3, "meat")]
let input2 = [(2, "carrot"), (3, "steak"), (1, "apple"), (3, "pork")]
var newDict = [Int:[String:[String]]]()
for category in input1 {
newDict[category.0] = [category.1:[]]
}
for meal in input2 {
if let existingCat = newDict[meal.0]?.first {
newDict[meal.0] = [existingCat.key: existingCat.value + [meal.1]]
}
}
print (newDict)
最后newDict
打印:
[2: ["vegetable": ["carrot"]], 3: ["meat": ["steak", "pork"]], 1: ["fruit": ["apple"]]]
正如@Bogdan Farca在評論中指出的那樣,由於重復鍵,你的第二本字典將無法編譯
let dict2 = [2: "carrot", 3: "steak", 1: "apple", 3: "pork"]
表示dict2
信息的更好方法是使用食物名稱作為key
,使用類別ID作為value
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<String, Array<String>>]
它是字典/數組的復雜組合,為什么不簡單地使用模型值?
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"])
]
如果你更正你的詞典:
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: ["vegetable": ["carrot"]], 3: ["meat": ["pork", "steak"]], 1: ["fruit": ["apple"]]]
這可能對你有所幫助
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")
}
}
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.