In [ ]:
# Dictionaries
# hashes,hash,dict
# ssn,UIN
# ABX123RTC : male,hyd,#301
# ABX1IRCTC : female,hyd,#301
# key : value
# key - unique
# value - repetative
In [1]:
my_fruits = {'a':'apple','b':'banana','c':'cherry','d':'dates'}
print my_fruits,type(my_fruits)
In [2]:
my_empty = {}
print my_empty,type(my_empty)
my_empty = dict()
print my_empty,type(my_empty)
In [3]:
# how to retrive an element from a dictionary
print my_fruits['b']
In [5]:
# modify
my_fruits['b']='brinjal'
print my_fruits
In [6]:
# adding a new key
my_fruits['f']='figs'
print my_fruits
In [10]:
my_fruits['a'] = ['apple','avacado']
print my_fruits
In [8]:
# in operation
# in operation works only on key in a dictionary.
print 'a' in my_fruits
print 'apple' in my_fruits
In [26]:
# dictionary is an iterator
for key in my_fruits:
print "key:{},value:{}".format(key,my_fruits[key])
In [ ]:
# task
# square of numbers from 1 to 10
# {1:1,2:4,.....,10:100}
In [11]:
my_square={}
num = int(raw_input("please enter the number:"))
for value in range(1,num+1):
my_square[value]=value*value
print my_square
In [13]:
# functions
print dir(my_fruits)
In [ ]:
# fromkeys,setdefault,update => google -> tutorialpoint
In [14]:
# get
print help(my_fruits.get)
In [15]:
print my_fruits.get('a')
print my_fruits['a']
In [17]:
# has_key
print help(my_fruits.has_key)
In [18]:
print my_fruits.has_key('a')
print 'a' in my_fruits
In [19]:
# keys
print help(my_fruits.keys)
In [20]:
print my_fruits.keys()
In [27]:
# iterkeys
print help(my_fruits.iterkeys)
In [29]:
print my_fruits.iterkeys()
for value in my_fruits.iterkeys():
print value
In [33]:
# viewkeys
print help(my_fruits.viewkeys)
print my_fruits.viewkeys()
In [36]:
# keys
print help(my_fruits.values)
In [37]:
print my_fruits.values()
In [31]:
# itervalues
print help(my_fruits.itervalues)
print my_fruits.itervalues()
for value in my_fruits.itervalues():
print value
In [ ]:
# viewvalues
In [23]:
# items
print help(my_fruits.items)
In [24]:
print my_fruits.items()
In [32]:
# iteritems
print help(my_fruits.iteritems)
print my_fruits.iteritems
for value in my_fruits.iteritems():
print value
In [ ]:
# viewitems
In [2]:
# copy
print help(my_fruits.copy)
In [14]:
# shallow copy works on complex objects
a = ['ant','ball','call','doll']
b = ['one','two','three','four']
Cc = [a,b]
print Cc
print id(Cc),id(a),id(Cc[0]),id(b),id(Cc[1])
# soft copy
Sc = Cc
print id(Sc),id(Cc)
print Sc is Cc
print Sc
print Cc
a[0]='Ant'
print Sc
print Cc
# deep copy
import copy
Dc = copy.deepcopy(Cc)
print id(Dc),id(Cc)
print Dc is Cc
print Dc
print id(Dc[0]),id(a),id(Dc[1]),id(b)
print Cc
b[0]="One"
print Dc
print Cc
In [15]:
# shallow copy
print help(copy.copy)
In [19]:
a = ['ant','ball','call','doll']
b = ['one','two','three','four']
Cc = [a,b]
print Cc
Sh = copy.copy(Cc)
print Sh
print id(Cc),id(a),id(Cc[0]),id(b),id(Cc[1])
print id(Sh),id(a),id(Sh[0]),id(b),id(Sh[1])
a[1] = "ballon"
print Cc,Sh
In [20]:
D_fruits = my_fruits.copy()
In [22]:
print D_fruits
print my_fruits
print D_fruits is my_fruits
In [23]:
# pop
print help(my_fruits.pop)
In [24]:
D_fruits.pop('d')
Out[24]:
In [25]:
print D_fruits
In [26]:
# popitem
print help(D_fruits.popitem)
In [27]:
print D_fruits.popitem()
In [28]:
print D_fruits.popitem()
In [29]:
print D_fruits
In [31]:
# clear
print help(D_fruits.clear)
In [32]:
D_fruits.clear()
In [33]:
print D_fruits
In [ ]:
#
# numpy,pandas
# http://tinyurl.com/TconSeminars