-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path12-tuple.txt
More file actions
660 lines (455 loc) · 17.7 KB
/
12-tuple.txt
File metadata and controls
660 lines (455 loc) · 17.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
Tuples
======
This chapter presents one more built-in type, the tuple, and then shows
how lists, dictionaries, and tuples work together. I also present a
useful feature for variable-length argument lists, the gather and
scatter operators.
One note: there is no consensus on how to pronounce “tuple”. Some people
say “tuh-ple”, which rhymes with “supple”. But in the context of
programming, most people say “too-ple”, which rhymes with “quadruple”.
Tuples are immutable
--------------------
A tuple is a sequence of values. The values can be any type, and they
are indexed by integers, so in that respect tuples are a lot like lists.
The important difference is that tuples are immutable.
Syntactically, a tuple is a comma-separated list of values:
::
>>> t = 'a', 'b', 'c', 'd', 'e'
Although it is not necessary, it is common to enclose tuples in
parentheses:
::
>>> t = ('a', 'b', 'c', 'd', 'e')
To create a tuple with a single element, you have to include a final
comma:
::
>>> t1 = 'a',
>>> type(t1)
<class 'tuple'>
A value in parentheses is not a tuple:
::
>>> t2 = ('a')
>>> type(t2)
<class 'str'>
Another way to create a tuple is the built-in function tuple. With no
argument, it creates an empty tuple:
::
>>> t = tuple()
>>> t
()
If the argument is a sequence (string, list or tuple), the result is a
tuple with the elements of the sequence:
::
>>> t = tuple('lupins')
>>> t
('l', 'u', 'p', 'i', 'n', 's')
Because tuple is the name of a built-in function, you should avoid using
it as a variable name.
Most list operators also work on tuples. The bracket operator indexes an
element:
::
>>> t = ('a', 'b', 'c', 'd', 'e')
>>> t[0]
'a'
And the slice operator selects a range of elements.
::
>>> t[1:3]
('b', 'c')
But if you try to modify one of the elements of the tuple, you get an
error:
::
>>> t[0] = 'A'
TypeError: object doesn't support item assignment
Because tuples are immutable, you can’t modify the elements. But you can
replace one tuple with another:
::
>>> t = ('A',) + t[1:]
>>> t
('A', 'b', 'c', 'd', 'e')
This statement makes a new tuple and then makes t refer to it.
The relational operators work with tuples and other sequences; Python
starts by comparing the first element from each sequence. If they are
equal, it goes on to the next elements, and so on, until it finds
elements that differ. Subsequent elements are not considered (even if
they are really big).
::
>>> (0, 1, 2) < (0, 3, 4)
True
>>> (0, 1, 2000000) < (0, 3, 4)
True
Tuple assignment
----------------
It is often useful to swap the values of two variables. With
conventional assignments, you have to use a temporary variable. For
example, to swap a and b:
::
>>> temp = a
>>> a = b
>>> b = temp
This solution is cumbersome; **tuple assignment** is more elegant:
::
>>> a, b = b, a
The left side is a tuple of variables; the right side is a tuple of
expressions. Each value is assigned to its respective variable. All the
expressions on the right side are evaluated before any of the
assignments.
The number of variables on the left and the number of values on the
right have to be the same:
::
>>> a, b = 1, 2, 3
ValueError: too many values to unpack
More generally, the right side can be any kind of sequence (string, list
or tuple). For example, to split an email address into a user name and a
domain, you could write:
::
>>> addr = 'monty@python.org'
>>> uname, domain = addr.split('@')
The return value from split is a list with two elements; the first
element is assigned to uname, the second to domain.
::
>>> uname
'monty'
>>> domain
'python.org'
Tuples as return values
-----------------------
Strictly speaking, a function can only return one value, but if the
value is a tuple, the effect is the same as returning multiple values.
For example, if you want to divide two integers and compute the quotient
and remainder, it is inefficient to compute x/y and then x%y. It is
better to compute them both at the same time.
The built-in function divmod takes two arguments and returns a tuple of
two values, the quotient and remainder. You can store the result as a
tuple:
::
>>> t = divmod(7, 3)
>>> t
(2, 1)
Or use tuple assignment to store the elements separately:
::
>>> quot, rem = divmod(7, 3)
>>> quot
2
>>> rem
1
Here is an example of a function that returns a tuple:
::
def min_max(t):
return min(t), max(t)
max and min are built-in functions that find the largest and smallest
elements of a sequence. ``min_max`` computes both and returns a tuple of
two values.
Variable-length argument tuples
-------------------------------
Functions can take a variable number of arguments. A parameter name that
begins with **gathers** arguments into a tuple. For example, printall
takes any number of arguments and prints them:
::
def printall(*args):
print(args)
The gather parameter can have any name you like, but args is
conventional. Here’s how the function works:
::
>>> printall(1, 2.0, '3')
(1, 2.0, '3')
The complement of gather is **scatter**. If you have a sequence of
values and you want to pass it to a function as multiple arguments, you
can use the operator. For example, divmod takes exactly two arguments;
it doesn’t work with a tuple:
::
>>> t = (7, 3)
>>> divmod(t)
TypeError: divmod expected 2 arguments, got 1
But if you scatter the tuple, it works:
::
>>> divmod(*t)
(2, 1)
Many of the built-in functions use variable-length argument tuples. For
example, max and min can take any number of arguments:
::
>>> max(1, 2, 3)
3
But sum does not.
::
>>> sum(1, 2, 3)
TypeError: sum expected at most 2 arguments, got 3
As an exercise, write a function called sumall that takes any number of
arguments and returns their sum.
Lists and tuples
----------------
zip is a built-in function that takes two or more sequences and returns
a list of tuples where each tuple contains one element from each
sequence. The name of the function refers to a zipper, which joins and
interleaves two rows of teeth.
This example zips a string and a list:
::
>>> s = 'abc'
>>> t = [0, 1, 2]
>>> zip(s, t)
<zip object at 0x7f7d0a9e7c48>
The result is a **zip object** that knows how to iterate through the
pairs. The most common use of zip is in a for loop:
::
>>> for pair in zip(s, t):
... print(pair)
...
('a', 0)
('b', 1)
('c', 2)
A zip object is a kind of **iterator**, which is any object that
iterates through a sequence. Iterators are similar to lists in some
ways, but unlike lists, you can’t use an index to select an element from
an iterator.
If you want to use list operators and methods, you can use a zip object
to make a list:
::
>>> list(zip(s, t))
[('a', 0), ('b', 1), ('c', 2)]
The result is a list of tuples; in this example, each tuple contains a
character from the string and the corresponding element from the list.
If the sequences are not the same length, the result has the length of
the shorter one.
::
>>> list(zip('Anne', 'Elk'))
[('A', 'E'), ('n', 'l'), ('n', 'k')]
You can use tuple assignment in a for loop to traverse a list of tuples:
::
t = [('a', 0), ('b', 1), ('c', 2)]
for letter, number in t:
print(number, letter)
Each time through the loop, Python selects the next tuple in the list
and assigns the elements to letter and number. The output of this loop
is:
::
0 a
1 b
2 c
If you combine zip, for and tuple assignment, you get a useful idiom for
traversing two (or more) sequences at the same time. For example,
``has_match`` takes two sequences, t1 and t2, and returns True if there
is an index i such that t1[i] == t2[i]:
::
def has_match(t1, t2):
for x, y in zip(t1, t2):
if x == y:
return True
return False
If you need to traverse the elements of a sequence and their indices,
you can use the built-in function enumerate:
::
for index, element in enumerate('abc'):
print(index, element)
The result from enumerate is an enumerate object, which iterates a
sequence of pairs; each pair contains an index (starting from 0) and an
element from the given sequence. In this example, the output is
::
0 a
1 b
2 c
Again.
Dictionaries and tuples
-----------------------
Dictionaries have a method called items that returns a sequence of
tuples, where each tuple is a key-value pair.
::
>>> d = {'a':0, 'b':1, 'c':2}
>>> t = d.items()
>>> t
dict_items([('c', 2), ('a', 0), ('b', 1)])
The result is a ``dict_items`` object, which is an iterator that
iterates the key-value pairs. You can use it in a for loop like this:
::
>>> for key, value in d.items():
... print(key, value)
...
c 2
a 0
b 1
As you should expect from a dictionary, the items are in no particular
order.
Going in the other direction, you can use a list of tuples to initialize
a new dictionary:
::
>>> t = [('a', 0), ('c', 2), ('b', 1)]
>>> d = dict(t)
>>> d
{'a': 0, 'c': 2, 'b': 1}
Combining dict with zip yields a concise way to create a dictionary:
::
>>> d = dict(zip('abc', range(3)))
>>> d
{'a': 0, 'c': 2, 'b': 1}
The dictionary method update also takes a list of tuples and adds them,
as key-value pairs, to an existing dictionary.
It is common to use tuples as keys in dictionaries (primarily because
you can’t use lists). For example, a telephone directory might map from
last-name, first-name pairs to telephone numbers. Assuming that we have
defined last, first and number, we could write:
::
directory[last, first] = number
The expression in brackets is a tuple. We could use tuple assignment to
traverse this dictionary.
::
for last, first in directory:
print(first, last, directory[last,first])
This loop traverses the keys in directory, which are tuples. It assigns
the elements of each tuple to last and first, then prints the name and
corresponding telephone number.
There are two ways to represent tuples in a state diagram. The more
detailed version shows the indices and elements just as they appear in a
list. For example, the tuple ``('Cleese', 'John')`` would appear as in
Figure [fig.tuple1].
.. figure:: figs/tuple1.pdf
:alt: State diagram.
State diagram.
But in a larger diagram you might want to leave out the details. For
example, a diagram of the telephone directory might appear as in
Figure [fig.dict2].
.. figure:: figs/dict2.pdf
:alt: State diagram.
State diagram.
Here the tuples are shown using Python syntax as a graphical shorthand.
The telephone number in the diagram is the complaints line for the BBC,
so please don’t call it.
Sequences of sequences
----------------------
I have focused on lists of tuples, but almost all of the examples in
this chapter also work with lists of lists, tuples of tuples, and tuples
of lists. To avoid enumerating the possible combinations, it is
sometimes easier to talk about sequences of sequences.
In many contexts, the different kinds of sequences (strings, lists and
tuples) can be used interchangeably. So how should you choose one over
the others?
To start with the obvious, strings are more limited than other sequences
because the elements have to be characters. They are also immutable. If
you need the ability to change the characters in a string (as opposed to
creating a new string), you might want to use a list of characters
instead.
Lists are more common than tuples, mostly because they are mutable. But
there are a few cases where you might prefer tuples:
#. In some contexts, like a return statement, it is syntactically
simpler to create a tuple than a list.
#. If you want to use a sequence as a dictionary key, you have to use an
immutable type like a tuple or string.
#. If you are passing a sequence as an argument to a function, using
tuples reduces the potential for unexpected behavior due to aliasing.
Because tuples are immutable, they don’t provide methods like sort and
reverse, which modify existing lists. But Python provides the built-in
function sorted, which takes any sequence and returns a new list with
the same elements in sorted order, and reversed, which takes a sequence
and returns an iterator that traverses the list in reverse order.
Debugging
---------
Lists, dictionaries and tuples are examples of **data structures**; in
this chapter we are starting to see compound data structures, like lists
of tuples, or dictionaries that contain tuples as keys and lists as
values. Compound data structures are useful, but they are prone to what
I call **shape errors**; that is, errors caused when a data structure
has the wrong type, size, or structure. For example, if you are
expecting a list with one integer and I give you a plain old integer
(not in a list), it won’t work.
To help debug these kinds of errors, I have written a module called
structshape that provides a function, also called structshape, that
takes any kind of data structure as an argument and returns a string
that summarizes its shape. You can download it from
http://thinkpython2.com/code/structshape.py
Here’s the result for a simple list:
::
>>> from structshape import structshape
>>> t = [1, 2, 3]
>>> structshape(t)
'list of 3 int'
A fancier program might write “list of 3 int\ *s*”, but it was easier
not to deal with plurals. Here’s a list of lists:
::
>>> t2 = [[1,2], [3,4], [5,6]]
>>> structshape(t2)
'list of 3 list of 2 int'
If the elements of the list are not the same type, structshape groups
them, in order, by type:
::
>>> t3 = [1, 2, 3, 4.0, '5', '6', [7], [8], 9]
>>> structshape(t3)
'list of (3 int, float, 2 str, 2 list of int, int)'
Here’s a list of tuples:
::
>>> s = 'abc'
>>> lt = list(zip(t, s))
>>> structshape(lt)
'list of 3 tuple of (int, str)'
And here’s a dictionary with 3 items that map integers to strings.
::
>>> d = dict(lt)
>>> structshape(d)
'dict of 3 int->str'
If you are having trouble keeping track of your data structures,
structshape can help.
.. _glossary12:
Glossary
--------
.. include:: glossary/12.txt
Exercises
---------
Write a function called ``most_frequent`` that takes a string and prints
the letters in decreasing order of frequency. Find text samples from
several different languages and see how letter frequency varies between
languages. Compare your results with the tables at
http://en.wikipedia.org/wiki/Letter_frequencies. Solution:
http://thinkpython2.com/code/most_frequent.py.
[anagrams]
More anagrams!
#. Write a program that reads a word list from a file (see
Section [wordlist]) and prints all the sets of words that are
anagrams.
Here is an example of what the output might look like:
::
['deltas', 'desalt', 'lasted', 'salted', 'slated', 'staled']
['retainers', 'ternaries']
['generating', 'greatening']
['resmelts', 'smelters', 'termless']
Hint: you might want to build a dictionary that maps from a
collection of letters to a list of words that can be spelled with
those letters. The question is, how can you represent the collection
of letters in a way that can be used as a key?
#. Modify the previous program so that it prints the longest list of
anagrams first, followed by the second longest, and so on.
#. In Scrabble a “bingo” is when you play all seven tiles in your rack,
along with a letter on the board, to form an eight-letter word. What
collection of 8 letters forms the most possible bingos? Hint: there
are seven.
Solution: http://thinkpython2.com/code/anagram_sets.py.
Two words form a “metathesis pair” if you can transform one into the
other by swapping two letters; for example, “converse” and “conserve”.
Write a program that finds all of the metathesis pairs in the
dictionary. Hint: don’t test all pairs of words, and don’t test all
possible swaps. Solution: http://thinkpython2.com/code/metathesis.py.
Credit: This exercise is inspired by an example at http://puzzlers.org.
Here’s another Car Talk Puzzler
(http://www.cartalk.com/content/puzzlers):
What is the longest English word, that remains a valid English word,
as you remove its letters one at a time?
Now, letters can be removed from either end, or the middle, but you
can’t rearrange any of the letters. Every time you drop a letter,
you wind up with another English word. If you do that, you’re
eventually going to wind up with one letter and that too is going to
be an English word—one that’s found in the dictionary. I want to
know what’s the longest word and how many letters does it have?
I’m going to give you a little modest example: Sprite. Ok? You start
off with sprite, you take a letter off, one from the interior of the
word, take the r away, and we’re left with the word spite, then we
take the e off the end, we’re left with spit, we take the s off,
we’re left with pit, it, and I.
Write a program to find all words that can be reduced in this way, and
then find the longest one.
This exercise is a little more challenging than most, so here are some
suggestions:
#. You might want to write a function that takes a word and computes a
list of all the words that can be formed by removing one letter.
These are the “children” of the word.
#. Recursively, a word is reducible if any of its children are
reducible. As a base case, you can consider the empty string
reducible.
#. The wordlist I provided, words.txt, doesn’t contain single letter
words. So you might want to add “I”, “a”, and the empty string.
#. To improve the performance of your program, you might want to memoize
the words that are known to be reducible.
Solution: http://thinkpython2.com/code/reducible.py.