码迷,mamicode.com
首页 > 编程语言 > 详细

python-字典操作

时间:2019-03-27 00:08:55      阅读:214      评论:0      收藏:0      [点我收藏+]

标签:mos   ESS   user   返回   生成   sdi   rom   总数   module   

字典的特性:

  • dict是无序的
  • key必须是唯一的,so 天生去重

字典内置方法:

radiansdict.clear() #删除字典内所有元素
radiansdict.copy() #返回一个字典的浅复制
radiansdict.fromkeys() #创建一个新字典,以序列seq中元素做字典的键,val为字典所有键对应的初始值
radiansdict.get(key, default=None) #返回指定键的值,如果值不在字典中返回default值
radiansdict.has_key(key) #如果键在字典dict里返回true,否则返回false
radiansdict.items() #以列表返回可遍历的(键, 值) 元组数组
radiansdict.keys() #以列表返回一个字典所有的键
radiansdict.setdefault(key, default=None) #和get()类似, 但如果键不已经存在于字典中,将会添加键并将值设为default
radiansdict.update(dict2) #把字典dict2的键/值对更新到dict里
radiansdict.values() #以列表返回字典中的所有值

举例介绍:

>>> info
{stu1102: LongZe Luola, stu1103: XiaoZe Maliya, stu1101: 武藤兰}
>>> info.pop("stu1101") #标准删除姿势
武藤兰
>>> info
{stu1102: LongZe Luola, stu1103: XiaoZe Maliya}
>>> del info[stu1103] #换个姿势删除
>>> info
{stu1102: LongZe Luola}
>>> 
>>> 
>>> 
>>> info = {stu1102: LongZe Luola, stu1103: XiaoZe Maliya}
>>> info
{stu1102: LongZe Luola, stu1103: XiaoZe Maliya} #随机删除
>>> info.popitem()
(stu1102, LongZe Luola)
>>> info
{stu1103: XiaoZe Maliya}
>>> info = {stu1102: LongZe Luola, stu1103: XiaoZe Maliya}
>>> 
>>> "stu1102" in info #标准用法
True
>>> info.get("stu1102")  #获取
LongZe Luola
>>> info["stu1102"] #同上,但是看下面
LongZe Luola
>>> info["stu1105"]  #如果一个key不存在,就报错,get不会,不存在只返回None
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
KeyError: stu1105
#values
>>> info.values()
dict_values([LongZe Luola, XiaoZe Maliya])

#keys
>>> info.keys()
dict_keys([stu1102, stu1103])


#setdefault
>>> info.setdefault("stu1106","Alex")
Alex
>>> info
{stu1102: LongZe Luola, stu1103: XiaoZe Maliya, stu1106: Alex}
>>> info.setdefault("stu1102","龙泽萝拉")
LongZe Luola
>>> info
{stu1102: LongZe Luola, stu1103: XiaoZe Maliya, stu1106: Alex}


#update 
>>> info
{stu1102: LongZe Luola, stu1103: XiaoZe Maliya, stu1106: Alex}
>>> b = {1:2,3:4, "stu1102":"龙泽萝拉"}
>>> info.update(b)
>>> info
{stu1102: 龙泽萝拉, 1: 2, 3: 4, stu1103: XiaoZe Maliya, stu1106: Alex}

#items
info.items()
dict_items([(stu1102, 龙泽萝拉), (1, 2), (3, 4), (stu1103, XiaoZe Maliya), (stu1106, Alex)])


#通过一个列表生成默认dict,有个没办法解释的坑,少用吧这个
>>> dict.fromkeys([1,2,3],testd)
{1: testd, 2: testd, 3: testd}

 

字典内置函数:

cmp(dict1, dict2)  #比较两个字典元素。
len(dict)              #计算字典元素个数,即键的总数。
str(dict)              #输出字典可打印的字符串表示。
type(variable)     #返回输入的变量类型,如果变量是字典就返回字典类型。        

 

字典练习代码

print(‘‘‘|---欢迎进入通讯录程序---|
|---1、 查询联系人资料---|
|---2、 插入新的联系人---|
|---3、 删除已有联系人---|
|---4、 退出通讯录程序---|‘‘‘)
addressBook={}#定义通讯录
while 1:
    temp=input(请输入指令代码:)
    if not temp.isdigit():
        print("输入的指令错误,请按照提示输入")
        continue
    item=int(temp)#转换为数字
    if item==4:
        print("|---感谢使用通讯录程序---|")
        break
    name = input("请输入联系人姓名:")
    if item==1:
        if name in addressBook:
            print(name,:,addressBook[name])
            continue
        else:
            print("该联系人不存在!")
    if item==2:
        if name in addressBook:
            print("您输入的姓名在通讯录中已存在-->>",name,":",addressBook[name])
            isEdit=input("是否修改联系人资料(Y/N):")
            if isEdit==Y:
                userphone = input("请输入联系人电话:")
                addressBook[name]=userphone
                print("联系人修改成功")
                continue
            else:
                continue
        else:
            userphone=input("请输入联系人电话:")
            addressBook[name]=userphone
            print("联系人加入成功!")
            continue

    if item==3:
        if name in addressBook:
            del addressBook[name]
            print("删除成功!")
            continue
        else:
            print("联系人不存在")

 

python-字典操作

标签:mos   ESS   user   返回   生成   sdi   rom   总数   module   

原文地址:https://www.cnblogs.com/fsllovexzd/p/10604656.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!