Dictionary Types
* Dictionary types with special properties
* Default Dictionary
* Counters
* Require the collections module
* includes several dictionary types (including the two just mentioned)
* can use all dictionary operators and methods
Dictionary Example 1
dname={'first': 'Kathryn', 'last': 'Janeway'}
namelist=[dname['first'], dname['last']]
name=''.join(namelist)
print(f'Character Name: {name}')
# Output
Character Name: Kathryn Janeway
Dictionary Example 2 (how to handle non existing keys)
dname={'first': 'Spock'}
namelist=[dname['first'], dname['last']]
name=''.join(namelist)
print(f'Character Name: {name}')
# Output
KeyError:'last'
dname={'first': 'Spock'}
if 'last' not in dname:
dname['last']=''
namelist=[dname['first'], dname['last']]
name=''.join(namelist)
print(f'Character Name: {name}')
# Output
# Character Name: Spock
Default Dictionary
import collections
collections.defaultdict(factory, dictionary=None)
* return a default object
* default values for nonexitent keys are generated by a factory function
### Default Dictionary Example1
import collections
#we create a default dictionary and pass two arguments to it
dname = collections.defaultdict(str, {'first':'Spock'})
# we default to an empty str
namelist=[dname['first'], dname['last']]
# dname will default to an empty string
name=''.join(namelist)
print(f'Character Name: {name}')
# Output
# Character Name: Spock
Default Dictionary Example2
import collections
d = collections.defaultdict(list)
#we create a default dictionary that defaults an empty list
mylist=[1,1,2,3,5,8,13,21,34]
for n in mylist:
if n%2==0:
key='even'
else:
key='odd'
d[key].append(n)
print(d)
# Output
# defaultdict(<class'list'>, {'odd': [1,1,3,5,13,21], 'even':[2,8,34]})
if we would have not used defaultdictionary, we would have to explicitly creat a new list as a value.
Counter
* Store count of items from a sequence in a dictionary
* items form a sequence and serce as dictionary keys.
* the values are the counts of the keys.
* dictionary key is the item.
* dictionary vlaue is the count.
Dictionary Count Example
```python
s= "Berkeley"
# this is our sequence
d={}
# this iwll store our count information
# letter is the key and account is the value
for c in s:
# each letter in s will be stored in c
if c not in d:
d[c]=0
d[c]+=1
#if the key does not exist we default the value to zero
print(d)
# Output
# {'B':1,'e':3,'r':1,'k':1,'l':1,'y':1,}
```
Default Dictionary Count Example
import collections
d = collections.defaultdict(int)
# int: default new items to the value zero
s= "Berkeley"
for c in s:
d[c]=d[c]+1
print(d)
# Output
# defaultdict(<class'int'>, {'B':1,'e':3,'r':1,'k':1,'l':1,'y':1,})
Syntax
import collections
collections.Counter(iterable)
* return a Counter object form a given iterable
* it consist of items from iterable as keys and their counts as values
Counter Example
The default dict class already has reduced our code,
but the counter class shrink our code to jsut one line (d = collections.Counter(s))
```python
import collections
s= "Berkeley"
d = collections.Counter(s)
print(d)
# Output
# Counter({'e':3,'B':1,'r':1,'k':1,'l':1,'y':1,})
```
Counter Update Method
Syntax
counter.update(iterable)
- update counter object counts with a given iterable
Most common example
import collections
s= "Berkeley"
d = collections.Counter(s)
print(d)
d.update(['k','e''y'])
print(d)
# Output
# Counter({'e':3,'B':1,'r':1,'k':1,'l':1,'y':1})
# Counter({'e':4,k':2,'y':2,'B':1,'r':1,''l':1})