python基础——字典

1、创建一个字典

dic={1:'alex','age':35,'hobby':{'girl_name':'铁锤','age':45},'is_handsome':True}
dic={'age':'alex','age':35,'hobby':{'girl_name':'铁锤','age':45},'is_handsome':True}
创建字典(字典两大特点:无序,键唯一)

2、查看字典
 

dic = {'name':'陈璋','age':'27'}
dic1 = dic['age']     # 通过键去查看值
print(dic1)  

结果:
27


# 查看keys、values、items方法
dic5={'age': 18, 'name': 'alex', 'hobby': 'girl'}   # 查看指定键的值
print(dic5['hobby'])
print(dic5)

dic6 = {'name':'陈璋','age':'23'}

print(list(dic5.keys()))       # 使用列表的格式,查看字典的键,
print(list(dic5.values()))      # 使用列表的格式,查看字典的值
print(list(dic5.items()))      # 使用列表的格式,查看字典的键值对

结果:
girl
{'age': 18, 'name': 'alex', 'hobby': 'girl'}
['age', 'name', 'hobby']
[18, 'alex', 'girl']
[('age', 18), ('name', 'alex'), ('hobby', 'girl')]

3、通过关键字,创建字典

#方法1
dic2=dict((('name','alex'),))    # 使用dict关键字创建字典
print(dic2)

#方法2
dic3=dict([['name','alex'],])
print(dic3)

结果:
{'name': 'alex'}        #两个结果都一样

4、对字典进行操作(方法)

新增键值对
 

#增加键值对
dic4 = {'name':'小程'}
dic4['age'] = 18    # 增加键值对
print(dic4)

# 如果键存在,不会修改值 setdefault方法
ret=dic4.setdefault('age',32)   # 键存在,不修改键值对,并返回原有值
print(ret)

#如果键不存在,增加键值对,setdefault方法
ret=dic4.setdefault('shengao',32)   # 键不存在,新增的键值对,并返回相应的值
print(dic4)

结果:
{'name': '小程', 'age': 18}
18
{'name': '小程', 'age': 18, 'shengao': 32}

修改(fromkeys)

dic7 = {'name':'陈璋','age':'23','hobby':'girl'}
dic7['age'] = 27                # 通过键去修改值
print(dic7)

结果:
{'name': '陈璋', 'age': 27, 'hobby': 'girl'}



dic13=dict.fromkeys(['host1','host2','host3'],'test')       # 使用formkeys方法,给每一键加上(test)值
print(dic13)#{'host3': 'test', 'host1': 'test', 'host2': 'test'}

结果:
{'host1': 'test', 'host2': 'test', 'host3': 'test'}



dic13 = {'host1': 'test', 'host2': 'test', 'host3': 'test'}
dic13['host2']='abc'            # 修改字典中的值
print(dic13)

结果:
{'host1': 'test', 'host2': 'abc', 'host3': 'test'}



dic13=dict.fromkeys(['host1','host2','host3'],['test1','tets2'])        # 使用formkeys方法,给每一键加上(列表)值
print(dic13)    # {'host2': ['test1', 'tets2'], 'host3': ['test1', 'tets2'], 'host1': ['test1', 'tets2']}

结果:
{'host1': ['test1', 'tets2'], 'host2': ['test1', 'tets2'], 'host3': ['test1', 'tets2']}



dic13['host2'][1]='test3'         # 修改字典中host2中test2的值
print(dic13)        

结果:
{'host1': ['test1', 'test3'], 'host2': ['test1', 'test3'], 'host3': ['test1', 'test3']}
# 修改一个键值对,就修改了所有键值对

更新(update)

dic8={'age': 18, 'name': 'alex', 'hobby': 'girl'}
dic9={'1':'111','name':'222'}
dic8.update(dic9)   # 将dic9的字典更新到dic8字典中去
print(dic8)
print(dic9)

结果:
{'age': 18, 'name': '222', 'hobby': 'girl', '1': '111'}
{'1': '111', 'name': '222'}            # dic9字典键值对不变

删除(clear、pop、popitem、del)

dic10 = {'name': 'alex', 'age': 18, 'class': 1}
dic10.clear()   # 清空字典
print(dic10)

结果如下:
{}


dic12 = {'name': 'alex', 'age': 18, 'class': 1}
ret = dic12.pop('age')    # 删除字典中指定键值对,并返回该键值对的值
print(ret)
print(dic12)

结果如下:
18
{'name': 'alex', 'class': 1}


dic12 = {'name': 'alex', 'age': 18, 'class': 1}
a = dic12.popitem()     # 随机删除某组键值对,并以元组方式返回值
print(a)
print(dic12)

结果如下:
('class', 1)
{'name': 'alex', 'age': 18}


dic12 = {'name': 'alex', 'age': 18, 'class': 1}
del dic12       # 删除整个字典

通过多层嵌套大字典查询

 # 种类大字典
 class_dict = {"蔬菜":{"苦瓜":["很苦","有降火功效"],
                     "南瓜":["比较甜","个头也比较大"],},
               "水果":{"西瓜":["很甜","形状是圆的"],
                     "橘子":["清甜可口","包含多种维生素"]},
               "电器":{"空调":["凉爽","耗电"],
                     "洗衣机":["快速洗衣","节省时间"],
                     "微波炉":["快速加热","方便快捷"]},
               "交通":{"自行车":["上坡费劲","适合短程"],
                     "摩托车":["速度快","不安全"],
                     "汽车":["适应多种天气","价格昂贵"]}
               }
 aa = class_dict["交通"]["摩托车"][1]  #能过键去找值,最后通过索引号找到对应的信息
 print(aa)

结果:
不安全

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值