See All Titles |
![]() ![]() OperatorsDictionaries do not support sequence operations such as concatenation and repetition, although an update() built-in method exists that populates one dictionary with the contents of another. Dictionaries do not have a "membership" operator either, but the has_key() built-in method basically performs the same task. Standard Type OperatorsDictionaries will work with all of the standard type operators. These were introduced in Chapter 4, but we will present some examples of how to use them with dictionaries here: >>> dict4 = { 'abc': 123 } >>> dict5 = { 'abc': 456 } >>> dict6 = { 'abc': 123, 98.6: 37 } >>> dict7 = { 'xyz': 123 } >>> dict4 < dict5 1 >>> (dict4 < dict6) and (dict4 < dict7) 1 >>> (dict5 < dict6) and (dict5 < dict7) 1 >>> dict6 < dict7 0 How are all these comparisons performed? Like lists and tuples, the process is a bit more complex than it is for numbers and strings. The algorithm is detailed below in Section 7.3.1. Dictionary Key-lookup Operator ( [ ] )The only operator specific to dictionaries is the key-lookup operator, which works very similar to the single element slice operator for sequence types. For sequence types, an index offset is the sole argument or subscript to access a single element of a sequence. For a dictionary, lookups are by key, so that is the argument rather than an index. The key-lookup operator is used for both assigning values to and retrieving values from a dictionary: dict[k] = v # set value 'v' in dictionary with key 'k' dict[k] # lookup value in dictionary with key 'k'
|
© 2002, O'Reilly & Associates, Inc. |