[Tutor] python questions about dictionary loops

2012-02-28 Thread justin fargus
Hello,


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

Regards,

Justin
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] python questions about dictionary loops

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


Re: [Tutor] python questions about dictionary loops

2012-02-28 Thread Steven D'Aprano

justin fargus wrote:

Hello,


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?


You don't tell us what parts of the problem are causing you trouble. Also, I 
don't know if this is homework or not, but a dictionary does not sound like 
the right data structure to use here. What happens if the first sentence (the 
keys) contains duplicate words?



Here are some examples to get you started. Run them in Python, and see what 
they do. They won't solve your problem exactly, but if you study them and see 
what they are doing, you should be able to adapt them to solve your problem. 
If anything is unclear, please ask.



paragraph = This line has one sentence.\nAnd so does this one.
first_line, second_line = paragraph.split('\n')
print(first_line)
print(second_line)

sentence = this is a sentence of seven words
words = sentence.split()
ordinals = 1st 2nd 3rd 4th 5th 6th 7th.split()
for ordinal, word in zip(ordinals, words):
print(%s ::  %s % (ordinal, word))

d = {}
for key, value in zip(['a', 'bb', 'ccc', ''], [1, 2, 3, 4]):
d[key] = value

for k, v in d.items():
print(%s ::  %s % (k, v))



Do you need any of this code explained, or is it clear what it does?


--
Steven

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor