Posts: 45
Threads: 15
Joined: Oct 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.
Posts: 11,880
Threads: 474
Joined: Sep 2016
This looks like the assignment.
What have you tried to do so far?
hint:
for key, value in di.items():
... print(key, value)
Posts: 45
Threads: 15
Joined: Oct 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.
Posts: 11,880
Threads: 474
Joined: Sep 2016
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)
Posts: 45
Threads: 15
Joined: Oct 2017
i am running python3. The problem is that i cannot change the code, but only add where yyy is. Any ideas, please?
Posts: 606
Threads: 3
Joined: Nov 2016
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.
Posts: 1
Threads: 0
Joined: Jun 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?
Posts: 75
Threads: 20
Joined: Jun 2018
Jun-20-2018, 03:45 PM
(This post was last modified: Jun-20-2018, 03:46 PM by Nwb.)
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
Posts: 127
Threads: 3
Joined: Mar 2018
Jun-20-2018, 07:00 PM
(This post was last modified: Jun-20-2018, 07:00 PM by ljmetzger.)
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
|