在Swift中将一本字典与另一本字典相乘 [英] Multiply one dictionary by another in Swift

查看:78
本文介绍了在Swift中将一本字典与另一本字典相乘的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前,我有一个嵌套的字典声明为:

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!

推荐答案

在您的上一个问题中给出我的答案只需将值数组映射到新数组

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)}

这篇关于在Swift中将一本字典与另一本字典相乘的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