Home > OS >  How to loop dictionary keys?
How to loop dictionary keys?

Time:12-19

does anyone know how to generalize my code so that I don't have to use 3 different functions to calculate 'fluctuations' the keys 'NOMANSLAND' 'PICTUREBAY' and 'COVENSTEAD' are from a csv file. (already read the csv file and stored it as a dictionary in 'gegevens')

my code

CodePudding user response:

In every function, the only change is the actual index of gemiddeldes[index]. So you could create one function that takes the index parameter to simplify your code :

fluctuaties = bereken_stijging_daling (gegevens, gemiddeldes, index=1)
fluctuaties2 = bereken_stijging_daling(gegevens, gemiddeldes, index=2)
fluctuaties3 = bereken_stijging_daling(gegevens, gemiddeldes, index=3)

CodePudding user response:

This should work

name = {'s':1,'k':3}
for i in name:
    print(i)
  • Related