Dictionary

  • a key match a value
  • there are several methods to create a dictionary.
  • it is similar to struct in C, and list in R
In [8]:
d0 = {'wang':3000,'li':4000,'yang':5000}
d0
Out[8]:
{'wang': 3000, 'li': 4000, 'yang': 5000}
In [10]:
d2 = [('wang',3000),('li',4000),('yang',5000)]
dict(d2)
Out[10]:
{'wang': 3000, 'li': 4000, 'yang': 5000}
In [4]:
d = dict((('wang',3000),('li',4000),('yang',5000)))
d
Out[4]:
{'wang': 3000, 'li': 4000, 'yang': 5000}
In [7]:
d1 = {}.fromkeys(('wang','li','yang'),3000)
sorted(d1)
Out[7]:
['li', 'wang', 'yang']
In [1]:
d3 = dict(wang=3000,li=4000,yang=5000)
d3
Out[1]:
{'wang': 3000, 'li': 4000, 'yang': 5000}
In [22]:
names = ['wang','li','yang']
salaries = [3000,4000,5000]
d4 = dict(zip(names,salaries))
d4
Out[22]:
{'wang': 3000, 'li': 4000, 'yang': 5000}
In [5]:
data = [('SUFE','Shanghai University of Finance and Economics','8000'),
        ('MSU','Michigan State University','30000'),
        ('NWU','Northwestern University','9000')]
tmp1 = []
tmp2 = []
for i in range(3):
    tmp1.append(data[i][0])
    tmp2.append(data[i][2])

d5 = dict(zip(tmp1,tmp2))
d5
Out[5]:
{'SUFE': '8000', 'MSU': '30000', 'NWU': '9000'}
In [23]:
for k,v in d4.items():
    d4[k] += 1000

d4
Out[23]:
{'wang': 4000, 'li': 5000, 'yang': 6000}
In [20]:
d4.keys()
d4.values()
d4.items()
Out[20]:
dict_items([('wang', 4000), ('li', 5000), ('yang', 6000)])
In [24]:
d5 = {'wang':6000,'niu':4000,'liu':7000}
d5.update(d4)
d5
Out[24]:
{'wang': 4000, 'niu': 4000, 'liu': 7000, 'li': 5000, 'yang': 6000}
In [25]:
d5.get('wang')
Out[25]:
4000

Note

  • If assign a dictionary to another,both share the same address,that is,they posess one point.
In [26]:
d6 = d5
d5['wang']=10000
d6
Out[26]:
{'wang': 10000, 'niu': 4000, 'liu': 7000, 'li': 5000, 'yang': 6000}
In [27]:
d5 = {}
d6
Out[27]:
{'wang': 10000, 'niu': 4000, 'liu': 7000, 'li': 5000, 'yang': 6000}
In [29]:
d6 = d5
d5.clear()
d6
Out[29]:
{}
In [30]:
import json
js1 = json.dumps(d4)
js1
Out[30]:
'{"wang": 4000, "li": 5000, "yang": 6000}'
In [31]:
js2 = json.loads(js1)
js2
Out[31]:
{'wang': 4000, 'li': 5000, 'yang': 6000}
In [34]:
import requests
kw = {'q':'Python dict'}
# http://cn.bing.com/search
# http://cn.bing.com/search/?q=%us
r = requests.get('http://cn.bing.com/search',params = kw)
r.url
#print(r.text)
Out[34]:
'http://cn.bing.com/search?q=Python+dict'
In [35]:
def dct(args1,*argst,**argsd):
    print(args1)
    print(argst)
    print(argsd)
    
dct('hello','wang','niu','liu','li','yang',a1=1,a2=2,a3=3,a4=5)
hello
('wang', 'niu', 'liu', 'li', 'yang')
{'a1': 1, 'a2': 2, 'a3': 3, 'a4': 5}
In [38]:
class Test:
    def kws(self,a,b,*non_keyword,**keyword):
        print('a is ',a)
        print('b is ',b)
        print('non_keyword',non_keyword)
        for key in keyword:
            print("'%s':%s" % (key,str(keyword[key])))
test = Test()
#dis = test.kws('a','b',('c','d'),e='e',f='f')
dis = test.kws('a','b','c','d',e='e',f='f')
a is  a
b is  b
non_keyword ('c', 'd')
'e':e
'f':f
  • set1.issubset.(set2)
  • set1.difference(set2)
  • set1.intersection(set2)
  • set3 = set1.copy()
  • set1.add('a')
  • set1.remove('a')
  • set1.update('abc')
  • set1.clear()
In [3]:
dd = {'wang': 4000, 'niu': 4000, 'liu': 7000, 'li': 5000, 'yang': 6000}
set1 =  []
for key in dd.keys():
    set1.append(key)

set1.append('wang')
set(set1)
Out[3]:
{'li', 'liu', 'niu', 'wang', 'yang'}
In [6]:
set2 = set('SUFE')
set3 = frozenset('SUFE')
type(set3)
Out[6]:
frozenset
In [7]:
'A' in set2
Out[7]:
False
In [8]:
set2 == set3 
Out[8]:
True
In [9]:
set2 < set3
Out[9]:
False
In [10]:
set('SU') < set2
Out[10]:
True
In [11]:
set4 = set('ECNU') 
set4 ^ set2
Out[11]:
{'C', 'F', 'N', 'S'}
In [12]:
set4 & set2
set4 | set2
Out[12]:
{'C', 'E', 'F', 'N', 'S', 'U'}
In [14]:
set5 = set('ECNU')
set5 -= set2
set5
Out[14]:
{'C', 'N'}
In [ ]: