Data Structures Python Cheat Sheet Create Tuple tup1 = 4, 5, 6 or tup1 = (6,7,8) • Note : • 'start' index is included, but 'stop' index is NOT. just the basics Create Nested Tuple tup1 = (4,5,6), (7,8) • start/stop can be omitted in which they default to Convert Sequence or tuple([1, 0, 2]) the start/end. Iterator to Tuple Created By: Arianne Colton and Sean Chen Concatenate Tuples tup1 + tup2 Unpack Tuple a, b, c = tup1 § Application of 'step' : Application of Tuple Take every other element list1[::2] General Scalar Types Swap variables b, a = a, b Reverse a string str1[::-1] LIST DICT (HASH MAP) • Python is case sensitive * str(), bool(), int() and float() are also explicit type One dimensional, variable length, mutable (i.e. • Python index starts from 0 cast functions. contents can be modified) sequence of Python objects Create Dict dict1 = {'key1' :'value1', 2 of ANY type. :[3, 2]} • Python uses whitespace (tabs or spaces) to indent 5. NoneType(None) - Python 'null' value (ONLY Create Dict from dict(zip(keyList, code instead of using braces. list1 = [1, 'a', 3] or Sequence valueList)) one instance of None object exists) Create List list1 = list(tup1) HELP • None is not a reserved keyword but rather a Get/Set/Insert Element dict1['key1']* list1 + list2 or dict1['key1'] = 'newValue' unique instance of 'NoneType' Concatenate Lists* list1.extend(list2) dict1.get('key1', Help Home Page help() Get with Default Value defaultValue) ** • None is common default value for optional Append to End of List list1.append('b') Function Help help(str.replace) function arguments : Insert to Specific list1.insert(posIdx, Check if Key Exists 'key1' in dict1 Module Help help(re) Position 'b') ** Delete Element del dict1['key1'] def func1(a, b, c = None) valueAtIdx = list1. Get Key List dict1.keys() *** Inverse of Insert pop(posIdx) MODULE (AKA LIBRARY) • Common usage of None : Remove First Value list1.remove('a') Get Value List dict1.values() *** from List dict1.update(dict2) Python module is simply a '.py' file Update Values if variable is None : Check Membership 3 in list1 => True *** # dict1 values are replaced by dict2 List Module Contents dir(module1) Sort List list1.sort() Load Module import module1 * 6. datetime - built-in python 'datetime' module * 'KeyError' exception if the key does not exist. Sort with User- list1.sort(key = len) provides 'datetime', 'date', 'time' types. Call Function from Module module1.func1() Supplied Function # sort by length ** 'get()' by default (aka no 'defaultValue') will • 'datetime' combines information stored in 'date' and 'time' return 'None' if the key does not exist. * import statement creates a new namespace and * List concatenation using '+' is expensive since executes all the statements in the associated .py a new list must be created and objects copied *** Returns the lists of keys and values in the same dt1 = datetime. file within that namespace. If you want to load the Create datetime strptime('20091031', over. Thus, extend() is preferable. order. However, the order is not any particular module's content into current namespace, use 'from from String '%Y%m%d') order, aka it is most likely not sorted. module1 import * ' ** Insert is computationally expensive compared Get 'date' object dt1.date() with append. Valid dict key types Get 'time' object dt1.time() • Keys have to be immutable like scalar types (int, *** Checking that a list contains a value is lot slower float, string) or tuples (all the objects in the tuple Scalar Types Format datetime dt1.strftime('%m/%d/%Y to String %H:%M') than dicts and sets as Python makes a linear scan where others (based on hash tables) in need to be immutable too) Change Field dt2 = dt1.replace(minute = constant time. • The technical term here is 'hashability', Check data type : type(variable) Value 0, second = 30) check whether an object is hashable with the diff = dt1 - dt2 hash('this is string'), hash([1, 2]) Get Difference Built-in 'bisect module‡ SIX COMMONLY USED DATA TYPES # diff is a 'datetime.timedelta' object • Implements binary search and insertion into a - this would fail. 1. int/long* - Large int automatically converts to long Note : Most objects in Python are mutable except sorted list SET 2. float* - 64 bits, there is no 'double' type for 'strings' and 'tuples' • 'bisect.bisect' finds the location, where 'bisect. • A set is an unordered collection of UNIQUE insort' actually inserts into that location. elements. 3. bool* - True or False 4. str* - ASCII valued in Python 2.x and Unicode in Python 3 ‡ WARNING : bisect module functions do not • You can think of them like dicts but keys only. • String can be in single/double/triple quotes Data Structures check whether the list is sorted, doing so would be computationally expensive. Thus, using them Create Set set([3, 6, 3]) or {3, 6, 3} • String is a sequence of characters, thus can be in an unsorted list will succeed without error but Test Subset set1.issubset (set2) treated like other sequences may lead to incorrect results. Note : All non-Get function call i.e. list1.sort() Test Superset set1.issuperset (set2) • Special character can be done via \ or preface examples below are in-place (without creating a new with r SLICING FOR SEQUENCE TYPES† Test sets have same set1 == set2 object) operations unless noted otherwise. content str1 = r'this\f?ff' † Sequence types include 'str', 'array', 'tuple', 'list', etc. • Set operations : • String formatting can be done in a number of ways TUPLE Union(aka 'or') set1 | set2 One dimensional, fixed-length, immutable sequence Notation list1[start:stop] Intersection (aka 'and') set1 & set2 template = '%.2f %s haha $%d'; str1 = template % (4.88, 'hola', 2) of Python objects of ANY type. list1[start:stop:step] Difference set1 - set2 (If step is used) § Symmetric Difference (aka 'xor') set1 ^ set2 Functions Object-Oriented Exception Handling Python is pass by reference, function arguments • Application : Programming 1. Basic Form : are passed by reference. 1. 'object' is the root of all Python types try: sorted(set('abc bcd')) => [' ', .. • Basic Form : 'a', 'b', 'c', 'd'] 2. Everything (number, string, function, class, module, except ValueError as e: # returns sorted unique characters etc.) is an object, each object has a 'type'. Object print e def func1(posArg1, keywordArg1 = variable is a pointer to its location in memory. 1, ..): except (TypeError, AnotherError): 3. Zip pairs up elements of a number of lists, tuples or .. 3. All objects are reference-counted. except: other sequences to create a list of tuples : .. Note : sys.getrefcount(5) => x zip(seq1, seq2) => finally: • Keyword arguments MUST follow positional a = 5, b = a .. # clean up, e.g. close db arguments. [('seq1_1', 'seq2_1'), (..), ..] # This creates a 'reference' to the object on the • Python by default is NOT "lazy evaluation", right side of =, thus both a and b point to 5 2. Raise Exception Manually expressions are evaluated immediately. • Zip can take arbitrary number of sequences. However, the number of elements it produces is raise AssertionError # assertion failed sys.getrefcount(5) => x + 2 raise SystemExit # request program exit • Function Call Mechanism : determined by the 'shortest' sequence. del(a); sys.getrefcount(5) => x + 1 raise RuntimeError('Error message : 1. All functions are local to the module level • Application : Simultaneously iterating over multiple ..') scope. See 'Module' section. sequences : 4. Class Basic Form : 2. Internally, arguments are packed into a tuple for i, (a, b) in List, Set and Dict class MyObject(object): and dict, function receives a tuple 'args' and enumerate(zip(seq1, seq2)): # 'self' is equivalent of 'this' in Java/C++ dict 'kwargs' and internally unpack. • Common usage of 'Functions are objects' : • Unzip - another way to think about this is converting a list of rows to a list of columns. def __init__(self, name): self.name = name Comprehansions def func1(ops = [str.strip, user_ def memberFunc1(self, arg1): Syntactic sugar that makes code easier to read and write define_func, ..], ..): seq1, seq2 = zip(*zipOutput) for function in ops: .. 1. List comprehensions value = function(value) @staticmethod 4. Reversed iterates over the elements of a sequence • Concisely form a new list by filtering the elements def classFunc2(arg1): of a collection and transforming the elements RETURN VALUES in reverse order. passing the filter in one concise expression. .. • None is returned if end of function is reached list(reversed(range(10))) * obj1 = MyObject('name1') • Basic form : without encountering a return statement. obj1.memberFunc1('a') [expr for val in collection if condition] • Multiple values return via ONE tuple object * reversed() returns the iterator, list() makes MyObject.classFunc2('b') it a list. A shortcut for : return (value1, value2) 5. Useful interactive tool : result = [] value1, value2 = func1(..) dir(variable1) # list all methods available on for val in collection: ANONYMOUS (AKA LAMBDA) FUNCTIONS Control and Flow the object if condition: result.append(expr) • What is Anonymous function? A simple function consisting of a single statement. 1. Operators for conditions in 'if else' : The filter condition can be omitted, leaving only the lambda x : x * 2 Check if two variables are Common String expression. var1 is var2 2. Dict Comprehension # def func1(x) : return x * 2 same object operations • Basic form : . . . are different object var1 is not var2 • Application of lambda functions : 'curring' aka Concatenate ', '.join([ 'v1', 'v2', {key-expr : value-expr for value in Check if two variables have var1 == var2 deriving new functions from existing ones by same value List/Tuple with 'v3']) => 'v1, v2, v3' collection if condition} partial argument application. Separator 3. Set Comprehension WARNING : Use 'and', 'or', 'not' operators for string1 = 'My name is {0} ma60 = lambda x : pd.rolling_mean(x, {name}' • Basic form : same as List Comprehension except 60) compound conditions, not &&, ||, !. with curly braces instead of [] Format String newString1 = string1. 2. Common usage of 'for' operator : format('Sean', name = 4. Nested list Comprehensions USEFUL FUNCTIONS (FOR DATA STRUCTURES) 'Chen') Iterating over a collection (i.e. list for element in • Basic form : 1. Enumerate returns a sequence (i, value) tuples or tuple) or an iterator iterator : sep = '-'; where i is the index of current item. . . . If elements are sequences, for a, b, c in Split String stringList1 = [expr for val in collection for can be 'unpack' iterator : string1.split(sep) innerVal in val if condition] for i, value in enumerate(collection): 3. 'pass' - no-op statement. Used in blocks where no • Application : Create a dict mapping of value action is to be taken. Get Substring start = 1; string1[start:8] of a sequence (assumed to be unique) to their 4. Ternary Expression - aka less verbose 'if else' Created by Arianne Colton and Sean Chen locations in the sequence. [email protected] • Basic Form : month = '5'; 2. Sorted returns a new sorted list from any sequence value = true-expr if condition String Padding month.zfill(2) => '05' Based on content from else false-expr with Zeros month = '12'; 'Python for Data Analysis' by Wes McKinney sorted([2, 1, 3]) => [1, 2, 3] month.zfill(2) => '12' 5. No switch/case statement, use if/elif instead. Updated: May 3, 2016
Enter the password to open this PDF file:
-
-
-
-
-
-
-
-
-
-
-
-