Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Editing Dictionaries
#6
Thanks Guys,
I am slowly learning!
I have removed the parentheses (j.crater), and also adopted ichabod801's suggestion to combine the three dictionaries.

A = {'Bi' : 0,'Of' : 0,'Spd' : 0}
E = {'Bi' : 0,'Of' : 0,'Spd' : 0}
U = {'Bi' : 0,'Of' : 0,'Spd' : 0}


Ab = 24
Ao = 41
Eb = 67
Eo = 80
Ub = 15
Uo = 30


Pairs = {'A': A, 'E': E, 'U': U}
print(Pairs)

for key in Pairs:
    Pairs[key]['Bi'] = key + 'b'
    Pairs[key]['Of'] = key + 'o'
    
print(Pairs)
It's concise code and a very nice improvement on the original.
The printout is as follows:

"{'A': {'Bi': 0, 'Of': 0, 'Spd': 0}, 'E': {'Bi': 0, 'Of': 0, 'Spd': 0}, 'U': {'Bi': 0, 'Of': 0, 'Spd': 0}}
{'A': {'Bi': 'Ab', 'Of': 'Ao', 'Spd': 0}, 'E': {'Bi': 'Eb', 'Of': 'Eo', 'Spd': 0}, 'U': {'Bi': 'Ub', 'Of': 'Uo', 'Spd': 0}}"

Which whilst it runs correctly, it does not load the values of the variables
e.g instead of 'Ab' I need it to load the value of Ab (i.e 24) for the key 'Bi' etc...

If I try replacing
Pairs[key]['Bi'] = key + 'b'
with
Pairs[key]['Bi'] = float(key + 'b')
this error shows up:

ValueError: could not convert string to float: 'Ab'

I have searched on line for this error, but can't see how apply a correction.

Any further suggestions?

Sorry to test your patience....

Astrikor
Reply


Messages In This Thread
Editing Dictionaries - by Astrikor - Aug-14-2018, 10:02 AM
RE: Editing Dictionaries - by j.crater - Aug-14-2018, 10:09 AM
RE: Editing Dictionaries - by ichabod801 - Aug-14-2018, 12:27 PM
RE: Editing Dictionaries - by Astrikor - Aug-14-2018, 03:03 PM
RE: Editing Dictionaries - by ichabod801 - Aug-14-2018, 05:47 PM
RE: Editing Dictionaries - by Astrikor - Aug-15-2018, 01:43 PM
RE: Editing Dictionaries - by ichabod801 - Aug-15-2018, 02:00 PM
RE: Editing Dictionaries - by Astrikor - Aug-15-2018, 05:05 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Need help editing a PDF omar 4 1,251 Oct-22-2022, 08:52 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020