lundi 26 avril 2021

Python pass condition formatting a dictionary [duplicate]

I am formatting a dictionary like so:

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

Now I'd like to pass a condition for adding the value, like: ...

 {'x': float(dataframe['x'][index]) if str(dataframe['home'][index] == 'home',
 'y': float(dataframe['y'][index]) if str(dataframe['home'][index] == 'away'}

But this syntax is not valid. Which one is the right one?

Aucun commentaire:

Enregistrer un commentaire