Arrays
From charlesreid1
Notes on arrays and array-like structures.
Notes
Goodrich Python Chapter 5 Notes
Questions
Goodrich Python Questions
See also Arrays/Python
exploring relationship between list length and size
Question 5-1: execute the code fragment, and compare the results to those in the book.
The code fragment starts with an empty list, dynamically growing the list by appending items to the end, and showing how the size of the list is doubled each time the list gets bigger.
This also demonstrates the importance of declaring a list size if you know it! It's really expensive to keep resizing a list. (Remember the days of Matlab - you'd get an order of magnitude savings in time simply by declaring the array straight out.
Key function:
sys.getsizeof(data)to get the size of a list in bytes
The code populates the list with None (null) objects, which take up as much space as pointers to addresses in memory (64 bits on a 64 bit memory/computer)
import sys
data = []
n = 50
for k in range(n+1):
a = len(data)
b = sys.getsizeof(data)
print("Length: {0:3d}\tSize in bytes: {1:6d}".format(a,b))
data.append(None)
import sys <pre> $ python list_length_sizeof.py Length: 0 Size in bytes: 72 Length: 1 Size in bytes: 104 Length: 2 Size in bytes: 104 Length: 3 Size in bytes: 104 Length: 4 Size in bytes: 104 Length: 5 Size in bytes: 136 Length: 6 Size in bytes: 136 Length: 7 Size in bytes: 136 Length: 8 Size in bytes: 136 Length: 9 Size in bytes: 200 Length: 10 Size in bytes: 200 Length: 11 Size in bytes: 200 Length: 12 Size in bytes: 200 Length: 13 Size in bytes: 200 Length: 14 Size in bytes: 200 Length: 15 Size in bytes: 200 Length: 16 Size in bytes: 200 Length: 17 Size in bytes: 272 Length: 18 Size in bytes: 272 Length: 19 Size in bytes: 272 Length: 20 Size in bytes: 272 Length: 21 Size in bytes: 272 Length: 22 Size in bytes: 272 Length: 23 Size in bytes: 272 Length: 24 Size in bytes: 272 Length: 25 Size in bytes: 272 Length: 26 Size in bytes: 352 Length: 27 Size in bytes: 352 Length: 28 Size in bytes: 352 Length: 29 Size in bytes: 352 Length: 30 Size in bytes: 352 Length: 31 Size in bytes: 352 Length: 32 Size in bytes: 352 Length: 33 Size in bytes: 352 Length: 34 Size in bytes: 352 Length: 35 Size in bytes: 352 Length: 36 Size in bytes: 440 Length: 37 Size in bytes: 440 Length: 38 Size in bytes: 440 Length: 39 Size in bytes: 440 Length: 40 Size in bytes: 440 Length: 41 Size in bytes: 440 Length: 42 Size in bytes: 440 Length: 43 Size in bytes: 440 Length: 44 Size in bytes: 440 Length: 45 Size in bytes: 440 Length: 46 Size in bytes: 440 Length: 47 Size in bytes: 536 Length: 48 Size in bytes: 536 Length: 49 Size in bytes: 536 Length: 50 Size in bytes: 536
| Data Structures Part of Computer Science Notes
This is the staging ground for computer science notes as part of the 2017 CS Study Plan.
Classes of data structures: Abstract Data Types Array-based and Link-based memory management: ArrayLists and Linked Lists Algorithmic Analysis of Data Structures: Algorithmic Analysis of Data Structures Advanced data structures: Advanced Data Structures
|
| Arrays Part of Computer Science Notes
Series on Data Structures Python: Arrays/Python · Arrays/Python/Sizeof · Arrays/Python/AppendCost · Arrays/Python/CaesarCipher · Arrays/Python/CompactArrays · Arrays/Python/DynamicArray Java: Arrays/Java · Arrays/Java/CaesarCipher · Arrays/Java/FisherYates · Arrays/Java/PythonList · Arrays/Java/Repeatedly_Remove Categories: Category:Python Arrays
|
| Stacks and Queues Part of Computer Science Notes
Series on Data Structures
Stacks and Queues: Python StacksQueues/Python · StacksQueues/Python/ArrayStack · StacksQueues/Python/ArrayQueue · StacksQueues/Python/ArrayDeque StacksQueues/Python/LinkedStack
Stacks and Queues: Java StacksQueues/Java · StacksQueues/Java/ArrayStack · StacksQueues/Java/ArrayQueue · StacksQueues/Java/ArrayQueueFS · StacksQueues/Java/ArrayDeque StacksQueues/Java/LinkedStack · StacksQueues/Java/LinkedQueue · StacksQueues/Java/LinkedDeque
Applications Postfix_Expressions#Stacks · StacksQueues/Subsets · StacksQueues/Subsets/Java
|
| Priority Queues and Heaps Part of Computer Science Notes
Series on Data Structures
Java: Priority Queues/Java · Priority Queues/ADT · Priority Queues/Sorted · Priority Queues/Unsorted Performance: Priority Queues/Timing and Performance Applications: Maximum Oriented Priority Queue · Priority Queues/Stack
Priority Queues/Heap · Priority Queues/Java · Priority Queues/Comparators
|
| Linked List Part of Computer Science Notes
Series on Data Structures Java: Linked Lists/Java · Linked Lists/Java/Single · Linked Lists/Java/Double · Linked Lists/Java/Circular Performance: Linked Lists/Java/Timing · Linked Lists/Java/Reverse Python: Linked Lists/Python · Linked Lists/Python/Single
|
| Trees Part of Computer Science Notes
Series on Data Structures Abstract data type: Trees/ADT Concrete implementations: Trees/LinkedTree · Trees/ArrayTree · SimpleTree
Tree Traversal Preorder traversal: Trees/Preorder Postorder traversal: Trees/Postorder In-Order traversal: Binary Trees/Inorder Breadth-First Search: BFS Breadth-First Traversal: BFT Depth-First Search: DFS Depth-First Traversal: DFT OOP Principles for Traversal: Tree Traversal/OOP · Tree Traversal/Traversal Method Template Tree operations: Trees/Operations Performance · Trees/Removal
Tree Applications Finding Minimum in Log N Time: Tree/LogN Min Search
Abstract data type: Binary Trees/ADT Concrete implementations: Binary Trees/LinkedBinTree · Binary Trees/ArrayBinTree Binary Trees/Cheat Sheet · Binary Trees/OOP · Binary Trees/Implementation Notes
|
| Search Trees Part of Computer Science Notes
Series on Data Structures
Binary Search Trees · Balanced Search Trees Trees/OOP · Search Trees/OOP · Tree Traversal/OOP · Binary Trees/Inorder
(Note that heaps are also value-sorting trees with minimums at the top. See Template:PriorityQueuesFlag and Priority Queues.)
|
| Maps and Dictionaries Part of Computer Science Notes
Series on Data Structures
Maps/Dictionaries Maps · Maps/ADT · Maps in Java · Maps/OOP · Maps/Operations and Performance Map implementations: Maps/AbstractMap · Maps/UnsortedArrayMap · Maps/SortedArrayMap Dictionary implementations: Dictionaries/LinkedDict · Dictionaries/ArrayDict
Hashes Hash Maps/OOP · Hash Maps/Operations and Performance Hash Maps/Dynamic Resizing · Hash Maps/Collision Handling with Chaining Hash functions: Hash Functions · Hash Functions/Cyclic Permutation Hash map implementations: Hash Maps/AbstractHashMap · Hash Maps/ChainedHashMap
Skip Lists · Java/ConcurrentSkipList · Java implementations: SkipList
Sets Sets · Sets/ADT · Sets in Java · Sets/OOP · Multisets
|