-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdictExt.py
More file actions
64 lines (39 loc) · 1 KB
/
dictExt.py
File metadata and controls
64 lines (39 loc) · 1 KB
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
from collections import defaultdict
from collections import OrderedDict
import random
str_dict = defaultdict(str)
print 'missing key' + str_dict['foo']
float_dict = defaultdict(float)
print float_dict['foo']
int_dict = defaultdict(int)
print int_dict['foo']
int_dict['apples'] += 1
class Fraction(object):
def __init__(self):
self.n = 1
self.d = 2
def __repr__(self):
return '{0}/{1}'.format(self.n, self.d)
fraction_dict = defaultdict(Fraction)
print fraction_dict['foo']
def dne():
return 'DNE'
dne_dict = defaultdict(dne)
print dne_dict['foo']
print int_dict
print int_dict['foo']
int_dict['foo'] = 9
print int_dict['foo']
fruits = OrderedDict()
for f in ['apples', 'bananas', 'cherries', 'lemons', 'limes', 'oranges', 'peaches']:
fruits[f] = random.randint(50, 100)
print fruits
fruits['bananas'] = 50
print fruits
# not avialable in 2.7
#fruits.move_to_end('bananas')
fruits.popitem()
print fruits
fruits.popitem(False)
print fruits
print fruits['apples']