标签:pre tde 判断 字典元素 含义 seq 结合 用法 间接
作为Python的初学者,难免会为了一两个小问题找资料,然而一找资料,发现Python关于字典这方面的解析很少,我在此稍稍解析一下字典的用法和含义,若有不足,望大家批评。(在这我直接以代码形式为大家解析)
先来讲讲我对字典的理解吧
简单粗暴,字典就是键值对,而且是无序的,可控的键值对,并且python中字典可以嵌套字典
a={‘name’:’Tom’,’age’:20,’job’:’Student’} print a >>> {‘name’:’Tom’,’age’:20,’job’:’Student’}
b={} b[‘name’]=’Tom’ b[‘age’]=20 b[‘job’]=’Student’ print b >>> {‘name’:’Tom’,’age’:20,’job’:’Student’}
b={} b[‘name’]=’Tom’ b[‘age’]=20 b[‘job’]=’Student’ print b print ‘添加后’.center(50,’-’) b[‘friend’]=’Sam’ >>> {‘name’:’Tom’,’age’:20,’job’:’Student’} ------------------------添加后------------------------ {‘friend’:’Sam’,‘name’:’Tom’,’age’:20,’job’:’Student’}
c=dict(name=’Tom’,age=20,job=’Student’) print c >>> {‘name’:’Tom’,’age’:20,’job’:’Student’}
c=[(‘name’,’Tom’),(‘age’,20),(‘job’,’Student’)] d=dict(c) print d >>> {‘name’:’Tom’,’age’:20,’job’:’Student’}
d=dict() print d >>> {}
创建一个薪字典,参数seq中的元素作为key,参数x作为values,若无参数x则默认为None
* seq为元组
ss=(‘name’,’age’,’job’) f=d.fromkeys(ss,’Tom’) g=d.fromkeys(ss) print f print ‘无参数x,values默认为None’.center(50,’*’) print g >>> {‘name’:’Tom’,’age’:’Tom’,’job’:’Tom’} ***************无参数x,values默认为None*************** {‘name’:None,’age’:None,’job’:None}
h=dict(zip(‘abc’,[1,2,3])) print h >>> {‘a’:1,’c’:3,’’b’:2}
I={a:2*a for a in range(3)} Print i >>> {0:0,1:2,2:4}
i={k:v for (k,v) in zip([‘a’,’b’],[1,2])} Print i >>> {‘a’:1,‘b’:2}
list=[‘x’,1,’y’,2,’z’,3] dic=dict(zip(list[::2],list[1::2])) print dic {‘y’:2,’z’:3,’x’:1}
关于字典的创建方法就写这么多了,还有些比较奇葩的,基本不会用到的,我就不一一列举出来了,以免颠覆各位三观。。
a={‘name’:’Tom’,’age’:20,’job’:’Student’} print a print ‘添加新元素friend’.center(20,’-’) a[‘friend’]=’Sam’ print a >>> {‘name’:’Tom’,’age’:20,’job’:’Student’} -----------添加新元素friend---------- {‘name’:’Tom’,’age’:20,’job’:’Student’,’friend’:’Sam’}
a={‘name’:’Tom’,’age’:20,’job’:’Student’} print a print ‘添加新元素friend’.center(20,’-’) a.setdefault(‘friend’,’Sam’) #若value为空,则默认为None print a >>> {‘name’:’Tom’,’age’:20,’job’:’Student’} -----------添加新元素friend---------- {‘name’:’Tom’,’age’:20,’job’:’Student’,’friend’:’Sam’}
a={‘name’:’Tom’,’age’:20,’job’:’Student’} print a print ‘添加新元素friend’.center(20,’-’) b={‘friend’:’Sam’,’age’:21} a.update(b) print a >>> {‘name’:’Tom’,’age’:20,’job’:’Student’} -----------添加新元素friend---------- {‘name’:’Tom’,’age’:21,’job’:’Student’,’friend’:’Sam’}
a={‘name’:’Tom’,’age’:20,’job’:’Student’} print a print ‘添加新元素friend’.center(20,’-’) a.update(b=2) print a {‘name’:’Tom’,’b’:2,’age’:21,’job’:’Student’,’friend’:’Sam’}
a={‘name’:’Tom’,’age’:20,’job’:’Student’} print a print ‘ ’.center(50,’-’) del a[‘job’] print a >>> {‘name’:’Tom’,’age’:20,’job’:’Student’} ------------------------------------------ -------------------------------------------- {‘name’:’Tom’,’age’:20 }
a={‘name’:’Tom’,’age’:20,’job’:’Student’} print a print ‘ ’.center(50,’-’) a.pop(‘job’) print a >>> {‘name’:’Tom’,’age’:20,’job’:’Student’} ------------------------------------------ -------------------------------------------- {‘name’:’Tom’,’age’:20 }
a={‘name’:’Tom’,’age’:20,’job’:’Student’} print a print ‘ ’.center(50,’-’) a.popitem() #随机删,数据少时不易发现 print a >>> {‘name’:’Tom’,’age’:20,’job’:’Student’} ------------------------------------------ -------------------------------------------- {‘name’:’Tom’,’age’:20 }
a={‘name’:’Tom’,’age’:20,’job’:’Student’} print a print ‘ ’.center(50,’-’) a.clear() print a >>> {‘name’:’Tom’,’age’:20,’job’:’Student’} ------------------------------------------ -------------------------------------------- {}
a={‘name’:’Tom’,’age’:20,’job’:’Student’} print a print ‘ ’.center(50,’-’) a[‘name’]=’superme’ print a >>> {‘name’:’Tom’,’age’:20,’job’:’Student’} ------------------------------------------ -------------------------------------------- {‘name’:’superme,’age’:20,’job’:’Student’}
a={‘name’:’Tom’,’age’:20,’job’:’Student’} print a print ‘ ’.center(50,’-’) a.update({‘name’:’superme’}) print a >>> {‘name’:’Tom’,’age’:20,’job’:’Student’} ------------------------------------------ -------------------------------------------- {‘name’:’superme’,’age’:20,’job’:’Student’}
a={‘name’:’Tom’,’age’:20,’job’:’Student’} print a print ‘ ’.center(50,’-’) a[‘friend’]=a.pop[‘name’] print a >>> {‘name’:’Tom’,’age’:20,’job’:’Student’} ------------------------------------------ -------------------------------------------- {‘friend’:’Tom’,’age’:20,’job’:’Student’}
a={‘name’:’Tom’,’age’:20} print a print ‘ ’.center(50,’-’) print a[‘name’] >>> {‘name’:’Tom’,’age’:20} ------------------------------------------ -------------------------------------------- Tom
a={‘name’:’Tom’,’age’:20} print a print ‘ ’.center(50,’-’) b=a.get(‘name’) c=a.get(‘superme’) print b print c >>> {‘name’:’Tom’,’age’:20} ------------------------------------------ -------------------------------------------- Tom None
a={‘name’:’Tom’,’age’:20} print a print ‘ ’.center(50,’-’) print a.keys() print a.values() >>> {‘name’:’Tom’,’age’:20} ------------------------------------------ -------------------------------------------- Name,age Tom,20
a={‘name’:’Tom’,’age’:20} print a print ‘ ’.center(50,’-’) print ‘name’ in a >>> {‘name’:’Tom’,’age’:20} ------------------------------------------ -------------------------------------------- True
至于遍历字典什么的,就是循环,循环大致都相通,在这就不说了,希望我写的这五点,能对各位读者有帮助,谢谢观看。
标签:pre tde 判断 字典元素 含义 seq 结合 用法 间接
原文地址:https://www.cnblogs.com/FCTEY/p/9680227.html