~ubuntu-branches/ubuntu/karmic/python3.0/karmic

« back to all changes in this revision

Viewing changes to Doc/tutorial/datastructures.rst

  • Committer: Bazaar Package Importer
  • Author(s): Matthias Klose
  • Date: 2009-02-16 17:18:23 UTC
  • mfrom: (1.1.4 upstream)
  • Revision ID: james.westby@ubuntu.com-20090216171823-1d5cm5qnnjvmnzzm
Tags: 3.0.1-0ubuntu1
New upstream version.

Show diffs side-by-side

added added

removed removed

Lines of Context:
159
159
 
160
160
List comprehensions provide a concise way to create lists from sequences.
161
161
Common applications are to make lists where each element is the result of
162
 
some operations applied to each member of the sequence, or to create a 
 
162
some operations applied to each member of the sequence, or to create a
163
163
subsequence of those elements that satisfy a certain condition.
164
164
 
165
165
 
167
167
clause, then zero or more :keyword:`for` or :keyword:`if` clauses.  The result
168
168
will be a list resulting from evaluating the expression in the context of the
169
169
:keyword:`for` and :keyword:`if` clauses which follow it.  If the expression
170
 
would evaluate to a tuple, it must be parenthesized. 
 
170
would evaluate to a tuple, it must be parenthesized.
171
171
 
172
172
Here we take a list of numbers and return a list of three times each number::
173
173
 
195
195
 
196
196
Tuples can often be created without their parentheses, but not here::
197
197
 
198
 
   >>> [x, x**2 for x in vec]   # error - parens required for tuples
 
198
   >>> [x, x**2 for x in vec]  # error - parens required for tuples
199
199
     File "<stdin>", line 1, in ?
200
200
       [x, x**2 for x in vec]
201
201
                  ^
227
227
powerful tool but -- like all powerful tools -- they need to be used carefully,
228
228
if at all.
229
229
 
230
 
Consider the following example of a 3x3 matrix held as a list containing three 
 
230
Consider the following example of a 3x3 matrix held as a list containing three
231
231
lists, one list per row::
232
232
 
233
233
    >>> mat = [
236
236
    ...        [7, 8, 9],
237
237
    ...       ]
238
238
 
239
 
Now, if you wanted to swap rows and columns, you could use a list 
 
239
Now, if you wanted to swap rows and columns, you could use a list
240
240
comprehension::
241
241
 
242
242
    >>> print([[row[i] for row in mat] for i in [0, 1, 2]])
254
254
            print(row[i], end="")
255
255
        print()
256
256
 
257
 
In real world, you should prefer builtin functions to complex flow statements. 
 
257
In real world, you should prefer builtin functions to complex flow statements.
258
258
The :func:`zip` function would do a great job for this use case::
259
259
 
260
260
    >>> list(zip(*mat))
376
376
 
377
377
   >>> basket = {'apple', 'orange', 'apple', 'pear', 'orange', 'banana'}
378
378
   >>> print(basket)
379
 
   {'orange', 'bananna', 'pear', 'apple'}
 
379
   {'orange', 'banana', 'pear', 'apple'}
380
380
   >>> fruit = ['apple', 'orange', 'apple', 'pear', 'orange', 'banana']
381
381
   >>> fruit = set(basket)               # create a set without duplicates
382
382
   >>> fruit
440
440
value associated with that key is forgotten.  It is an error to extract a value
441
441
using a non-existent key.
442
442
 
443
 
The :meth:`keys` method of a dictionary object returns a list of all the keys
 
443
Performing ``list(d.keys())`` on a dictionary returns a list of all the keys
444
444
used in the dictionary, in arbitrary order (if you want it sorted, just apply
445
 
the :meth:`sort` method to the list of keys).  To check whether a single key is
 
445
the :meth:`sorted` function instead).  To check whether a single key is
446
446
in the dictionary, use the :keyword:`in` keyword.
447
447
 
448
448
Here is a small example using a dictionary::
458
458
   >>> tel
459
459
   {'guido': 4127, 'irv': 4127, 'jack': 4098}
460
460
   >>> list(tel.keys())
 
461
   ['irv', 'guido', 'jack']
 
462
   >>> sorted(tel.keys())
461
463
   ['guido', 'irv', 'jack']
462
464
   >>> 'guido' in tel
463
465
   True
517
519
   >>> answers = ['lancelot', 'the holy grail', 'blue']
518
520
   >>> for q, a in zip(questions, answers):
519
521
   ...     print('What is your {0}?  It is {1}.'.format(q, a))
520
 
   ...  
 
522
   ...
521
523
   What is your name?  It is lancelot.
522
524
   What is your quest?  It is the holy grail.
523
525
   What is your favorite color?  It is blue.
540
542
   >>> basket = ['apple', 'orange', 'apple', 'pear', 'orange', 'banana']
541
543
   >>> for f in sorted(set(basket)):
542
544
   ...     print(f)
543
 
   ...  
 
545
   ...
544
546
   apple
545
547
   banana
546
548
   orange