[Tutor] a quick Q: how to use for loop to read a series of files with .doc end

Prasad, Ramit ramit.prasad at jpmorgan.com
Wed Oct 5 22:33:47 CEST 2011


>>yes, you're iterating over the keys of a dictionary.  Since it only has the key "E", that's what you get.  Try printing dir(results) to see what methods might return something other than the key.  Make the language work for you.

>Sorry I am not smart.  value?

Dictionaries {} are containers for key/value based pairs like { key : value, another_key : value(can be same or repeated) }

For example: 
{'B': [0, 0, 0, 0, 0, 0], 'E': [2, 1, 4, 0, 1, 0]}
The keys here are 'B' and 'E'. The values here are [0, 0, 0, 0, 0, 0] (for key 'B') and [2, 1, 4, 0, 1, 0] (for key 'E')

You can get the value of a dictionary by doing: value = dictionary[key]
You can set the value of a dictionary by doing: dictionary[key] = value

You will get an exception if you try to get a value for a key that is not in the dictionary.
>>> dictionary = {'B': [0, 0, 0, 0, 0, 0], 'E': [2, 1, 4, 0, 1, 0]}
>>> dictionary['F']
KeyError: 'F'

You can iterate over dictionaries in a couple ways
1-
for key in dictionary: # also the same as 
                      # for key in dictionary.keys()
    value = dictionary[ key ]
2-
for key, value in dictionary.iteritems():
    < do something here with >



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