-
Notifications
You must be signed in to change notification settings - Fork 0
/
immutable_data_structure.py
100 lines (77 loc) · 3.92 KB
/
immutable_data_structure.py
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
# named tuple
from collections import namedtuple
# Create a namedtuple type, Point
Point = namedtuple("Point", "x y")
issubclass(Point, tuple)
# Instantiate the new type
point = Point(2, 4)
point
# Dot notation to access coordinates
point.x
point.y
# Indexing to access coordinates
point[0]
point[1]
# Convert to dict
point._asdict()
# Error: Named tuples are immutable!
point.x = 100
#| Name | Field | Born | Nobel Prize? |
#|-----------------|------------|------|--------------|
#| Ada Lovelace | math | 1815 | no |
#| Emmy Noether | math | 1882 | no |
#| Marie Curie | math | 1867 | yes |
#| Tu Youyou | physics | 1930 | yes |
#| Vera Rubin | chemistry | 1928 | no |
#| Sally Ride | physics | 1951 | no |
# list of dict objects
scientists = [
{'name': 'Ada Lovelace', 'field': 'math', 'born': 1815, 'nobel': False},
{'name': 'Emy Noether', 'field': 'math', 'born': 1882, 'nobel': False},
{'name': 'Marie Curie', 'field': 'math', 'born': 1867, 'nobel': True},
{'name': 'Tu Youyou', 'field': 'physics', 'born': 1930, 'nobel': True},
{'name': 'Vera Rubin', 'field': 'chemistry', 'born': 1928, 'nobel': False},
{'name': 'Sally Ride', 'field': 'physics', 'born': 1951, 'nobel': False}
]
scientists[0]['name'] = 'Ed Lovelance' # mutable
scientists
# mutable data structure disadvantages
# 1 - code repetition (boilerplate keys)
# 2 - in multithread parallel processing, risk of data structure change while many threads access it (absence of resource lock)
# immutable data structure: record
import collections
from pprint import pprint
ScientistRecordBuilder = collections.namedtuple('ScientistRecordBuilder', ['name','field','born','nobel'])
print(ScientistRecordBuilder)
ada = ScientistRecordBuilder(name='Ada Lovelace', field= 'math', born=1815, nobel=False)
ada.name
ada.field
ada.name = 'Ed Lovelace' # immutable
emy = ScientistRecordBuilder(name='Emy Noether', field= 'math', born=1882, nobel=False)
marie = ScientistRecordBuilder(name='Marie Curie', field= 'math', born=1867, nobel=True)
tu = ScientistRecordBuilder(name='Tu Youyou', field= 'physics', born=1930, nobel=True)
vera = ScientistRecordBuilder(name='Vera Rubin', field= 'chemistry', born=1928, nobel=False)
sally = ScientistRecordBuilder(name='Sally Ride', field= 'physics', born=1928, nobel=False)
scientists = [ada,emy,marie,tu,vera,sally] # immutable datastructures (tuple) added to mutable datastructure (list)
pprint(scientists)
scientists[0].name = 'Ed Lovelace' # record is immutable
del scientists[0] # list is mutable
pprint(scientists)
# solid datastructure: fully immutable
scientists = (ada,emy,marie,tu,vera,sally) # immutable datastructures (tuple) added to immutable datastructure (tuple)
pprint(scientists)
del scientists[0] # immutable
# list of nested dict
scientists = [
{'name':'Ada Lovelace', 'field':'math', 'born':1815, 'nobel':False, 'affiliation':{'university':'Cambridge','country':'UK'}},
{'name':'Emy Noether', 'field':'math', 'born':1882, 'nobel':False, 'affiliation':{'university':'Erlangen','country':'DE'}},
{'name':'Marie Curie', 'field':'math', 'born':1867, 'nobel':True, 'affiliation':{'university':'Sorbonne','country':'FR'}},
{'name':'Tu Youyou', 'field':'physics', 'born':1930, 'nobel':True, 'affiliation':{'university':'Ningbo','country':'PRC'}},
{'name':'Vera Rubin', 'field':'chemistry', 'born':1928, 'nobel':False, 'affiliation':{'university':'Princeton','country':'USA'}},
{'name':'Sally Ride', 'field':'physics', 'born':1951, 'nobel':False, 'affiliation':{'university':'Stanford','country':'USA'}}
]
scientists
[scientist['name'] for scientist in scientists]
[scientist['affiliation']['university'] for scientist in scientists]
list(map(lambda x: x['affiliation']['university'], [scientist for scientist in scientists]))
list(map(lambda x: x['affiliation']['country'], [scientist for scientist in scientists]))