|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
如果您觉得本篇CentOSLinux教程讲得好,请记得点击右边漂浮的分享程序,把好文章分享给你的小伙伴们!基础语法:
dict={ob1:computer,ob2:mouse,ob3:printer}
技能:
字典中包括列表:dict={yangrong:[23,IT],"xiaohei":[22,dota]}
字典中包括字典:dict={yangrong:{"age":"23","job":"IT"},"xiaohei":{"age:22,job:dota"}}
可用下令:
root@yangrong:~#cd/python
root@yangrong:/python#python
Python2.7.5+(default,Sep192013,13:48:49)
[GCC4.8.1]onlinux2
Type"help","copyright","credits"or"license"formoreinformation.
>>>importtab
>>>d={}
>>>d.
d.__class__(d.__ge__(d.__len__(d.__setitem__(d.has_key(d.setdefault(
d.__cmp__(d.__getattribute__(d.__lt__(d.__sizeof__(d.items(d.update(
d.__contains__(d.__getitem__(d.__ne__(d.__str__(d.iteritems(d.values(
d.__delattr__(d.__gt__(d.__new__(d.__subclasshook__(d.iterkeys(d.viewitems(
d.__delitem__(d.__hash__d.__reduce__(d.clear(d.itervalues(d.viewkeys(
d.__doc__d.__init__(d.__reduce_ex__(d.copy(d.keys(d.viewvalues(
d.__eq__(d.__iter__(d.__repr__(d.fromkeys(d.pop(
d.__format__(d.__le__(d.__setattr__(d.get(d.popitem(
>>>d.
增添字典元素
>>>nameinfo={}
>>>nameinfo[a1]=yangrong#若字典里有a1主键,则掩盖本来的值,没有,则增加
>>>nameinfo
{a1:yangrong}
遍历字典主键与键值
>>>fork,valueinnameinfo.items():
...printk,value
...
a1yangrong
检察字典一切主键
>>>dict={ob1:computer,ob2:mouse,ob3:printer}
>>>
>>>
>>>dict.keys()
[ob2,ob3,ob1]
判别字典中是不是有该主键
>>>dict.keys()
[ob2,ob3,ob1]
>>>dict.has_key(ob2)#或ob2indict
True
>>>dict.has_key(ob4)
False
也有人用轮回***来判别
forkeyindict.keys():
可是这类***究竟不敷简便,
检察字典一切键值内容
>>>dict={ob1:computer,ob2:mouse,ob3:printer}
>>>dict.values()
[mouse,printer,computer]
列出一切项目
>>>dict.items()
[(ob2,mouse),(ob3,printer),(ob1,computer)]
清空字典
>>>dict.clear()
>>>dict
{}
拷贝字典
>>>dict
{ob2:mouse,ob3:printer,ob1:computer}
>>>a=dict
>>>a
{ob2:mouse,ob3:printer,ob1:computer}
>>>b=dict.copy()
>>>b
{ob2:mouse,ob3:printer,ob1:computer}
对照字典
>>>cmp(a,b)
起首对照主键长度,然后对照键巨细,然后对照键值巨细,(第一个年夜前往1,小前往-1,一样前往0)
更新字典
>>>dict={yangrong:{"age":"23","job":"IT"},"xiaohei":{"age:22,job:dota"}}
>>>dict
{xiaohei:set(["age:22,job:dota"]),yangrong:{age:23,job:IT}}
>>>dict[xiaohei]=111#修正一级字典
>>>dict
{xiaohei:111,yangrong:{age:23,job:IT}}
>>>dict[yangrong][age]=25#修正二级字典
>>>dict
{xiaohei:111,yangrong:{age:25,job:IT}}
>>>dict={yangrong:[23,IT],"xiaohei":[22,dota]}
>>>dict[xiaohei][1]="dota2"#修正字典中列表某项,1是代表列表中第2个字符串。
>>>dict
{xiaohei:[22,dota2],yangrong:[23,IT]}
删除字典元素
>>>dict
{xiaohei:[22,dota2],yangrong:[23,IT]}
>>>deldict[xiaohei]#删除xiaohei键值
>>>dict
{yangrong:[23,IT]}
>>>dict
{yangrong:[23,IT]}
>>>
>>>deldict[yangrong][1]#删除yangrong主键的每2字值
>>>dict
{yangrong:[23]}
删除全部字典
>>>dict
{yangrong:[23]}
>>>dict.clear()#同deldict
>>>dict
{}
将字符串切分为列表
>>>s="helloworldbye"
>>>s.split()#用于把有纪律的文本,读掏出来后,利用列表举行修正,再写进文件。
[hello,world,bye]
将列表转换为字符串
S.split(str,)#将string转list,以空格切分
存储字典(pickle序列化)
#需导进pickle模块importpickle
把字典内容存进文件
f=file(data.txt,wb)#新建文件data.txt,wb,b是翻开块文件,关于装备文件有效
pickle.dump(a,f)#把a序列化存进文件
f.close()
把内容读进字典(反序列化)
a=open(data.txt,rb)#翻开文件
printpickle.load(a)#把内容全体反序列化
本文出自“此心安处是吾乡”博客,请务必保存此出处http://yangrong.blog.51cto.com/6945369/1364007
如果您觉得本篇CentOSLinux教程讲得好,请记得点击右边漂浮的分享程序,把好文章分享给你的小伙伴们! |
|