Data Structures

Ace your homework & exams now with Quizwiz!

Generator

We can create functions that do not just return one result, but rather an entire sequence of results, by using the yield statement. These functions are called ...s Python contains generator functions, which are an easy way to create iterators and they are especially useful as a replacement for unworkably long lists. A ... yields items rather than build lists.

Dictionaries

... are arbitrary collections of objects indexed by numbers, strings, or other immutable objects. ... themselves are mutable; however, their index keys must be immutable. ... comprehension example: > {str(x):y for x,y in zip(range(0, 4),[1,2,3,'a']) } {'0': 1, '1': 2, '2': 3, '3': 'a'}

strings

... are immutable sequence objects, with each character representing an element in the sequence.

Tuples

... are immutable sequences of arbitrary objects. They are indexed by integers greater than zero. ... are hashable, which means we can sort lists of them and they can be used as keys to dictionaries. Syntactically, ... are just a comma-separated sequence of values; however, it is common practice to enclose them in parentheses: tpl= ('a', 'b', 'c')

lists

... are probably the most used built-in data structures in Python because they can be composed of any number of other data types. They are a simple representation of arbitrary objects. Like strings, they are indexed by integers starting with zero

Sets

... are unordered collections of unique items. ... are themselves mutable, we can add and remove items from them; however, the items themselves must be immutable. An important distinction with ... is that they cannot contain duplicate items. ... are typically used to perform mathematical operations such as intersection, union, difference, and complement.

Counter

... is a subclass of a dictionary where each dictionary key is a hashable object and the associated value is an integer count of that object. There are three ways to initialize a .... We can pass it any sequence object, a dictionary of key:value pairs, or a tuple of the format (object = value, ...),

Sequence

...s are ordered sets of objects indexed by non-negative integers. Lists and tuples are ... of arbitrary objects, strings are ...s of characters. String, tuple, and range objects are immutable. All ... types have a number of operations in common. For all ...s, the indexing and slicing operators apply as described in the previous chapter. Note that for the immutable types, any operation will only return a value rather than actually change the value.

Doubly Linked List (DLL)

A ... contains an extra pointer, typically called previous pointer, together with next pointer and data which are there in singly linked list

stack

A ... is a data structure that is often likened to a ... of plates. If you have just washed a plate, you put it on top of the .... When you need a plate, you take it off the top of the .... So the last plate to be added to the ... will be the first to be removed from the .... Thus, a ... is a *last in, first out (LIFO)*

Tree

A ... is a hierarchical form of data structure. When we dealt with lists, queues, and stacks, items followed each other. But in a ..., there is a parent-child relationship between items. To visualize what ...s look like, imagine a ... growing up from the ground. Now remove that image from your mind. ...s are normally drawn downward, so you would be better off imagining the root structure of the ... growing downward. At the top of every ... is the so-called root node. This is the ancestor of all other nodes in the ....

linked list

A ... is a sequence of data elements, which are connected together via links. Each data element contains a connection to another data element in form of a pointer. Python does not have ...s in its standard library. We implement the concept of ...s using the concept of nodes

Binary search tree (BST)

A ... is a special kind of a binary tree. That is, it is a tree that is structurally a binary tree. Functionally, it is a tree that stores its nodes in such a way to be able to search through the tree efficiently. There is a structure to a ... For a given node with a value, all the nodes in the left sub-tree are less than or equal to the value of that node. Also, all the nodes in the right sub-tree of this node are greater than that of the parent node.

heap

A ... is a specialization of a tree in which the nodes are ordered in a specific way. ...s are divided into max and min ...s. In a max ..., each parent node must always be greater than or equal to its children. It follows that the root node must be the greatest value in the tree. A min ... is the opposite. Each parent node must be less than or equal to both its children. As a consequence, the root node holds the lowest value. ...s are used for a number of different things. For one, they are used to implement priority queues. There is also a very efficient sorting algorithm, called ... sort, that uses ...s. We are going to study these in depth in subsequent chapters.

Numeric Types

All ... types, apart from bool, are signed and they are all immutable. Booleans have two possible values, True and False. These values are mapped to 1 and 0, respectively. The integer type, int, represents whole numbers of unlimited range. Floating point numbers are represented by the native double precision floating point representation of the machine. Complex numbers are represented by two floating point numbers. They are assigned using the j operator to signify the imaginary part of the complex number, for example: a = 2+3j We can access the real and imaginary parts with a.real and a.imag, respectively.

deques

