python - Python - Merge two dictionaries into a single list (By Keys)
问题描述
I have:
market = {
"APPL" : 2.33 },
"IBM" : 3.44 },
"AMZN" : 5.33 }
}
portfolio = {
"APPL" : 0.20,
"IBM" : 0.05
}
I want to merge the above dictionaries into a single list, that has the following structure. It uses common keys to do the multiplication:
index 0: Multiplication of 2.33 and 0.20
index 1: Multiplication of 3.44 and 0.05
Any ideas?
解决方案
You could iterate through the items of portfolio like this:
[value * market[key] for key, value in portfolio.items()]
or, if you wanted to keep the keys:
{key: value * market[key] for key, value in portfolio.items()}
Edit: I missed that you wanted a list, not a dict
推荐阅读
- spring - 如何测试 Spring 事务
- javascript - 加载 gif 直到页面完全加载
- linux - 我可以捕获我启动的另一个进程的输出吗?
- python - matplotlib set_major_formatter 创建 2 个图
- redis - @EnableRedisRepositories - 在 Spring Data Redis 中有什么用?
- asp.net-mvc - ASP.NET MVC 应用程序池不适用于部署
- oracle - 在 oracle 中插入选择失败
- windows - 如何使用 PowerShell 判断是 SAN 还是本地磁盘存储类型?
- matlab - 构造一个形成链的递归,直到不能添加新元素
- android - 在框架布局顶部添加按钮