I want to use a for loop to create newly named variables from the keys, which can then be retrieved globally down the road. To define the variable before with None
, has unfortunately brought nothing. Also, with Global
I did not get further, but possibly I used it also wrong. I have googled a lot, but unfortunately I couldn’t get hold of the solution. Is there any trick how I can retrieve the new variables in the later course?
JavaScript
x
16
16
1
LAeq = { 'Januar': [69.1, 57.9, 58.3, 55.6],
2
'Februar': [66.7, 65.5, 63.5, 62.4] }
3
4
anzahl_Januar = None #anzahl is number in English
5
print ("{:<9} {:<5} {:<9}".format('Monat','Tage','Prozent'))
6
7
for key in LAeq.keys():
8
for value in LAeq[key]:
9
anzahl_key = 0
10
for i in LAeq[key]: #LAeq['Januar']
11
if i > 63 :
12
anzahl_key = anzahl_key + 1
13
print("{:<9} {:<5} {:<9}".format(key, anzahl_key, "{:.1%}".format(anzahl_key/len(LAeq[key]))))
14
15
print(anzahl_Januar)
16
Obviously the dictionary is much larger, I just put down an example here. I am thankful for any advice! :)
Advertisement
Answer
You can use the exec command, like this:
JavaScript
1
7
1
dict={}
2
for i in range (10):
3
key=str("x"+str(i))
4
dict[key]=i
5
for key,value in dict.items():
6
exec(f'{key}={value}')
7
Try adjusting this to your data.