Page 1 of 2 Data structures are what we create out of data and Python has some good standard data structures. Find out how named tuples and counters work in this extract from Programmer's Python: Everything is Data.
Programmer's Python Everything is Data
Is now available as a print book: Amazon
Contents
- Python – A Lightning Tour
- The Basic Data Type – Numbers
Extract: Bignum
- Truthy & Falsey
- Dates & Times
Extract Naive Dates ***NEW!!!
- Sequences, Lists & Tuples
Extract Sequences
- Strings
Extract Unicode Strings
- Regular Expressions
- The Dictionary
Extract The Dictionary
- Iterables, Sets & Generators
Extract Iterables
- Comprehensions
Extract Comprehensions
- Data Structures & Collections
Extract Stacks, Queues and Deques Extract Named Tuples and Counters
- Bits & Bit Manipulation
Extract Bits and BigNum
- Bytes
Extract Bytes And Strings Extract Byte Manipulation
- Binary Files
- Text Files
- Creating Custom Data Classes
Extract A Custom Data Class
- Python and Native Code
Extract Native Code Appendix I Python in Visual Studio Code Appendix II C Programming Using Visual Studio Code
<ASIN:1871962765>
<ASIN:1871962749>
<ASIN:1871962595>
<ASIN:B0CK71TQ17>
<ASIN:187196265X>
Data Structures & Collections
So far we have focused on the inbuilt data structures that Python provides as part of the core language – the list, tuple, set and dictionary. There are also data structures that are fundamental to programming that do not correspond to Python’s built-in data structures, but they are very easy to implement or they are implemented for you in a suitable module. In this chapter we look at the stack, the queue and the deque, which are supplied structures in other languages but not in Python. Most of the additional data structures are in the collections module – but it also has a number of classes – OrderedDict, defaultdict, UserDict, UserList and UserString – which aren’t particularly useful in modern Python 3 and hence are not covered in this chapter.
As an example of implementing a data structure making use of the supplied data structures we create a binary tree class. This is a good example of using both stacks and queues, but turns out to be more complicated than you might expect. It is re-used in later chapters as an example of how to add your own data structures.
In chapter but not in this extract
- The Stack
- The Queue
- Double-Ended Queue
Named Tuples
Although not in the same category of fundamental data structures, the named tuple is useful and also provided by the collections module. The named tuple works exactly like a tuple, but with the added feature of having named fields. In other languages this is often called a struct, but notice that Python’s named tuple is immutable. How to use named tuples as part of file I/O is described in Chapter 13.
The named tuple works in a slightly different way to the data structures we have been exploring. The constructor doesn’t return a named tuple object, but a new subclass of namedtuple:
type = namedtuple(typename, [field1, field2, field3…])
where typename is the name of the new type and field1 and so on are the names of the fields. The field names can be specified using any sequence type or as a single string with names separated by whitespace or commas. Notice that this implies that you can construct a named tuple with names derived from another source as a string or a sequence.
You can use the getattr(string) function to return the value of a field whose name is stored in string. For example:
from collections import namedtuple
Point = namedtuple("Point",["x","y"])
creates a new class called Point which can in turn be used to create instances of the Point type:
p = Point(10,-42)
Now p references a new Point instance and this is essentially a tuple with fields that can be accessed using the field names:
print(p.x,p.y)
or, using the standard index notation:
print(p[0],p[1])
Both display 10 -42.
You can use the rename = True parameter to replace any invalid field names with generated field names of the form _n where n is the position of the field. You can also set the defaults parameter to an iterable which sets the fields to default values:
Point = namedtuple("Point",["x","y"],defaults=(0,0))
p = Point()
If not enough defaults are provided they are used starting from the right-hand end of the field name list and any remaining fields have to be specified in the constructor.
Notice that named tuples are fairly efficient because the constructor for the type stores a dictionary mapping the field names to index position and this makes instances of the type use little more space that a standard tuple.
There are some extra methods:
-
_make(iterable) creates an instance using the iterable for the values
-
_asdict() converts the names and values to key values in a dictionary
-
_replace(name1=value1, name2=value2..) creates a new instance of the tuple with the specified fields updated to their new values
-
_fields() returns the field names as a string
-
_field_defaults() returns a dictionary of field names and defaults
|