|
| 1 | +There are seven builtin types in Python that are Sequences: |
| 2 | + |
| 3 | + strings |
| 4 | + unicode strings |
| 5 | + lists |
| 6 | + tuples |
| 7 | + bytearrays |
| 8 | + buffers |
| 9 | + array.arrays |
| 10 | + xrange objects (almost) |
| 11 | + |
| 12 | +We will be using mostly string types, lists, tuples -- the rest are special |
| 13 | + |
| 14 | +Everything we say today applies to all sequences -- with minor caveats |
| 15 | + |
| 16 | +Indexing: |
| 17 | + |
| 18 | + looking up by index using the subscription operator |
| 19 | + |
| 20 | + negative index = count from the end |
| 21 | + |
| 22 | + Asking for an index out of the range == index error |
| 23 | + |
| 24 | + slicing == getting a range of objects from the sequence |
| 25 | + |
| 26 | + [start:finish] |
| 27 | + |
| 28 | + The indices don't decribe the item itself, but rather the place BETWEEN the items |
| 29 | + |
| 30 | + You do not need to provide start ad finish |
| 31 | + |
| 32 | + Either 0 or len(string) will be assumed |
| 33 | + |
| 34 | + s[:-4] start to four from the end |
| 35 | + |
| 36 | + len(seq[a:]) == b-a |
| 37 | + |
| 38 | +Slicing takes a third argument = step -- which controls which items are returned |
| 39 | + |
| 40 | + a_thing[0:15] |
| 41 | + |
| 42 | + a_thing[0:15:2] = 0,2,4,6,8 <----WAT |
| 43 | + |
| 44 | + |
| 45 | +Slicing != indexing |
| 46 | + |
| 47 | + indexing will always return a single object |
| 48 | + slicing will always return a sequence, even if it's one object long |
| 49 | + |
| 50 | + |
| 51 | + a_tuple[:] == copy of a_tuple |
| 52 | + |
| 53 | + All sequences support the in and not in membership operators |
| 54 | + |
| 55 | + |
| 56 | + You can include LISTS in LISTS (yo dog) |
| 57 | + |
| 58 | + l = [1,3,4,5,[4,5]7,8] |
| 59 | + |
| 60 | + Sequences support concatenation |
| 61 | + |
| 62 | + |
| 63 | + Slicing will give you as much of a sequence as it can, even if the slice is way out of range (indexing will give an error) |
| 64 | + |
| 65 | + All sequences have a len() you can use it on anything (though it doesn't start at 0) |
| 66 | + |
| 67 | + min() = minimum value of a sequence |
| 68 | + |
| 69 | + max() = maximum value of a sequence |
| 70 | + |
| 71 | + ord(a) = value associated with a == 97 (from ASCII character set) |
| 72 | + |
| 73 | + string.index('a') = answers "where is a in string" |
| 74 | + |
| 75 | + Will give the first one if there are multiples |
| 76 | + |
| 77 | + string.count('a') will give how many of a in string |
| 78 | + |
| 79 | +PyCon - Raymond Hedinger - watch his stuff |
| 80 | + |
| 81 | + |
| 82 | +lists = [] <---square brackets |
| 83 | +list type object as a constructor -> list() "Try and turn this into a list" |
| 84 | + |
| 85 | +the elements of a list need not be a single type |
| 86 | + |
| 87 | +each element in a list is a value and can be in multiple lists and have multiple names (or no name) |
| 88 | + |
| 89 | +Tuples LOOK like a list, but use () instead of [] |
| 90 | + |
| 91 | +Tuples don't NEED parens. They are defined by commas, not parens. |
| 92 | + |
| 93 | +If you want a tuple with a single item, put a comma after it |
| 94 | + |
| 95 | +tuple() to create a tuple out of something else |
| 96 | + |
| 97 | +each element in a tuple is a value and can be in multiple tuples and have multiple names (or no name) |
| 98 | + |
| 99 | +Why do we have both? |
| 100 | + |
| 101 | +Objects are either mutable or immutable. |
| 102 | + |
| 103 | +mutable = can be changed in place |
| 104 | + |
| 105 | +immutable CAN NOT CHANGE |
| 106 | + |
| 107 | +Tuples are immutable |
| 108 | + |
| 109 | +Lists are MUTABLE: |
| 110 | + |
| 111 | + In [28]: food = ['spam', 'eggs', 'ham'] |
| 112 | + In [29]: food |
| 113 | + Out[29]: ['spam', 'eggs', 'ham'] |
| 114 | + In [30]: food[1] = 'raspberries' |
| 115 | + In [31]: food |
| 116 | + Out[31]: ['spam', 'raspberries', 'ham'] |
| 117 | + |
| 118 | +with a tuple: |
| 119 | + |
| 120 | + |
| 121 | + In [32]: food = ('spam', 'eggs', 'ham') |
| 122 | + In [33]: food |
| 123 | + Out[33]: ('spam', 'eggs', 'ham') |
| 124 | + In [34]: food[1] = 'raspberries' |
| 125 | + --------------------------------------------------------------------------- |
| 126 | + TypeError Traceback (most recent call last) |
| 127 | + <ipython-input-34-0c3401794933> in <module>() |
| 128 | + ----> 1 food[1] = 'raspberries' |
| 129 | + |
| 130 | + TypeError: 'tuple' object does not support item assignment |
| 131 | + |
| 132 | +You can use nested lists to create a pseudo 2D array |
| 133 | + |
| 134 | +Watch out for passing mutable objects as default values for function parameters |
| 135 | + |
| 136 | + list.append(add this to list) |
| 137 | + |
| 138 | +If you have something mutable as a default object (function param) set it =None in the paramters |
| 139 | + |
| 140 | + def function(x, a_list=None) |
| 141 | + |
| 142 | +a_list[2:3] = [5,6,7,8,] |
| 143 | +a_list |
| 144 | + ]34,56,5,6,7,8,23,56] |
| 145 | + |
| 146 | +Growing the list: |
| 147 | + |
| 148 | + .append |
| 149 | + .insert (add at this index) |
| 150 | + .extend (a whole bunch of stuff on the end, add the items themselves (if it's a list, unpack the items)) |
| 151 | + |
| 152 | +Making the list smaller: |
| 153 | + |
| 154 | + .pop (remove last item off the list and return) |
| 155 | + .remove (remove the last item of the list, or whichever one you want to remove) |
| 156 | + |
| 157 | + you can use del to delete slices of a list del nums[1:6:2] |
| 158 | + |
| 159 | +Slicing makes a copy |
| 160 | + |
| 161 | + In [249]: food = ['spam', ['eggs', 'ham']] |
| 162 | + In [251]: food_copy = food[:] |
| 163 | + In [252]: food[1].pop() |
| 164 | + Out[252]: 'ham' |
| 165 | + In [253]: food |
| 166 | + Out[253]: ['spam', ['eggs']] |
| 167 | + In [256]: food.pop(0) |
| 168 | + Out[256]: 'spam' |
| 169 | + In [257]: food |
| 170 | + Out[257]: [['eggs']] |
| 171 | + In [258]: food_copy |
| 172 | + Out[258]: ['spam', ['eggs']] |
| 173 | + |
| 174 | + WATCH OUT FOR REFERENCES - first line here, the second list is copied AND referenced, changing the original changes the copy |
| 175 | + |
| 176 | +To iterate over and remove, make a copy of the list and mutate the original |
| 177 | + list = range(10) |
| 178 | + for x in list[:] |
| 179 | + etc. |
| 180 | + |
| 181 | +You can iterate over any sequence with for |
| 182 | + .reverse reverses the order of the list without returning |
| 183 | + .sort will sort the list |
| 184 | + |
| 185 | + A METHOD THAT CHANGES SOMETHING IN PLACE RETURNS NONE |
| 186 | + |
| 187 | + .sort can take a key parameter |
| 188 | + |
| 189 | + In [137]: def third_letter(string): |
| 190 | + .....: return string[2] |
| 191 | + .....: |
| 192 | + |
| 193 | + In [138]: food.sort(key=third_letter) |
| 194 | + In [139]: food |
| 195 | + Out[139]: ['spam', 'eggs', 'ham'] |
| 196 | + |
| 197 | + Indexing is fast and constant time: O(1) |
| 198 | + x in s proportional to n: O(n) |
| 199 | + visiting all is proportional to n: O(n) |
| 200 | + operating on the end of list is fast and constant time: O(1) |
| 201 | + append(), pop() |
| 202 | + operating on the front (or middle) of the list depends on n: O(n) |
| 203 | + pop(0), insert(0, v) |
| 204 | + But, reversing is fast. Also, collections.deque |
| 205 | + |
| 206 | + http://wiki.python.org/moin/TimeComplexity |
0 commit comments