home *** CD-ROM | disk | FTP | other *** search
/ The Datafile PD-CD 5 / DATAFILE_PDCD5.iso / utilities / p / python / pyhtmldoc / m / mapping_ty next >
Text File  |  1996-11-14  |  2KB  |  43 lines

  1. <TITLE>Mapping Types -- Python library reference</TITLE>
  2. Next: <A HREF="../o/other_built-in_types" TYPE="Next">Other Built-in Types</A>  
  3. Prev: <A HREF="../s/sequence_types" TYPE="Prev">Sequence Types</A>  
  4. Up: <A HREF="../t/types" TYPE="Up">Types</A>  
  5. Top: <A HREF="../t/top" TYPE="Top">Top</A>  
  6. <H2>2.1.6. Mapping Types</H2>
  7. A <DFN>mapping</DFN> object maps values of one type (the key type) to
  8. arbitrary objects.  Mappings are mutable objects.  There is currently
  9. only one standard mapping type, the <DFN>dictionary</DFN>.  A dictionary's keys are
  10. almost arbitrary values.  The only types of values not acceptable as
  11. keys are values containing lists or dictionaries or other mutable
  12. types that are compared by value rather than by object identity.
  13. Numeric types used for keys obey the normal rules for numeric
  14. comparison: if two numbers compare equal (e.g. 1 and 1.0) then they
  15. can be used interchangeably to index the same dictionary entry.
  16. <P>
  17. Dictionaries are created by placing a comma-separated list of
  18. <CODE><VAR>key</VAR>:,<VAR>value</VAR></CODE> pairs within braces, for example:
  19. <CODE>{'jack':,4098, 'sjoerd':,4127}</CODE> or
  20. <CODE>{4098:,'jack', 4127:,'sjoerd'}</CODE>.
  21. <P>
  22. The following operations are defined on mappings (where <VAR>a</VAR> is a
  23. mapping, <VAR>k</VAR> is a key and <VAR>x</VAR> is an arbitrary object):
  24. <P>
  25. <DL>
  26. <DT><I>Operation</I><DD><I>Result</I>  ---  <I>Notes</I>
  27. <P>
  28. <DT><CODE>len(<VAR>a</VAR>)</CODE><DD>the number of items in <VAR>a</VAR>
  29. <DT><CODE><VAR>a</VAR>[<VAR>k</VAR>]</CODE><DD>the item of <VAR>a</VAR> with key <VAR>k</VAR>  ---  (1)
  30. <DT><CODE><VAR>a</VAR>[<VAR>k</VAR>] = <VAR>x</VAR></CODE><DD>set <CODE><VAR>a</VAR>[<VAR>k</VAR>]</CODE> to <VAR>x</VAR>
  31. <DT><CODE>del <VAR>a</VAR>[<VAR>k</VAR>]</CODE><DD>remove <CODE><VAR>a</VAR>[<VAR>k</VAR>]</CODE> from <VAR>a</VAR>  ---  (1)
  32. <DT><CODE><VAR>a</VAR>.items()</CODE><DD>a copy of <VAR>a</VAR>'s list of (key, item) pairs  ---  (2)
  33. <DT><CODE><VAR>a</VAR>.keys()</CODE><DD>a copy of <VAR>a</VAR>'s list of keys  ---  (2)
  34. <DT><CODE><VAR>a</VAR>.values()</CODE><DD>a copy of <VAR>a</VAR>'s list of values  ---  (2)
  35. <DT><CODE><VAR>a</VAR>.has_key(<VAR>k</VAR>)</CODE><DD><CODE>1</CODE> if <VAR>a</VAR> has a key <VAR>k</VAR>, else <CODE>0</CODE>
  36. </DL>
  37.  Notes:
  38. <DL>
  39. <DT><B>(1)</B><DD>Raises an exception if <VAR>k</VAR> is not in the map.
  40. <P>
  41. <DT><B>(2)</B><DD>Keys and values are listed in random order.
  42. </DL>
  43.