Multiply one dictionary by another in Swift

时间秒杀一切 提交于 2021-02-11 12:42:48

问题


Currently, I have a nested dictionary declared as :

let VitamineARetinol = "VitamineARetinol"
let VitamineABetacarotene = "VitamineABetacarotene"
let VitamineC = "VitamineC"
let VitamineD = "VitamineD"
let Calories = "Calories"

typealias ListOfAliment = [String: [String: Double]]

let Data: ListOfAliment = [
    "Orange": [VitamineARetinol: 0.5, VitamineC: 0.3, VitamineD: 0.98, Calories: 160],
    "Pomme": [VitamineARetinol: 0.2, VitamineC: 0.2, VitamineD: 0.38, Calories: 120],
    "Poire": [VitamineARetinol: 0.1, VitamineC: 0.7, VitamineD: 0.58, Calories: 140],
    "Laitue": [VitamineARetinol: 0.3, VitamineC: 0.1, VitamineD: 0.92, Calories: 105],
    "Banane": [VitamineARetinol: 0.7, VitamineC: 0.01, VitamineD: 0.63, Calories: 122],
]

And I need to divide each value with its corresponding value in the following dictionary:

let reference: ListOfAliment = [
    "reference": [VitamineARetinol: 0.2, VitamineC: 0.1, VitamineD: 0.4],
]

In the end, I should get a food dictionary where all the values ​​of each food have been divided by their corresponding value in the dictionary reference Please, could someone help me!


回答1:


Given my answer in your previous question you can simply map your array of values into a new array

let adjustedData = array
    .map { Aliment(name: $0.name, 
                   vitamineARetinol: $0.vitamineARetinol / 0.2, 
                   vitaminC: $0.vitaminC / 0.1, 
                   vitaminD: $0.vitaminD / 0.4, 
                   calories: $0.calories)}



回答2:


You should definitely create a structs for the inner dictionary. Because then you can add a dividedBy method in the struct to do the division. I'm not sure if it makes sense to "divide" one aliment by another aliment. If it does, you can change the divideBy method to accept another Aliment instead.

struct Aliment {
    let vitaminARetinol: Double
    let vitaminC: Double
    let vitaminD: Double
    let calories: Double
    
    func dividedBy(vitaminARetinolFactor: Double, vitaminCFactor: Double, vitaminDFactor: Double, caloriesFactor: Double) -> Aliment {
        Aliment(vitaminARetinol: vitaminARetinol / vitaminARetinolFactor,
                vitaminC: vitaminC / vitaminCFactor,
                vitaminD: vitaminD / vitaminDFactor,
                calories: calories / caloriesFactor)
    }
}
typealias ListOfAliment = [String: Aliment]
let data: ListOfAliment = [
    "Orange": Aliment(vitaminARetinol: 0.5, vitaminC: 0.3, vitaminD: 0.98, calories: 160),
    "Pomme": Aliment(vitaminARetinol: 0.2, vitaminC: 0.2, vitaminD: 0.38, calories: 120),
    "Poire": Aliment(vitaminARetinol: 0.1, vitaminC: 0.7, vitaminD: 0.58, calories: 140),
    "Laitue": Aliment(vitaminARetinol: 0.3, vitaminC: 0.1, vitaminD: 0.92, calories: 105),
    "Banane": Aliment(vitaminARetinol: 0.7, vitaminC: 0.01, vitaminD: 0.63, calories: 122),
]

To transform the dictionary, you can use mapValues:

let referenceNumbers = reference["reference"]!
let newData = data.mapValues { 
    $0.dividedBy(vitaminARetinolFactor: referenceNumbers[VitamineARetinol]!, 
                 vitaminCFactor: referenceNumbers[VitamineC]!, 
                 vitaminDFactor: referenceNumbers[VitamineD]!, 
                 caloriesFactor: referenceNumbers[Calories]!) 
}

Again, if it makes sense to divide one aliment by another, you can make the reference an Aliment object too, and pass it to divideBy.



来源:https://stackoverflow.com/questions/63354864/multiply-one-dictionary-by-another-in-swift

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!