-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMOD_3_NOTES-Understanding_Data_Structures.txt
187 lines (135 loc) · 5.61 KB
/
MOD_3_NOTES-Understanding_Data_Structures.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
Transactions in Blockchain
A chain of blocks, all of which contain data and metadata. Each block has data about the previous block
on the previous transactions. Sender, Recipient, Amount.
Mining a block -> Providing the computational resources to process an unprocessed transaction (list of them).
Eventually, these will be added to a new block. Mining will result in a reward for mining a new block by
emptying list of outstanding transactions.
Need a data structure which stores key-value pairs of Transaction data (sender, recipient, amount), order
doesn't matter.
Outgoing list needs a data structure of mutable list of values, order doesn't matter
Data structure for the blockchain which is mutable, but order DOES matter
Each identifier should only occur once, and be unique
============
Iterables
------------
-> We can loop through them
List : [ ] : can hold multiple values, can nest lists within list, mutable, mostly one type, duplicates
are allowed, ordered.
Set: { } : mutable, unordered (order not guaranteed), *cannot have duplicates*, mostly one type.
Tuple: ( ) : Immutable, ordered list, duplicates allowed, *often mixed types. Good for hard-coded defaults
Good for grouping sorts of data, say key values, one after another.
Dictionary {'key':'val'} : Basically Maps. Mutable, *unordered map, no duplicate keys, often mixed types.
Unique identifier that has to be wrapped in quotations. KEYS must not be duplicated, but values can be
duplicates. UNIQUE KEY. Mixed data types!
Mapping data structures to parts of blockchain:
Transaction (sender, recipient, amount) -> stores key-value pairs, order not important.
Possibly a tuple, but a dictionary is better, since you always know what the key is
List of transactions -> Needs to be mutable, need duplicates, order doesn't matter.
List!
Blockchain -> Mutable, order matters.
List!
Single Block -> Hash and Index, transactions. key-value pairs
Dictionary! Could use tuple
Participant list -> unique values, order doesn't matter. Guarantees each val occurs only once
Set!
-> To access values within a dictionary:
Use [] on list to access the value
last_block = [ ... ]
for keys in last_block:
value = last_block[keys]
or last_block['key_name']
++++++===
List Comprehension
(Alternative to a loop applying logic to a list)
list = [1, 2, 3, 4]
double = []
For loop version
for element in list:
double.append(element * 2)
double = [2, 4, 6, 8]
------------------
List Comprehension
==================
double = [el * 2 for el in list]
read from right to left -> for el in list (for each element in each given list), apply logic to first el
FIRST EL mentioned is what will be outputted in new list
ADDING CONDITIONALS TO Comprehension
-> Allows us to apply conditional logic to comprehensions
Ex.
dup_list = [el * 2 for el in list if el %2 == 0]
calc_items = [1,2]
dup_list = [el * 2 for el in list if el in calc_items]
---------------
Dict Comprehension
Take a dictionary that convers tuples to key value pairs
stats = [('age', 29), ('weight', 75), ('height', 175)]
dict_stats = {key: value for (key, value) in stats}
=========
ENUMERATE
---------
Calling enumerate([]) on a list will return a tuple with key-value pairs, with the key corresponding
to the index of the list value
for (index, block) in enumerate(blockchain):
Creating a set -> via set() function, passing in a list
If you pass in a string, say ('Taddes'), you get '{'T', 'a', 'd', 'd', 'e', 's'}
set(['Taddes', 'Sarah', 'Pepper']) -> {'Taddes', 'Sarah', 'Pepper'}
============================
References vs Value Copying
-----------------------------
Everything but booleans and strings is copied by refernece, not by value.
Lists are stored in memory only ONCE. Stored in same place in memory.
my_list = [a, b, c, d, e]
^
dup_list = my_list |
Therefore: dup_lost[0] = z makes my_list = [z, b, c, d, e]
LIST IS NOT COPIED, REFERS TO SAME PLACE! Any change to one changes the other
How to properly copy a list
==========================
dup_list = my_list[:]
Range selector, that copies all elements in a list, creating a new list populated by new elements
============================
Range Selector [ : ]
-----------------------------
Select specific indexes in a string, tuple or list
Doesn't work in dictionary or set, because order not important in those data types
**NOT Inclusive of end element**
----
my_list = [a, b, c, d, e]
new_list = my_list[0:3]
new_list = [a, b, c ]
--- Negative indexing
my_list = [a, b, c, d, e]
new_list = my_list[:-1]
(start at first, go all the way to the right excluding rightmost)
new_list = [a, b, c, d ]
============================
Shallow vs. Deep Copy
-----------------------------
If a data structure contains another data structure
stats = [{'name':'Taddes'}, {'age': '30'}]
copied_stats = stats[:]
copied_stats[0]['name'] = 'Sarah'
copied_stats = [{'name':'Sarah'}, {'age': '30'}]
***! DOES NOT COPY/OVERWROTE ORIGINAL !*** Only a shallow copy
stats = [{'name':'Sarah'}, {'age': '30'}]
Shallow copy only copies the top level, so references in deeper data references original object
==============
!= vs. is
--------------
is compares if it is the exact same objet in memory, even if they hold same values
list_one = [1,2,3,4]
list_two = [1,2,3,4]
list_one == list_two // True
list_one is list_two // False
==============
all and any functions
--------------
Boolean Checks
new_list = [True, True, False]
any(all_list) -> True
all(all-_list) -> True
Any -> Checks whether any values are True in a list
All -> Checks whether all values are True in a list
How to use this to you advantage to filter through lists
numbers = [1,2,3,-5]
all([el > 0 for el in number])