python - Python通过条件格式化字典
问题描述
我正在像这样格式化字典:
for index, team_id in dataframe.get('team_id').items():
format_redis[str(team_id)] = {
'x': float(dataframe['x'][index]),
'y': float(dataframe['y'][index]),
'z': float(dataframe['z'][index]),
'n': float(dataframe['n'][index])
}
现在我想传递一个添加值的条件,例如:...
{'x': float(dataframe['x'][index]) if str(dataframe['home'][index] == 'home',
'y': float(dataframe['y'][index]) if str(dataframe['home'][index] == 'away'}
但是这种语法是无效的。哪一个是正确的?
解决方案
推荐阅读
- ios - 数据包隧道提供程序 - iOS:TCP 连接被对等方关闭
- android - Android 底页总是贴在左上角
- docker - 获取经过身份验证的用户的 Docker 拉取列表
- android - 如何更改 Mesibo Call UI 背景颜色
- vue.js - 在 Vue 2 中,如何在两个组件之间传递变量?这仅适用于父子组件吗?
- spring - 在事务方法中返回 UpdatedEntity
- python - 获取元素的滚动高度或高度,而不是正文硒
- python - 根据两列的值计数选择行
- sql - 删除 sql 表上的内存优化并删除它创建的文件组
- asp.net-core - 在加密的 cookie 中缓存散列密码以连接旧版 API?