python dictionary
2017-11-06 15:30 woodzcl 阅读(319) 评论(0) 编辑 收藏 举报//test.py
dict = {1:1, 2:2, 3:3}
print dict
del dict[2]
print dict
dict.clear()
print dict
del dict
print dict
dict1 = {1:1, 1:2}
print dict1
dict3 = {(1, 2, 3): 4}
print dict3[(1, 2,3)]
print str(dict3)
print type(dict3)
dict3.clear()
print dict3
dict3['school'] = 'brand'
print dict3.copy()
ch = (1, 2, 3)
chdict = {}
print 'fromkeys',chdict.fromkeys(ch, 1)
chdict = chdict.fromkeys(ch, 1)
print chdict.get(1)
print chdict.get(2)
print chdict.get(3)
print chdict.get(4)
print chdict.has_key(3)
print 'dict3.has_key',dict3.has_key((1, 2, 3))
print dict3.has_key('school')
print dict1.has_key(1)
print chdict
print chdict.items()
print chdict.keys()
chdict.setdefault(1, 100)
print chdict
chdict.setdefault(4, 2000)
print chdict
chdict.setdefault(5)
print chdict
di1 = {1:1,2:2}
di2 = {3:3,4:4}
di1.update(di2)
print di1
print chdict.values()
print chdict.pop(5)
print chdict
print chdict.popitem()
print chdict
//result
# python test.py
{1: 1, 2: 2, 3: 3}
{1: 1, 3: 3}
{}
<type 'dict'>
{1: 2}
4
{(1, 2, 3): 4}
<type 'dict'>
{}
{'school': 'brand'}
fromkeys {1: 1, 2: 1, 3: 1}
1
1
1
None
True
dict3.has_key False
True
True
{1: 1, 2: 1, 3: 1}
[(1, 1), (2, 1), (3, 1)]
[1, 2, 3]
{1: 1, 2: 1, 3: 1}
{1: 1, 2: 1, 3: 1, 4: 2000}
{1: 1, 2: 1, 3: 1, 4: 2000, 5: None}
{1: 1, 2: 2, 3: 3, 4: 4}
[1, 1, 1, 2000, None]
None
{1: 1, 2: 1, 3: 1, 4: 2000}
(1, 1)
{2: 1, 3: 1, 4: 2000}