'''
【课程4.1】 字典dict基本概念
字典是另一种可变容器模型,且可存储任意类型(字典嵌套,列表等都可以,万物皆对象)对象。
字典的每个键值(key=>value)对用冒号(:)分割,每个对之间用逗号(,)分割,整个字典包括在花括号({})中
键必须是唯一的,但值则不必。
(dict无序,故键是dict的index,必须唯一)
list/tuple/string有序,序号是dict的index,每个元素有唯一的序号
'''
'\n【课程4.1】 字典dict基本概念\n\n字典是另一种可变容器模型,且可存储任意类型(字典嵌套,列表等都可以,万物皆对象)对象。\n\n字典的每个键值(key=>value)对用冒号(:)分割,每个对之间用逗号(,)分割,整个字典包括在花括号({})中 \n\n键必须是唯一的,但值则不必。\n(dict无序,故键是dict的index,必须唯一)\n\nlist/tuple/string有序,序号是dict的index,每个元素有唯一的序号\n'
name = [ '大头儿子' , '小头爸爸' , '隔壁老王' ]
age = [ 6 , 35 , 37 ]
dic= { '大头儿子' : 6 , "小头爸爸" : 35 , "tom" : 45 }
print ( dic, type ( dic) )
lstofdic= [ { 'name' : 'Tom' , 'age' : 18 } , { 'name' : 'Jack' , 'age' : 19 } , { 'name' : 'Alex' , "age" : 17 } ]
print ( lstofdic)
{'大头儿子': 6, '小头爸爸': 35, 'tom': 45} <class 'dict'>
[{'name': 'Tom', 'age': 18}, {'name': 'Jack', 'age': 19}, {'name': 'Alex', 'age': 17}]
dic = { 'a' : 1 , 'b' : 2 , 'c' : 3 }
dic = { 'a' : 1 , 'a' : 2 }
print ( dic)
dic = { 'var1' : 1 , 'var2' : 'hello' , 'var3' : [ 1 , 2 , 3 ] , 'var4' : { 'a' : 1 , 'b' : 2 } }
dic[ 'var5' ] = 'addone'
dic[ 'var4' ] = 'changeone'
del dic[ 'var1' ]
print ( dic)
dic. clear( )
print ( dic)
del dic
print ( dic)
{'a': 2}
{'var2': 'hello', 'var3': [1, 2, 3], 'var4': 'changeone', 'var5': 'addone'}
{}
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
<ipython-input-22-440bae37fbee> in <module>
24 #完全删除dic ,dic对象完全消失
25 del dic
---> 26 print(dic)
NameError: name 'dic' is not defined
'''
注类似list() tuple()函数结合range或结合彼此的创建法
tup=tuple(range(1,5,2))
lst=list(range(1,5,2))
lst1=list(tup)
tup1=tuple(lst)
'''
'\n注类似list() tuple()函数结合range或结合彼此的创建法\ntup=tuple(range(1,5,2))\nlst=list(range(1,5,2))\nlst1=list(tup)\ntup1=tuple(lst)\n'
dic= { 'a' : 1 , 'b' : 2 }
lst1= [ ( 'a' , 1 ) , ( 'b' , 2 ) ]
lst2= [ [ 'a' , 'b' ] , [ 'c' , 'd' ] ]
lst3= [ [ 1 , [ 2 , 3 ] ] , [ 2 , [ 3 , 2 ] ] ]
print ( lst1, lst2, lst3)
dic1= dict ( lst1)
dic2= dict ( lst2)
dic3= dict ( lst3)
print ( dic1, dic2, dic3)
keys= [ 'a' , 'b' , 'c' ]
dic1= dict . fromkeys( keys)
dic2= dic. fromkeys( keys, "初始值" )
print ( dic1, dic2)
[('a', 1), ('b', 2)] [['a', 'b'], ['c', 'd']] [[1, [2, 3]], [2, [3, 2]]]
{'a': 1, 'b': 2} {'a': 'b', 'c': 'd'} {1: [2, 3], 2: [3, 2]}
{'a': None, 'b': None, 'c': None} {'a': '初始值', 'b': '初始值', 'c': '初始值'}
'''
小作业
① 用dict函数的时候,和直接申明一个字典,操作上什么区别?
dic = {'a':1,'b':2}
dic = dict(m = 10 ,n = 'aa', h = [1,2,3])
直接申明用key:value连接键值对 用dict()函数用key=vlaue连接键值对
② 用dict()函数从序列创建字典的时候,对序列有要求吗?必须是列表吗?
list或tuple都可以
③ 字典是无序的,强化一下
④ 以dict()函数生成一个人的成绩单,包括语文,数学,外语,物理,化学几个元素
lstofcoup=[['语文',100],['数学',100],['外语',100]]
dic=dict(lstofcoup)
print(dic)
'''
"\n小作业\n① 用dict函数的时候,和直接申明一个字典,操作上什么区别?\ndic = {'a':1,'b':2}\ndic = dict(m = 10 ,n = 'aa', h = [1,2,3])\n直接申明用key:value连接键值对 用dict()函数用key=vlaue连接键值对\n\n② 用dict()函数从序列创建字典的时候,对序列有要求吗?必须是列表吗?\nlist或tuple都可以\n\n③ 字典是无序的,强化一下\n\n④ 以dict()函数生成一个人的成绩单,包括语文,数学,外语,物理,化学几个元素\nlstofcoup=[['语文',100],['数学',100],['外语',100]\ndic=dic(lstofcoup)\nprint(dic)\n"
'''
【课程4.2】 字典常用操作
字典内置函数&方法
'''
'\n【课程4.2】 字典常用操作\n\n字典内置函数&方法\n\n'
dic1= { 'a' : 1 , 'b' : 2 }
dic2= { 'c' : 3 , 'd' : 4 }
dic1. update( dic2)
print ( dic1, '\n' , dic2)
a= { 'm' : 1 , 'n' : 2 , 's' : 3 }
b= a
a. update( { 666 : 222 , 666 : 555 } )
print ( a, '\n' , b)
a= { 'm' : 1 , 'n' : 2 , 's' : 3 }
b= a. copy( )
a. update( { 666 : 222 , 666 : 555 } )
print ( a, '\n' , b)
print ( len ( a) )
print ( 'm' in a)
print ( 1 not in a)
{'a': 1, 'b': 2, 'c': 3, 'd': 4}
{'c': 3, 'd': 4}
{'m': 1, 'n': 2, 's': 3, 666: 555}
{'m': 1, 'n': 2, 's': 3, 666: 555}
{'m': 1, 'n': 2, 's': 3, 666: 555}
{'m': 1, 'n': 2, 's': 3}
4
True
True
'''
【课程4.3】 字典的元素访问及遍历
如何读取字典?
'''
'\n【课程4.3】 字典的元素访问及遍历\n\n如何读取字典?\n\n'
dic= { 'a' : 1 , 'b' : 2 , 'c' : 3 }
print ( dic[ 'a' ] )
poi= { 'name' : 'shop' , 'city' : 'shanghai' , 'information' : { 'address' : 'somewhere' , 'num' : 66663333 } }
print ( poi[ 'information' ] [ 'address' ] )
print ( poi. get( 'name' ) )
print ( poi. get( 'xxx' , print ( 'nothing' ) ) )
print ( poi. values( ) , type ( poi. values( ) ) )
print ( list ( poi. values( ) ) )
print ( poi. keys( ) , type ( poi. keys( ) ) )
print ( list ( poi. keys( ) ) )
print ( poi. items( ) , type ( poi. values( ) ) )
print ( list ( poi. items( ) ) )
1
somewhere
shop
nothing
None
dict_values(['shop', 'shanghai', {'address': 'somewhere', 'num': 66663333}]) <class 'dict_values'>
['shop', 'shanghai', {'address': 'somewhere', 'num': 66663333}]
True
dict_keys(['name', 'city', 'information']) <class 'dict_keys'>
['name', 'city', 'information']
dict_items([('name', 'shop'), ('city', 'shanghai'), ('information', {'address': 'somewhere', 'num': 66663333})]) <class 'dict_values'>
[('name', 'shop'), ('city', 'shanghai'), ('information', {'address': 'somewhere', 'num': 66663333})]
poi = { 'name' : 'shop' , 'city' : 'shanghai' , 'information' : { 'address' : 'somewhere' , 'num' : 66663333 } }
for key in poi. keys( ) :
print ( key)
for value in poi. values( ) :
print ( value)
for ( k, v) in poi. items( ) :
print ( 'key=%s value=%s' % ( k, v) )
name
city
information
shop
shanghai
{'address': 'somewhere', 'num': 66663333}
key=name value=shop
key=city value=shanghai
key=information value={'address': 'somewhere', 'num': 66663333}
data= [ { 'name' : 'jam' , 'age' : 18 } , { 'name' : 'tim' , 'age' : 12 } , { 'name' : 'jason' , 'age' : 58 } ]
for i in data:
print ( i[ 'name' ] )
print ( i. get( 'name' ) )
jam
jam
tim
tim
jason
jason
"""
小作业
① dict.keys()生成的是不是列表?
是视图,通过list(dict.keys())转换为列表
② 如何判断一个value是否存在于字典中?
print('shop' in poi.values())
"""