[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


[Tutor] python dictionary

2009-04-14 Thread sudhanshu gautam
I was reading about the python dictionaries  had finished some methods
related to the dictionary.

we used get and setdefault methods to get the value from the dictionary if
it lies inside it

and setdefault used to set the value is side the dictionary if it does not
lies inside the dictionary.

so why we use setdefault I think in place of setdefault first we should
convert that dictionary in the list and now we can add new value then again
convert it in dictionary
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] python dictionary

2009-04-14 Thread bob gailer

sudhanshu gautam wrote:
I was reading about the python dictionaries  

That is a good thing to do.

had finished some methods related to the dictionary.

No comprehend.


we used get and setdefault methods to get the value from the 
dictionary if it lies inside it


and setdefault used to set the value is side the dictionary if it does 
not lies inside the dictionary.

Those are good things to do.



so why we use setdefault I think in place of setdefault first we 
should convert that dictionary in the list and now we can add new 
value then again convert it in dictionary



I'm sorry but I do not understand the final paragraph.
It might help a little if you were to use more punctuation.
Are you asking a question?
Could you give an example?

--
Bob Gailer
Chapel Hill NC
919-636-4239
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] python dictionary

2009-04-14 Thread Alan Gauld


sudhanshu gautam sudhanshu9...@gmail.com wrote

and setdefault used to set the value is side the dictionary if it does 
not

lies inside the dictionary.

so why we use setdefault I think in place of setdefault first we should
convert that dictionary in the list and now we can add new value then 
again

convert it in dictionary


You probably could convert it to a list, append a tuple and then convert
the list back to a dictionary. But why would you want to do that when
setdefault is so much easier?

Also the conversions between list and dictionary would take a lot of
time and computer power for a big dictionary

For example I tried


L = [(n,n) for n in range(100)]
d = dict(L)
d2 = [(k,v) for k,v in d.items()]
d.setdefault(-5,-5)



- producing the dict from the list took about half a second
- producing the list from the dict took over 3 seconds and
 used over 50% of my CPU...
- using setdefault() was effectively instantaneous.

HTH,

--
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/ 



___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor