Python Forum
dictionary questions - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Homework (https://python-forum.io/forum-9.html)
+--- Thread: dictionary questions (/thread-6161.html)



dictionary questions - atux_null - Nov-08-2017



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.


RE: dictionary questions - Larz60+ - Nov-08-2017

This looks like the assignment.
What have you tried to do so far?
hint:
for key, value in di.items():
...    print(key, value)



RE: dictionary questions - atux_null - Nov-09-2017

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.


RE: dictionary questions - Larz60+ - Nov-09-2017

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)



RE: dictionary questions - atux_null - Nov-09-2017

i am running python3. The problem is that i cannot change the code, but only add where yyy is. Any ideas, please?


RE: dictionary questions - heiner55 - Nov-10-2017

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.


RE: dictionary questions - VlaDimitris - Jun-20-2018

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?


RE: dictionary questions - Nwb - Jun-20-2018

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


RE: dictionary questions - ljmetzger - Jun-20-2018

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