Double-ended queues, or ... (usually pronounced decks), are list-like objects that support thread-safe, memory-efficient appends. ... are mutable and support some of the operations of lists, such as indexing. ... can be assigned by index, for example, dq[1] = z; however, we cannot directly slice .... For example, dq[1:2] results in a TypeError (we will look at a way to return a slice from a ... as a list shortly). A useful feature of ... is that they support a maxlen optional parameter that restricts the size of the .... This makes it ideally suited to a data structure known as a circular buffer. This is a fixed-size structure that is effectively connected end to end and they are typically used for buffering data streams.

Higher order functions

Functions that take other functions as arguments, or that return functions, are called .... Python 3 contains two built-in ..., filter() and map(). Note that in earlier versions of Python, these functions returned lists; in Python 3, they return an iterator, making them much more efficient. The map() function provides an easy way to transform each item into an iterable object. For example, here is an efficient, compact way to perform an operation on a sequence.

Functions as first class objects

In Python, it is not only data types that are treated as objects. Both functions and classes are what are known as first class objects, allowing them to be manipulated in the same ways as built-in data types. By definition, first class objects are: • Created at runtime • Assigned as a variable or in a data structure • Passed as an argument to a function • Returned as the result of a function

Immutable set

Python has an ... type called frozenset. It works pretty much exactly like set apart from not allowing methods or operations that change values such as the add() or clear() methods. There are several ways that this immutability can be useful. For example, since normal sets are mutable and therefore not hashable, they cannot be used as members of other sets.

Stack usage

Stacks are actually also used to pass data between functions. Say you have the following function call somewhere in your code: somefunc(14, 'eggs', 'ham', 'spam') What is going to happen is that 14, 'eggs', 'ham' and 'spam' will be pushed onto the stack, one at a time:

Circular Linked List

The ... a variant of the singly linked list where the last Node links to the head rather than None. Since the list contains a circular reference to the Head, we can navigate a ... starting at any index rather than starting at the head node.

namedtuple

The ... method returns a tuple-like object that has fields accessible with named indexes as well as the integer indexes of normal tuples. This allows for code that is, to a certain extent, self-documenting and more readable. It can be especially useful in an application where there is a large number of tuples and we need to easily keep track of what each tuple represents. The ... inherits methods from tuple and it is backward-compatible with tuple. space = ...('space', 'x y z') s1 = space(1,2,3) s1 space(x=1, y=2, z=3)

array

The ... module defines a datatype ... that is similar to the list datatype except for the constraint that their contents must be of a single type of the underlying representation, as is determined by the machine architecture or underlying C implementation.

collections

The ... module provides more specialized, high, performance alternatives for the built-in data types as well as a utility function to create named tuples.

defaultdict

The ... object is a subclass of dict and therefore they share methods and operations. It acts as a convenient way to initialize dictionaries. With a dict, Python will throw a KeyError when attempting to access a key that is not already in the dictionary. The ... overrides one method, __missing__(key), and creates a new instance variable, default_factory. Example: dd = ...(lambda:0 ,{'a':1,'b':2,'c':3}) dd[0] 0 dd[1] 0 dd['a'] 1

None type

The ... type is *immutable* and has one value, None. It is used to represent the absence of a value. It is returned by objects that do not explicitly return a value and evaluates to False in Boolean expressions. It is often used as the default value in optional arguments to allow the function to detect whether the caller has passed a value.

chainmap

The collections.(...) class was added in Python 3.2 and it provides a way to link a number of dictionaries, or other mappings, so that they can be treated as one object. In addition, there is a maps attribute, a new_child() method, and a parents property. The underlying mappings for ... objects are stored in a list and are accessible using the maps[i] attribute to retrieve the ith dictionary. Note that even though dictionaries themselves are unordered, ... are an ordered list of dictionaries. ... is useful in applications where we are using a number of dictionaries containing related data. The consuming application expects data in terms of a priority, where the same key in two dictionaries is given priority if it occurs at the beginning of the underlying list.... is typically used to simulate nested contexts such as when we have multiple overriding configuration settings.

Ordered dictionaries

The important thing about ... is that they remember the insertion order, so when we iterate over them, they return values in the order they were inserted. This is in contrast to a normal dictionary, where the order is arbitrary. When we test to see whether two dictionaries are equal, this equality is only based on their keys and values; however, with an ..., the insertion order is also considered An equality test between two ... with the same keys and values but a different insertion order will return False

queue

The way a ... works is that the first person to join the ... usually gets served first, all things being equal. The acronym FIFO best explains this. *FIFO stands for first in, first out*.


Related study sets

Chapter 9: Multiple Regression-FIN 360

View Set

305 Financial Chapter 1 (Part 2)

View Set