On 12/31/2011 11:07 AM, julien tayon wrote:
> The question is a dict the same as a vector or as a set ?
A dict is a mapping, which is to say, a functional set of key,value
pairs. 'Functional' means that the keys are unique. This is a key
property (pun intended). A vector can be regarded as a functional set of
count,value pairs. Some languages use dicts for vectors. Even in Python,
people use dicts for sparse arrays of various dimensions, with
unspecified pairs assumed to have a 0 value.
So it makes the most sense to me for d1 + d2 to be interpreted as set
union, with some resolution of key conflicts to restore the functional
property. Your vector interpretation of adding values only works for
values that can be added.
> since sets are using logical operators for operations that are roughly
> sets operations why not use & ^ | for sets operation on dict ?
> it would be pretty consistent with sets operations.
I agree, except that dicts are not just sets of pairs, but functional
sets. If one wants to view a dict simply as a set of keys, values, or
pairs, ignoring the functional property for the pairs, that is what the
.keys(), .values(), and .items() set view methods are for. All the set
operations are available on the resulting set view objects.
--
Terry Jan Reedy