[Tutor] A Dictionary question

Sophie DeNofrio swimbabe339 at yahoo.com
Mon Apr 11 15:24:42 CEST 2011


Thank you to everyone who helped!
Steven D'Aprano I was secretly hoping you'd help. This is a family not personal and 'junk' (this isn't junk obviously. But it falls into that not personal category) account so you are talking to Annie lol. I guess I should have just signed up using my own email address in retrospect. Thank you for walking me through what I have to do as I want to learn. That dictionary is a much smaller piece to a file reading program. Next time I will attach my code. I totally blanked on how helpful that would be. Thank you for taking time out of your day to explain so fully. :)   
.___.

{O,o}

/)__) Annie

-"-"-

--- On Mon, 4/11/11, Steven D'Aprano <steve at pearwood.info> wrote:

From: Steven D'Aprano <steve at pearwood.info>
Subject: Re: [Tutor] A Dictionary question
To: tutor at python.org
Date: Monday, April 11, 2011, 8:09 AM

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

_______________________________________________
Tutor maillist  -  Tutor at python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20110411/c24730b9/attachment-0001.html>


More information about the Tutor mailing list