首页 > 解决方案 > 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'}

但是这种语法是无效的。哪一个是正确的?

标签: pythonif-statement

解决方案


推荐阅读