The example code blocks from the book Hands-On Data Structures and Algorithms with Python - Second Edition that I read.
Method | Description |
---|---|
s.capitalize | Returns a string with only the first character capitalized, the rest remaining lowercase. |
s.count(substring,[start,end]) | Counts occurrences of a substring. Replaces tabs with spaces. |
s.expandtabs([tabsize]) | Returns True if a string ends with a specified substring. |
s.endswith(substring,[start, end]) | Returns index of first presence of a substring. Returns True if all chars are alphanumeric of string s. |
s.find(substring,[start,end]) s.isalnum() | Returns True if all chars are alphabetic of string s. |
s.isalpha() s.isdigit() | Returns True if all chars are digits in the string. |
s.split([separator],[maxsplit]) | Splits a string separated by whitespace or an optional separator. Returns a list. |
s.join(t) | Joins the strings in sequence t. |
s.lower() | Converts the string to all lowercase. Replaces old substring with a new substring. |
s.replace(old, new[maxreplace]) | Returns True if the string starts with a specified substring. |
s.startswith(substring, [start, end]]) | Returns a copy of the string with swapped case in the string. |
s.swapcase() | Removes whitespace or optional characters. |
s.strip([characters]) | Returns a copy of the string with leading characters removed. |
Method | Description |
---|---|
len(a) | Provides the total number of elements in the a set. |
a.copy() | Provides another copy of the a set. |
a.difference(t) | Provides a set of elements that are in the a set but not in t. |
a.intersection(t) | Provides a set of elements that are in both sets, a and t. |
a.isdisjoint(t) | Returns True if no element is common in both the sets, a and t. |
a.issubset(t) | Returns True if all of the elements of the a set are also in the t set. |
a.issuperset(t) | Returns True if all of the elements of the t set are also in the a set. |
a.symmetric_difference(t) | Returns a set of elements that are in either the a or t sets, but not in both. |
a.union(t) | Returns a set of elements that are in either the a or t sets. |
s.add(item) | Adds an item to s; nothing happens if the item is already added. |
s.clear() | Removes all elements from the set, s. |
s.difference_update(t) | Removes those elements from the s set that are also in the other set, t. |
s.discard(item) | Removes the item from the set, s. |
Method | Description |
---|---|
len(a) | Provides the total number of elements in the a set. |
a.copy() | Provides another copy of the a set. |
a.difference(t) | Provides a set of elements that are in the a set but not in t. |
a.intersection(t) | Provides a set of elements that are in both sets, a and t. |
a.isdisjoint(t) | Returns True if no element is common in both the sets, a and t. |
a.issubset(t) | Returns True if all of the elements of the a set are also in the t set. |
a.issuperset(t) | Returns True if all of the elements of the t set are also in the a set. |
a.symmetric_difference(t) | Returns a set of elements that are in either the a or t sets, but not in both. |
a.union(t) | Returns a set of elements that are in either the a or t sets. |
s.add(item) | Adds an item to s; nothing happens if the item is already added. |
s.clear() | Removes all elements from the set, s. |
s.difference_update(t) | Removes those elements from the s set that are also in the other set, t. |
s.discard(item) | Removes the item from the set, s. |
s.intersection_update(t) | Remove the items from the set, s, which are not in the intersection of the sets, s and t. |
s.pop() | Returns an arbitrary item from the set, s, and it removes it from the s set. |
s.remove(item) | Deletes the item from the s set. |
s.update(t) | Appends all of the items in an iterable object, t, to the s set. |
s.symetric_difference_update(t) | Deletes all of the elements from the s set that are not in the symmetric difference of the sets, s and t. |
Datatype or operation | Description |
---|---|
namedtuple() | Creates tuple subclasses with named fields. |
deque | Lists with fast appends and pops either end. |
ChainMap | Dictionary-like class to create a single view of multiple mappings. |
Counter | Dictionary subclass for counting hashable objects. |
OrderedDict | Dictionary subclass that remembers the entry order. |
defaultdict | Dictionary subclass that calls a function to supply missing values. |
UserDict UserList UserString | These three data types are simply wrappers for their underlying base classes. Their use has largely been supplanted by the ability to subclass their respective base classes directly. Can be used to access the underlying object as an attribute. |
Complexity class | Name | Example operations |
---|---|---|
O(1) | Constant | append, get item, set item. |
O(logn) | Logarithmic | Finding an element in a sorted array. |
O(n) | Linear | copy, insert, delete, iteration. |
nLogn | Linear-logarithmic | Sort a list, merge-sort. |
n^2 | Quadratic | Find the shortest path between two nodes in a graph. Nested loops. |
n^3 | Cubic | Matrix multiplication. |
2^n | Exponential | Towers of Hanoi problem, backtracking. |