Chapter 4.Dictionaries. 'Beginning Python From Novice to Professional' by Magnus Lie Hetland

DICTIONARIES

telephone numbers (and other numbers that may contain leading zeros) should be
represented as strings of digits—not integers.
There are keys and values,closed in a curly braces/

>>> phonebook = {'Alice':'1111','Beth':'2222','Cecil':'3333'}
>>> phonebook['Cecil']
'3333'
______________________________________________

dict function

>>> items = [('GG','FF'),('11',22)]
>>> items = dict(items)
>>> items
{'GG': 'FF', '11': 22}

It can be used with keyword arguments

>>> d = dict (name = "Gumby", age  = 42)
>>> d

{'age': 42, 'name': 'Gumby'}

_________________________________

BASIC DICTIONARY OPERATIONS

>>> d = dict(apple = 'gavno',android = 'fuflo')
>>> d
{'android': 'fuflo', 'apple': 'gavno'}
>>> len(d)                                    # len(k) 

2
>>> d['android']                          #d[k]
'fuflo'
>>> d['android'] = 'krutjak'       #d[k] = v   (changing the value of key)
>>> d
{'android': 'krutjak', 'apple': 'gavno'}
>>> del d['apple']                       #del d[k]
>>> d
{'android': 'krutjak'}
>>> 'android' in d                       #k in d 

True
_____________________________________________________________
adding a new pair

>>> d['windows'] = 'interesting'
>>> d
{'windows': 'interesting', 'android': 'krutjak'}
_____________________________________

PHONE BOOK

people = {
    'Dron' : {
        'phone':'1111',
        'addr':'LA'},
    'Beit' : {
        'phone':'2222',
        'addr':'NY'},
    'Grop' : {
        'phone':'3333',
         'addr':'CS'}
    }
labels = {'phone':'phone number',
          'addr':'address'}

name = raw_input('Enter the name: ')
request = raw_input('Enter the phone number(p) or address(a): ')
if request == 'p': key = 'phone'
if request == 'a': key = 'addr'

if name in people: print "%s's %s is %s." % (name,labels[key],people[name][key])

______________________________________________________________
>>> vegetables
{'strawberry': 100, 'apple': 5, 'grapes': 10}
1>>> 'Amount of strawberry is %(strawberry).f' % vegetables
'Amount of strawberry is 100'
__________________________________________________
Multipasting

>>> template = 'dfddfdfd %(title)s jjnjnvj %(title)s nknkn %(title)s %(text)s ddvvvdv'
>>> template
'dfddfdfd %(title)s jjnjnvj %(title)s nknkn %(title)s %(text)s ddvvvdv'
>>> info = {'title':'BRAVO','text':'army'}
>>> print template % info
dfddfdfd BRAVO jjnjnvj BRAVO nknkn BRAVO army ddvvvdv

_________________________________________________________________

DICTIONARY METHODS

.clear()

>>> x = {}
>>> y = x
>>> x['key']='value'
>>> y
{'key': 'value'}
>>> x={}
>>> x
{}
>>> y
{'key': 'value'}

and

>>> x = {}
>>> y = x
>>> x['key'] = 'value'
>>> y
{'key': 'value'}
>>> x.clear()
>>> x
{}
>>> y
{}
_______________________________

.copy()

>>> x
{'username': 'admin', 'machines': ['foo', 'bar', 'baz']}
>>> y = x.copy()
>>> y
{'username': 'admin', 'machines': ['foo', 'bar', 'baz']}
>>> y['username'] = 'mlh'
>>> y
{'username': 'mlh', 'machines': ['foo', 'bar', 'baz']}
>>> y['machines'].remove('bar')
>>> y
{'username': 'mlh', 'machines': ['foo', 'baz']}
>>> x
{'username': 'admin', 'machines': ['foo', 'baz']}

