~ubuntu-branches/ubuntu/lucid/python2.6/lucid

« back to all changes in this revision

Viewing changes to Doc/tutorial/datastructures.rst

  • Committer: Bazaar Package Importer
  • Author(s): Matthias Klose
  • Date: 2009-04-08 02:29:05 UTC
  • mto: (10.1.3 experimental)
  • mto: This revision was merged to the branch mainline in revision 23.
  • Revision ID: james.westby@ubuntu.com-20090408022905-xa5zbe8821m2o77o
Tags: upstream-2.6.2~rc1
ImportĀ upstreamĀ versionĀ 2.6.2~rc1

Show diffs side-by-side

added added

removed removed

Lines of Context:
214
214
   >>> def sum(seq):
215
215
   ...     def add(x,y): return x+y
216
216
   ...     return reduce(add, seq, 0)
217
 
   ... 
 
217
   ...
218
218
   >>> sum(range(1, 11))
219
219
   55
220
220
   >>> sum([])
251
251
   []
252
252
   >>> [[x,x**2] for x in vec]
253
253
   [[2, 4], [4, 16], [6, 36]]
254
 
   >>> [x, x**2 for x in vec]   # error - parens required for tuples
 
254
   >>> [x, x**2 for x in vec]  # error - parens required for tuples
255
255
     File "<stdin>", line 1, in ?
256
256
       [x, x**2 for x in vec]
257
257
                  ^
281
281
powerful tool but -- like all powerful tools -- they need to be used carefully,
282
282
if at all.
283
283
 
284
 
Consider the following example of a 3x3 matrix held as a list containing three 
 
284
Consider the following example of a 3x3 matrix held as a list containing three
285
285
lists, one list per row::
286
286
 
287
287
    >>> mat = [
290
290
    ...        [7, 8, 9],
291
291
    ...       ]
292
292
 
293
 
Now, if you wanted to swap rows and columns, you could use a list 
 
293
Now, if you wanted to swap rows and columns, you could use a list
294
294
comprehension::
295
295
 
296
296
    >>> print [[row[i] for row in mat] for i in [0, 1, 2]]
308
308
            print row[i],
309
309
        print
310
310
 
311
 
In real world, you should prefer builtin functions to complex flow statements. 
 
311
In real world, you should prefer builtin functions to complex flow statements.
312
312
The :func:`zip` function would do a great job for this use case::
313
313
 
314
314
    >>> zip(*mat)
401
401
 
402
402
   >>> x, y, z = t
403
403
 
404
 
This is called, appropriately enough, *sequence unpacking*. Sequence unpacking
405
 
requires the list of variables on the left to have the same number of elements
406
 
as the length of the sequence.  Note that multiple assignment is really just a
407
 
combination of tuple packing and sequence unpacking!
408
 
 
409
 
There is a small bit of asymmetry here:  packing multiple values always creates
410
 
a tuple, and unpacking works for any sequence.
 
404
This is called, appropriately enough, *sequence unpacking* and works for any
 
405
sequence on the right-hand side.  Sequence unpacking requires the list of
 
406
variables on the left to have the same number of elements as the length of the
 
407
sequence.  Note that multiple assignment is really just a combination of tuple
 
408
packing and sequence unpacking.
411
409
 
412
410
.. XXX Add a bit on the difference between tuples and lists.
413
411
 
551
549
   >>> answers = ['lancelot', 'the holy grail', 'blue']
552
550
   >>> for q, a in zip(questions, answers):
553
551
   ...     print 'What is your {0}?  It is {1}.'.format(q, a)
554
 
   ...  
 
552
   ...
555
553
   What is your name?  It is lancelot.
556
554
   What is your quest?  It is the holy grail.
557
555
   What is your favorite color?  It is blue.
574
572
   >>> basket = ['apple', 'orange', 'apple', 'pear', 'orange', 'banana']
575
573
   >>> for f in sorted(set(basket)):
576
574
   ...     print f
577
 
   ...  
 
575
   ...
578
576
   apple
579
577
   banana
580
578
   orange