【问题标题】:Compare Two Arrays That include a Dictionary and an Array比较包含字典和数组的两个数组
【发布时间】:2020-04-10 05:52:38
【问题描述】:

我有以下数组,其中包含用户搜索的产品;一旦搜索到,产品的标签也会存储在 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 数组?

【问题讨论】:

  • 到目前为止你有什么尝试?
  • 我尝试做for tag in productTags.values 并且我做了另一个for tag in searchedTags.values 并尝试一个一个匹配然后添加到一个数组并计数,但它似乎不正确或工作。我不知道该怎么做,因为我在这里也是一个新手。任何指导将不胜感激。
  • @JoakimDanielson 我刚刚用我尝试的方法编辑了我的问题;这是正确的方法吗?或者有更好的方法吗?
  • 我查看了发布的代码,但问题中其他地方没有提到集合,很难理解所有集合中的内容,包括键和值,所以我放弃了。
  • @JoakimDanielson 感谢您的尝试。我想出了一个解决方案,不得不更改我的一些编码;发布我结束她的内容,以防对其他人有所帮助。

标签: arrays swift dictionary arraylist comparison


【解决方案1】:

这是我修复它的方法。不得不改变我的代码相当多。

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{ $0.value > $1.value }
    var productSortIndex: Int = 0

    for (k,v) in sortedProductTagCountVar {
        productSortIndex = productSortIndex + 1
        sortedProductTagCount[productSortIndex, default: [:]][k] = v
    }

【讨论】:

    猜你喜欢
    • 2014-02-08
    • 1970-01-01
    • 2015-01-17
    • 2012-05-10
    • 2021-11-01
    • 2015-03-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多