[Tutor] python dictionary and loop

2012-02-28 Thread justin fargus
Hello,

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


I forgot to add in my first email that I am using Windows Vista 64x and
running Python 3.2.2.

The two sentences I would like to use in the program is the following:

This is line one\nThis is line two! so I write: text_message = This is
line one\nThis is line two!

The dictionary name and key/value pairs will be:

my_sentences {'This':'This','is':'is','line':'line','one':'two'}   # Does
this code split the words of each sentence so that the words in sentence 1
are keys, and the words in sentence 2 are values?
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] python dictionary and loop

2012-02-28 Thread Prasad, Ramit
The two sentences I would like to use in the program is the following:
This is line one\nThis is line two! so I write: text_message = This is line 
one\nThis is line two!
The dictionary name and key/value pairs will be:
my_sentences {'This':'This','is':'is','line':'line','one':'two'}   # Does this 
code split the words of each sentence so that the words in sentence 1 are 
keys, and the words in sentence 2 are values? 

That code does not split anything; it manually hard codes the results you want!
Although the dictionary you show is where each word in sentence one is the key
for the corresponding word in sentence two. Based on your description, I was 
thinking
you wanted something more like my_sentences = { 'This is line one': 'This is 
line two' }

What happens for 1 sentence or 3 sentences? What if the sentences are not the 
same 
length? This type of manipulation makes sense for handling tabular data, but
not sure exactly what your use case is...

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.  
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor