当前位置:   article > 正文

Python基础(字典的创建以及增删改查)_python中字典的增删改查

python中字典的增删改查

一、字典的基本信息

1.1定义

{key1:value1,key2:value2}

1.2特点

1.key必须为不可变数据类型,必须唯一
2.可以存放多个value,可修改,可以不唯一
3.无序
4.查询速度极快,且不受dict的大小影响

二、字典的创建

2.1法一

person={"name":"test","age":24}
print(person)
  • 1
  • 2

2.2法二

person=dict(name="test",age=24)
print(person)
  • 1
  • 2

2.3法三

person=dict({"name":"test","age":24})
print(person)
  • 1
  • 2

2.4法四

批量赋值

keys=[1,2,3,4]
print({}.fromkeys(keys,100))
print( {}.fromkeys([1,2,3,4],100))
  • 1
  • 2
  • 3

三、字典的增删改查

3.1 增加

family={
    "测试":[23,"女","tester"],
}
print("原字典:",family)
family["特斯汀"]=[24,"女","worker"]
print("增加后的字典:",family)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
# key值已存在,修改value值
family["特斯汀"]=[25,"女","worker"]
print("修改后的字典:",family)
  • 1
  • 2
  • 3

setdefault():不可修改value值

family.setdefault("学生",[18,"男","student"])
print("增加后的字典:",family)
  • 1
  • 2
family.setdefault("学生",[20,"男","student"])
print("测试修改后的字典:",family)
  • 1
  • 2

3.2 删除

原字典

family1={
   "测试":[23,"女","tester"],
   "学生":[18,"男","student"],
   "特斯汀":[25,"女","worker"],
   "高一":[23,"女","tester"],
   "高二":[23,"女","tester"],
}
print("原字典:",family1)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
3.2.1 删除指定key
del family1["高一"]
print("删除后的字典:",family1)
  • 1
  • 2
family1.pop("高二")
print("删除后的字典:",family1)
  • 1
  • 2
3.2.2 随便删除1个key
family1.popitem()
print("删除后的字典:",family1)
  • 1
  • 2
3.2.3 清空字典
family1.clear()
print("清空后的字典:",family1)
  • 1
  • 2

3.3 修改

原字典

family2={
   "测试":[23,"女","tester"],
   "学生":[18,"男","student"],
   "特斯汀":[25,"女","worker"],
   "高一":[23,"女","tester"],
   "高二":[23,"女","tester"],
}
print("原字典:",family2)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

修改其中的一个value值

family2["特斯汀"]=[25,"女","worker"]
print("修改后的字典:",family2)
  • 1
  • 2

合并两个字典,key值相同取合并的值

family3={
    "测试":[23,"女","test"],
    "高五":[18,"男","student"],
    "特斯":[22,"女","worker"],
    "高四":[23,"女","tester"],
    "高三":[23,"女","tester"],
}
family2.update(family3)
print("修改后的字典:",family2)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

3.4 查询

原字典

family4={
   "测试":[23,"女","tester"],
   "学生":[18,"男","student"],
   "特斯汀":[25,"女","worker"],
   "高一":[23,"女","tester"],
   "高二":[23,"女","tester"],
}
print("原字典:",family4)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
3.4.1根据key查询value
print("根据key查询value:",family4["测试"])#不存在,报错
  • 1
print("根据key查询value:",family4.get("高洁"))#不存在返回空
  • 1
3.4.2判断key值是否存在
print("判断key值是否存在:","ceshi" in family4)
  • 1
3.4.3查询key和value
print("查询value:",family4.values())
print("查询key:",family4.keys())
print("将key和value返回为一个元组:",family4.items())
  • 1
  • 2
  • 3
3.4.4 循环

只打印key值

for i in family4:
    print(i)
  • 1
  • 2

打印key和value

for i in family4:
    print(i,family4[i])
for i in family4.items():
    print(i)
for i,j in family4.items():
    print(i,j)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/繁依Fanyi0/article/detail/714491
推荐阅读
相关标签
  

闽ICP备14008679号