[Tutor] python questions about dictionary loops

Prasad, Ramit ramit.prasad at jpmorgan.com
Wed Feb 29 00:08:08 CET 2012


Justin wrote:


>I'm new to Python. Just started a few weeks ago and I've been learning some basic things that I would like to put together but I don't even know where to began. I am trying to do the following:
>Make a program using two sentences of about 8 words (total between the two sentences).  I would then like to create a dictionary {} and split the words of each sentence using one sentence as a dictionary key and using the other sentence for the dictionary value.  I would then like to use a loop (while or for) that will write out a file that has the dictionary key and dictionary value to it.  How can I go about doing this? I've noticed with Python there is more than one way to do most things. I would like the simplest so that I can practice over and over again on my own until I understand it. Thanks in advance


Steps:
1. Do you know how to input / retrieve the two sentences? (Look at open() or raw_input() if you do not )
2. Do you know how to split strings? Look at the string operations (the python web API will show you if you read it)
3. Do you know how to create a dictionary and set values? Play around on the interactive prompt if you do not.
4. How do you want to write this out? You have lots of options, look at open(), pickle, shelve for just a few options.

Now try and code something together and if you get stuck we can help. If you manage to finish it we can help improve it as well.


Ramit


Ramit Prasad | JPMorgan Chase Investment Bank | Currencies Technology
712 Main Street | Houston, TX 77002
work phone: 713 - 216 - 5423

--


This email is confidential and subject to important disclaimers and
conditions including on offers for the purchase or sale of
securities, accuracy and completeness of information, viruses,
confidentiality, legal privilege, and legal entity disclaimers,
available at http://www.jpmorgan.com/pages/disclosures/email.  


More information about the Tutor mailing list