In [ ]:
a = dict(one=1, two=2, three=3)
b = {'one': 1, 'two': 2, 'three': 3}
c = dict(zip(['one', 'two', 'three'], [1, 2, 3]))
d = dict([('two', 2), ('one', 1), ('three', 3)])
e = dict({'three': 3, 'one': 1, 'two': 2})
In [ ]:
a == b == c == d == e
In [ ]:
type(a)
In [ ]:
print 'one' in a
print 1 not in a
In [ ]:
len(a)
In [ ]:
a['two']
In [ ]:
a['two'] = 4
a
In [ ]:
del a['two']
a
In [ ]:
for i in a:
print "key :", i, "value :", a[i]
In [ ]:
a.clear()
a
In [ ]:
a = b
a
In [ ]:
print id(a)
print id(b)
id(a)
In [ ]:
a['three'] = 6
print a
print b
In [ ]:
# KEY = 键
myobjs = ('box1', 'box2', 'box3', 'box4', 'box5', 'box6')
aDict = {}.fromkeys(myobjs, 6)
aDict
In [ ]:
#aDict['box0', 0]
aDict.get('box0', 0)
In [ ]:
aDict.items()
In [ ]:
aDict.iteritems()
In [ ]:
aDict.pop('box0', 0)
In [ ]:
print aDict
aDict.popitem()
aDict
In [ ]:
print aDict.setdefault('box1')
print aDict.setdefault('box0', 0)
aDict
In [ ]:
aDict.update(box1=12, box7=10)
aDict
In [ ]:
print aDict.keys()
print aDict.values()
In [ ]:
def getDet():
return [3, 3, 1]
x = 1
bDict = {}
for i in ['box4', 'box7', 'box6', 'box1', 'box0', 'box3', 'box2']:
objDet = [3 + x, 3 + x, 1 + x]
print objDet
bDict[i] = objDet
x += 1
bDict
In [ ]:
# --------------------------------------------------------------------------------
# Copyright (c) 2013 Mack Stone. All rights reserved.
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
# --------------------------------------------------------------------------------