[Tutor] A Dictionary question
Steven D'Aprano
steve at pearwood.info
Mon Apr 11 14:09:57 CEST 2011
Hello Sophie, or do you prefer Annie?
Sophie DeNofrio wrote:
> Hi Everyone,
> I am a super beginner and am little muddled right now. So I apologize
> for the low level question but I am trying to write a function that
> will return a dictionary of a given list of strings containing two
> coordinates separated by a space with the first numbers as a key and
> the second numbers as its corresponding value. I thought maybe a set
> might be helpful but that didn't seem to work at all. I am pretty
> much as confused as they come and any help would be very much
> appreciated. Thank you so much for your time.
It will help if you give us an example of what your input data is, and
what you expect to get for your result. I'm going to try to guess.
I think your input data might look like this:
data = ["1 2", "4 5", "23 42"]
and you want to return a dictionary like:
{1: 2, 4: 5, 23: 42}
Am I close?
This sounds like homework, and our policy is not to solve homework for
people, but to guide them into solving it themselves. So here are some
hints. Please feel free to show us the code you are using if you have
any problems.
You will need to have a dict ready to store keys and values in, and then
you need to look at each string in the data list one at a time:
result = {} # This is an empty dictionary.
for s in data:
# Process the variable s each time.
print(s)
Since s is a string that looks like two numbers separated by a space,
processing the string needs two tasks: first you have to split the
string into the two parts, and then you have to turn each part from a
string into an actual number.
(Remember that in Python, "42" is not a number, it is just a string that
looks like a number.)
There are two functions that are useful for that: one is a string
method, and one is a proper function. Here are some examples to show
them in action:
# Splitting a string into two pieces:
>>> s = "1234 5678"
>>> s.split()
['1234', '5678']
# Converting a string into a proper number:
>>> a = "987"
>>> int(a)
987
The split method is especially useful when you use assignment to create
two variables at once:
>>> s = "1234 5678"
>>> a, b = s.split()
>>> a
'1234'
>>> b
'5678'
>>> int(a)
1234
Lastly, do you know how to store objects into a dictionary? You need two
pieces, a key and a value:
>>> d = {} # Empty dict.
>>> key = "dinner"
>>> value = "pizza"
>>> d[key] = value
>>>
>>> print "What's for dinner?", d["dinner"]
What's for dinner? pizza
Only in your case, rather than storing strings in the dict, you want to
store numbers created by int(). So now you have to put all the pieces
together into one piece of code:
(1) Create an empty dict.
(2) Loop over the individual strings in the input list.
(3) For each string, split it into two pieces.
(4) Convert each piece into an int (integer).
(5) Store the first piece in the dict as the key, and the second piece
as the value.
Good luck!
--
Steven
More information about the Tutor
mailing list