Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The notion of tuples are highly expressive:</p> <ul> <li><p>Pragmatically, they are great for packing and unpacking values (<code>x,y=coord</code>).</p></li> <li><p>In combination with dictionaries (hash tables), they allow forms of mapping that would otherwise require many levels of association. For example, consider marking that (x,y) has been found.</p> <pre><code>// PHP if (!isset($found[$x])) { $found[$x] = Array(); $found[$x][$y] = true; } else if (!isset($found[$x][$y])) { $found[$x][$y] = true; } # Python found[(x,y)] = True # parens added for clarity </code></pre></li> <li><p>Lists should be used with the expectation of operations on its contents (hence the various mentions of immutability). One will want to pop, push, splice, slice, search, insert before, insert after, etc with a list.</p></li> <li><p>Tuples should be a low-level representation of an object, where simple comparisons are made, or operations such as extracting the n'th element or n elements in a predictable fashion, such as the coordinates example given earlier.</p></li> <li><p>Lastly, lists are not hashable, so the type of mapping done with dictionaries (hash tables in Perl, associative arrays in PHP) must be done with tuples.</p> <p>Here's a simple example of tuples and dictionaries, together at last:</p> <pre><code>""" couple is a tuple of two people doesLike is a dictionary mapping couples to True or False """ couple = "john", "jane" doesLike = dict() doesLike[couple] = True doesLike["jane", "john"] = False # unrequited love :'( </code></pre></li> </ul>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload