比较包含字典和数组的两个数组
Compare Two Arrays That include a Dictionary and an Array
我有以下数组,其中包含用户搜索的产品;搜索后,产品的标签也存储在 searchTags 数组中,如下所示:
searchedTags = [ [product : [tagA, tagB, tagC, tagD, tagH ]] ]
现在我有一个包含每个产品不同标签的所有产品的数组列表:
productTagsArray = [ [product1 : [tagA, tagB, tagC, tagD]],
[product2 : [tagC, tagD, tagE, tagF, tag H]],
[product3 : [tagH, tagI, tagJ]],
[product4 : [tagK, tagL, tagM]],
...
]
现在我想检查并比较 searchedTags
中搜索产品的标签与 productTagsArray
中的每个产品。比较后,我想制作一个新的产品数组,按与搜索产品匹配(从高到低)的标签计数排序。如果没有匹配的标签,我不想将它们包含在新变量中。我想像下面这样填充这个排序匹配的结果:
sortedProductsByCount = [[productId : product1, numberOfTagsMatched : 4],
[productId : product2, numberOfTagsMatched : 2],
[productId : product1, numberOfTagsMatched : 1]
]
编辑:
这就是当用户点击 tableview 中的搜索结果时我写的内容:
var productsTagCount: [[String:Any]] = [[:]]
for tags in searchedProductTags {
for tag in tags {
for productArray in productTagsArray {
for product in productArray {
var tagCount: Int = 0
for productTag in product.value {
if productTag == tag {
tagCount = tagCount + 1
}
}
let data: [String : Any] = [
"productId": product.key,
"tagCount": tagCount
]
productsTagCount.append(data)
}
}
}
}
有没有更好的方法?如何完成 sortedProductByCount
数组?
这是我修复它的方法。不得不大量更改我的代码。
var productTagsArray : [[String : [String]]] = [[:]]
var productTagCount : [String : Int] = [:]
var sortedProductTagCount: [Int : [String : Int]] = [:]
let searchedProductTags = searchResults[indexPath.row].values
for productArray in productTagsArray {
for product in productArray {
let productId: String = product.key
var tagCount: Int = 0
for productTag in product.value {
for searchedTags in searchedProductTags {
for searchedTag in searchedTags {
if productTag == searchedTag {
tagCount = tagCount + 1
}
}
}
}
if tagCount != 0 {
productTagCount[productId] = tagCount
}
}
}
//Make an array with sorted tagscount
let sortedProductTagCountVar = productTagCount.sorted{ [=10=].value > .value }
var productSortIndex: Int = 0
for (k,v) in sortedProductTagCountVar {
productSortIndex = productSortIndex + 1
sortedProductTagCount[productSortIndex, default: [:]][k] = v
}
我有以下数组,其中包含用户搜索的产品;搜索后,产品的标签也存储在 searchTags 数组中,如下所示:
searchedTags = [ [product : [tagA, tagB, tagC, tagD, tagH ]] ]
现在我有一个包含每个产品不同标签的所有产品的数组列表:
productTagsArray = [ [product1 : [tagA, tagB, tagC, tagD]],
[product2 : [tagC, tagD, tagE, tagF, tag H]],
[product3 : [tagH, tagI, tagJ]],
[product4 : [tagK, tagL, tagM]],
...
]
现在我想检查并比较 searchedTags
中搜索产品的标签与 productTagsArray
中的每个产品。比较后,我想制作一个新的产品数组,按与搜索产品匹配(从高到低)的标签计数排序。如果没有匹配的标签,我不想将它们包含在新变量中。我想像下面这样填充这个排序匹配的结果:
sortedProductsByCount = [[productId : product1, numberOfTagsMatched : 4],
[productId : product2, numberOfTagsMatched : 2],
[productId : product1, numberOfTagsMatched : 1]
]
编辑: 这就是当用户点击 tableview 中的搜索结果时我写的内容:
var productsTagCount: [[String:Any]] = [[:]]
for tags in searchedProductTags {
for tag in tags {
for productArray in productTagsArray {
for product in productArray {
var tagCount: Int = 0
for productTag in product.value {
if productTag == tag {
tagCount = tagCount + 1
}
}
let data: [String : Any] = [
"productId": product.key,
"tagCount": tagCount
]
productsTagCount.append(data)
}
}
}
}
有没有更好的方法?如何完成 sortedProductByCount
数组?
这是我修复它的方法。不得不大量更改我的代码。
var productTagsArray : [[String : [String]]] = [[:]]
var productTagCount : [String : Int] = [:]
var sortedProductTagCount: [Int : [String : Int]] = [:]
let searchedProductTags = searchResults[indexPath.row].values
for productArray in productTagsArray {
for product in productArray {
let productId: String = product.key
var tagCount: Int = 0
for productTag in product.value {
for searchedTags in searchedProductTags {
for searchedTag in searchedTags {
if productTag == searchedTag {
tagCount = tagCount + 1
}
}
}
}
if tagCount != 0 {
productTagCount[productId] = tagCount
}
}
}
//Make an array with sorted tagscount
let sortedProductTagCountVar = productTagCount.sorted{ [=10=].value > .value }
var productSortIndex: Int = 0
for (k,v) in sortedProductTagCountVar {
productSortIndex = productSortIndex + 1
sortedProductTagCount[productSortIndex, default: [:]][k] = v
}