Beginning Python - Chapter4 : Dictionaries

# chapter4: dictionaries
# Question: why can not use dict function to init seq like dict(seq)
# dicts have no order
#1 init
dict={'lili':'01','lucy':'02'}
print dict['lili']
#2 basic dictionay operations
print len(dict)
print 'lucy' in dict
del dict['lucy']
dict['lili']='02'
print dict
#3 dictionary method
# -- clear
x = {}
y = x
x['no'] = 1
print y # {'no': 1},point same address
x = {}
print y # {'no': 1},no effect

x['no'] = 1
y = x
x.clear()
print "y is None too " , y # {}

# -- copy & deepcopy
# copy: x is the original dict, y is the copy one;
#        if you replace a value y, x is unaffected;
#        but if you modify(delete,add) y,x is affected
# deepcopy: x,y have no affect
from copy import deepcopy
x = {'no':'01','addr':['beijing','shanghai']}
# y = x.copy()
y = deepcopy(x)
x['no']='02'
print "x no change to 02,print y " , y # no affect
y['no']='03'
print "y no change to 03,print x " , x # no affect
x['addr'].remove('shanghai')
print "x remove shanghai,print y " , y # copy:affect deepcopy:no
y['addr'].remove('beijing')
print "y remove beijing,print x "  , x # copy:affect deepcopy:no

# -- fromkeys
# create a dict with keys
dict.fromkeys(['no','name']) #{'name': None, 'no': None}
# dict['no'] wrong
# print dict wrong
dict.fromkeys(['no','name'],'init') #{'name': 'init', 'no': 'init'}
 
# -- get
d = {}
# print d['name'] wrong
print d.get('name') # None
print d.get('name','N/A') #N/A

# -- has_key
# tip: has gone in Python 3.0 ; equivalent to 'in'
d = {}
print d.has_key('name') #false

# -- items & iteritems
# the items mothed returns all the items of the dict as a list of items (key,value),not particular order
# iteritems returns an iterator ,may be more efficient in many cases(especially if you want to iterate over the result)
dict={'lili':'01','lucy':'02'}
print dict.items() # [('lili', '01'), ('lucy', '02')]
print dict.iteritems() # <dictionary-itemiterator object at 0x015C9150>

# -- keys & iterkeys
# the keys method returns a list of keys in the dic;iterkeys returns an iterator over the keys
print dict.keys() #['lili', 'lucy']

# -- values & itervalues
# returns a list of values in the dict
d = {}
d[1] = 1
d[2] = 2
d[3] = 1
print '--',d.values() # [1, 2, 1]

# -- pop & popitem
# get the value corresponding to a given key, and then remove the key-value pair
# popitem: an efficient way, like pop, pop off the last element of a list,for dicts have no order, may pop off an arbitrary item
print dict.pop('lili') #01
print dict #{'lucy': '02'}

# -- setdefault
# set default value according to the key if key not exists and return the value; no key exists ,no change
d = {}
print d.setdefault('name','lili') #'lili'
print d #{'name':'lili'}
d['name'] = 'lucy'
print d #{'name':'lucy'}
print d.setdefault('name','lucy') #'lucy'
print d #{'name':'lucy'}

# -- update
# update on dict with the items of another if they have same key or add a new key-value pair
d1 = {'name':'sina','no':'11'}
d.update(d1)
print d #{'name': 'sina', 'no': '11'}

 #4 other example

dict={
    'lili':{'no':'01','addr':'beijing'},
    'lucy':{'no':'02','addr':'shanghai'}
    }
name=raw_input('please input name: ')

if name not in dict:
    print 'you are not in'
else:
    other=raw_input('please input other index condition:no(n) or addr(a)')
    if other=='n': key='no'
    if other=='a': key='addr'
    print  "%s's %s is %s" %(name,key,dict[name][key])
 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值