As you can see, when you replace a value in the copy, the original is unaffected. However,
if you modify a value (in place, without replacing it), the original is changed as well because the
same value is stored there (like the 'machines' list in this example).
_______________________________________________________________________

deepcopy()

>>> from copy import deepcopy
>>> d={}
>>> d['names'] = ['Alfred','Bertran']
>>> d
{'names': ['Alfred', 'Bertran']}
>>> c=d.copy()
>>> c

{'names': ['Alfred', 'Bertran']}
>>> dc = deepcopy(d)
>>> dc
{'names': ['Alfred', 'Bertran']}
>>> c['names'].append('Fred')
>>> c
{'names': ['Alfred', 'Bertran', 'Fred']}
>>> d
{'names': ['Alfred', 'Bertran', 'Fred']}
>>> dc
{'names': ['Alfred', 'Bertran']}
______________________________________

.fromkeys() 
In order to create unnamed dictionary

>>> {}.fromkeys(['age','name'])
{'age': None, 'name': None}
>>> dict.fromkeys(['glory','shame'])
{'shame': None, 'glory': None}
>>> dict.fromkeys(['glory','shame'], 'unknown')
{'shame': 'unknown', 'glory': 'unknown'}
_______________________________________

.get()

getting existing value

>>> f ={}
>>> f['name'] = 'Bred'
>>> f
{'name': 'Bred'}
>>> f.get('name')
'Bred'

getting unexisting value

>>> d
{}
>>> print d.get('name')
None
>>> print d.get('name','N/A')
N/A
____________________________________

.has_key()

(not available in Python 3)
>>> d={}
>>> d.has_key('name')
False
>>> d['name']='Eric'
>>> d
{'name': 'Eric'}
>>> d.has_key('name')
True
____________________________________

.items()

present as a list

>>> d
{'age': 42, 'name': 'Eric'}
>>> d.items()
[('age', 42), ('name', 'Eric')]
____________________________________

.iteritems()

Working in the same way, but returns an iterator instead of list

>>> d
{'age': 42, 'name': 'Eric'}
>>> it = d.iteritems()
>>> it

>>> list(it)
[('age', 42), ('name', 'Eric')]
______________________________________________________

keys and iterkeys
The keys method returns a list of the keys in the dictionary, while iterkeys returns an iterator
over the keys.
____________________________________________________________________________

.pop()

TO Get the value of the key and than removes the pair key-value

>>> d ={'x':1,'y':2}
>>> d
{'y': 2, 'x': 1}
>>> d.pop('x')
1
>>> d

{'y': 2}
___________________________________________

.popitem()

removes the last pair of the dictionary

>>> d = {'url':'http://ffffff','spam':'bred','python':'cool'}
>>> d
{'url': 'http://ffffff', 'python': 'cool', 'spam': 'bred'}
>>> d.popitem()
('url', 'http://ffffff')
>>> d
{'python': 'cool', 'spam': 'bred'}
__________________________________________

.setdefault()

>>> d = {}
>>> d.setdefault('name','N/A')
'N/A'
>>> d
{'name': 'N/A'}
>>> d['name']= 'Gumby'
>>> d
{'name': 'Gumby'}
>>> d.setdefault('name','N/A')
'Gumby'
>>> d
{'name': 'Gumby'}

___________________________________________

.update()

updating the ldictionary by anouther dictionary

>>> d
{'name': 'Gumby'}
>>> d = {'url': 'http://ffffff', 'python': 'cool', 'spam': 'bred'}
>>> x={'fignia':'understood'}
>>> d.update(x)
>>> d
{'url': 'http://ffffff', 'python': 'cool', 'fignia': 'understood', 'spam': 'bred'}
_____________________________________________________

.values and intervalues

returns values as a list

>>> d.values()
['http://ffffff', 'cool', 'understood', 'bred']

Popular posts from this blog

Ketamine: The Past, Present, and Potential Future of an Anesthetic Drug

Fast track anesthesia approaches