Python Forum
Thread Rating:
  • 2 Vote(s) - 2.5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
dictionary questions
#1


hi. i have two questions that i am stuck with in dictionarys. First one,
di1={5:'K',3:'A',2:'R',7:'T',4:'G',6:'S'}
di2={'GR':'Greece','IT':'Italy','ES':'Spain'}
print(len(di2[xxxx]))
it needs to replace xxx with something that print will output 6.



second,
di={'A':['a',41],'B':['b',42],'C':['c',43]}
s=0
for i in di:
    s+=yyy
print(s)
need to replace yyy with something that will print 126


please i am asking for your help.
Reply
#2
This looks like the assignment.
What have you tried to do so far?
hint:
for key, value in di.items():
...    print(key, value)
Reply
#3
for the first part of the code i did try di1['S'] but it comes with an error. the second part i do not have a clue.
Reply
#4
That's because 'S' is a value, not a key.
if you run the snippet in my first post you will get a list of key, value pairs.
Just change the dictionary name for the different dictionaries
which version of python do you run, because my snippet is for python 3.
To get by value, use:
for key, value in di1.items():
    if value == 'S':
        print(key)
Reply
#5
i am running python3. The problem is that i cannot change the code, but only add where yyy is. Any ideas, please?
Reply
#6
If you do not want to solve this very easy exercise by thinking,
you should try it by brute force.
You have 6 options from di2 for xxx and see what happens.
Reply
#7
I have the same problem but after a lot of thought I found this di1[4]+di1[2].Now,for second could someone to help? I think this is di['A'][1]-di['B'][1]+di['C'][1].Is it correct?
Reply
#8
Here are some hints:

For the first one,
len() returns the number of characters in a string. And since (di2[xxxx]) is already given, xxxx must be within di2. Also, since it's a dictionary, you should write the key which holds the value to get the value of the key.

For the second one,
In the for loop, "i" (since it is after for and before in) will hold the value of the key being iterated over. So it will have 41 in the first loop instance, 42 in the second and 43 in the third. What is 41+42+43?

Solving such questions is a great way to learn. Keep it up! :D
Reply
#9
When you have a problem that has you confused, sometimes you can use Python as a tool to examine your data to give you a clue on how to proceed. For example, the following code may help you get a better feel for the data. Combined with the clues above, you should be able to solve the problem.
di2={'GR':'Greece','IT':'Italy','ES':'Spain'}
print(di2.keys())
print(di2.values())
print(di2['GR'])
print("")
for i in di2:
    print(i)
    print(di2[i])

print("")
di={'A':['a',41],'B':['b',42],'C':['c',43]}
print(di.keys())
print(di.values())
s=0
for i in di:
    print("")
    print(di[i])
    print(di[i][0])
    print(di[i][1])
Output:
dict_keys(['ES', 'IT', 'GR']) dict_values(['Spain', 'Italy', 'Greece']) Greece ES Spain IT Italy GR Greece dict_keys(['B', 'C', 'A']) dict_values([['b', 42], ['c', 43], ['a', 41]]) ['b', 42] b 42 ['c', 43] c 43 ['a', 41] a 41
Lewis
To paraphrase: 'Throw out your dead' code. https://www.youtube.com/watch?v=grbSQ6O6kbs Forward to 1:00
Reply


Forum Jump:

User Panel Messages

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