Bottom Page

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
Quote
#2
                Pair = 'A'
        elif i == 1:
                Pair = 'E'
        elif i == 2:
                Pair = 'U'
Here you assign a string to Pair and not the dictionary you want to work with. This means your code really does:
'A'['Bi'] = Pair + 'b'  # when it should be A['Bi'] = Pair + 'b'
So instead just remove the parentheses from A E and U.
Quote
#3
To add to what jcrater said, you can totally simplify your loop:

for Pair in (A, E, U):
    Pair['Bi'] = Pair + 'b'
    ...
Of course, you will get another error on Pair['Bi'] = Pair + 'b', because you are trying to add a dictionary to a string, which won't work. But it's not clear to me what you're expecting to happen there, so I'm not sure how to fix it.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures

Quote
#4
Thanks for the replies,

By way of explanation, I was hoping to allocate the values of the 6 variables to the values of the dictionary keys, such that

when Pair = A then

Pair['Bi'] = Pair + 'b'

becomes:

A['Bi'] = 'Ab' (i.e A['Bi'] = 24)

and

A['Spd'] = float(Ao) - float(Ab) (i.e A['Spd'] = 41 - 24 = 17)

The purpose being to assign the respective values to each dictionary to result as follows:

A = {'Bi' : 24,'Of' : 41,'Spd' : 17}
E = {'Bi' : 67,'Of' : 80,'Spd' : 13}
U = {'Bi' : 15,'Of' : 30,'Spd' : 15}

and I was expecting the final print(Pair) to print out each dictionary in turn with the new values inserted.

(Subsequently new values of the 6 variables will then need to be inserted as new data is collected.)

But maybe there is a better way to do this?
Quote
#5
Well, if you put all of your pair dictionaries in another dictionary:

Pairs = {'A': A, 'E': E, 'U': U}
Then you could do:

key = 'A'
Pairs[key]['Bi'] = key + 'b'
Then Pairs['A']['Bi'] would equal 'Ab'.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures

Quote
#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
Quote
#7
Again, dictionaries:

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

Values = {'Ab': 24, 'Ao': 41, 'Eb': 67, 'Eo': 80, 'Ub': 15, 'Uo': 30}

print(Pairs)

for key in Pairs:
    Pairs[key]['Bi'] = Values[key + 'b']
    Pairs[key]['Of'] = Values[key + 'o']

print(Pairs)
Output:
{'A': {'Of': 0, 'Spd': 0, 'Bi': 0}, 'U': {'Of': 0, 'Spd': 0, 'Bi': 0}, 'E': {'Of': 0, 'Spd': 0, 'Bi': 0}} {'A': {'Of': 41, 'Spd': 0, 'Bi': 24}, 'U': {'Of': 30, 'Spd': 0, 'Bi': 15}, 'E': {'Of': 80, 'Spd': 0, 'Bi': 67}}
Astrikor likes this post
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures

Quote
#8
That's sorted then!

So elegant when you know.

Many thanks Ichabod801.
Astrikor
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Editing a txt document using python duckredbeard 3 210 Sep-24-2020, 02:38 AM
Last Post: duckredbeard
  I Need Help Editing My Code eddywinch82 10 869 Aug-29-2020, 03:54 PM
Last Post: eddywinch82
  Code Editing Help Rememberthecreed 2 484 Feb-08-2020, 09:56 AM
Last Post: Rememberthecreed
  Indexed position editing of a lst ShruthiLS 4 449 Sep-26-2019, 09:23 AM
Last Post: perfringo
  Editing and testing a package amasta 2 718 Mar-19-2019, 12:36 PM
Last Post: buran
  Help Editing/Problem with outputs stanthaman42 7 1,431 Jul-11-2018, 05:30 PM
Last Post: ichabod801
  Text editing with python UK_SPB 1 1,011 Nov-26-2017, 08:31 AM
Last Post: wavic
  [Discord.py] Editing messages Ouindoze 4 19,143 Oct-11-2017, 02:07 AM
Last Post: Ouindoze

Forum Jump:


Users browsing this thread: 1 Guest(s)