Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Editing Dictionaries
#1
I have 3 dictionaries in which I am trying to update the values of the keys.
The keys of each dictionary are identical, so I thought I could simply cycle round the dictionaries using a 'For' condition.

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

print(A)
Ab = 24
Ao = 41
Eb = 67
Eo = 80
Ub = 15
Uo = 30


for i in range(3): 
        if i == 0:
                Pair = 'A'
        elif i == 1:
                Pair = 'E'
        elif i == 2:
                Pair = 'U'
        
        Pair['Bi'] = Pair + 'b'
        Pair['Of'] = Pair + 'o'
        Pair['Spd'] = Pair + 'o' - Pair + 'b'
        print(Pair)
However, when run I get only the first printout followed by an error message as follows:

"{'Bi': 0, 'Of': 0, 'Spd': 0}
Traceback (most recent call last):
File "C:\Users\Astrikor\Desktop\CyclePairs.py", line 23, in <module>
Pair['Bi'] = Pair + 'b'
TypeError: 'str' object does not support item assignment"

As I am still a (very) newbie, I am sure I have a misunderstanding somewhere here (maybe I should be using a Function ?)

Any help would be welcome

Many thanks
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

Forum Jump:

User Panel Messages

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