On Thu, Oct 6, 2011 at 4:33 AM, Prasad, Ramit <ramit.pra...@jpmorgan.com>wrote:
> >>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 > > Now finally figure out what's the string, list and dictionary. May I ask a further question: >>> a {'B': [4, 5, 6], 'E': {1, 2, 3}} How can I get the value of set(a['E'])+set(a['B']) I mean, get a new dict 'B+E':[5,7,9] Thanks, > > > > 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 > -- Best Regards, lina
_______________________________________________ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor