• 技术文章 >Python技术 >Python基础教程

    如何使用python 字典添加数据?

    2020-11-08 21:10:55原创2813
    关于python添加的内容并不少,大部分小伙伴都非常喜欢用python去添加内容,根据大家的喜爱程度,给大家又准备了关于添加数据的内容,就是使用字典,小伙伴想不想了解?接下来看下面内容哦~

    首先新建一个python文件命名为py3_dict.py,在这个文件中进行字符串操作代码编写(如下为代码,文后有显示运行效果):

    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    11

    12

    13

    14

    15

    16

    17

    18

    19

    20

    21

    22

    23

    24

    25

    26

    27

    28

    29

    30

    31

    32

    33

    34

    35

    36

    37

    38

    39

    40

    41

    42

    43

    44

    45

    46

    47

    48

    49

    50

    51

    52

    53

    54

    55

    56

    57

    58

    59

    60

    61

    62

    63

    64

    65

    66

    #dictionaries 是一个Key-Value对形式的集合

    #定义一个字典

    student = {'name':'yale','age':25,'course':['数学','计算机']}

    print(student)

    print(student['name'])

    print(student['course'])

    #字典的key和value可定义为immutable data type

    #例如:定义key为1

    student = {1:'yale','age':25,'course':['数学','计算机']}

    print(student[1])

    #访问一个不存在的key

    #会出现异常

    #KeyError: 'phone'

    student = {'name':'yale','age':25,'course':['数学','计算机']}

    #print(student['phone'])

    #有时候我们希望不存在的key

    #可以返回None或者一个默认值

    #用如下方式实现:

    print(student.get('phone'))#None

    print(student.get('phone','未找到'))#返回默认值:未找到

    #往dict字典中添加数据

    student = {'name':'yale','age':25,'course':['数学','计算机']}

    student['phone']='010-55555555'

    print(student.get('phone','未找到'))#010-55555555

    #改变已存在的key对应的值

    student = {'name':'yale','age':25,'course':['数学','计算机']}

    student['name']='andy'

    print(student)

    #使用update() 改变字典中的多个值

    student = {'name':'yale','age':25,'course':['数学','计算机']}

    student.update({'name':'andy','age':26,'phone':'12345678'})

    print(student)

    #删除一个key

    #使用del 关键字

    del student['phone']

    print(student)

    #或者使用之前提到过的pop()方法

    #删除数据

    age = student.pop('age')

    print(age)#26

    print(student)

    #使用len()查看字典中一共有多少key

    student = {'name':'yale','age':25,'course':['数学','计算机']}

    print(len(student))#3

    #查看所有的key

    print(student.keys())#dict_keys(['name', 'age', 'course'])

    #查看所有的value

    print(student.values())#dict_values(['yale', 25, ['数学', '计算机']])

    #查看所有的key和value

    #得到一对一对的key-value

    #dict_items([('name', 'yale'), ('age', 25), ('course', ['数学', '计算机'])])

    print(student.items())

    #循环字典

    #像list的方式循环,打印的是key值

    #name

    #age

    #course

    for key in student:

     print(key)

    #所以我们用items()方法循环数据:

    for key,value in student.items():

     print(key,value)

    #结果:

    #name yale

    #age 25

    #course ['数学', '计算机']

    以上代码运行效果:

    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    11

    12

    13

    14

    15

    16

    17

    18

    19

    20

    21

    22

    {'name': 'yale', 'age': 25, 'course': ['数学', '计算机']}

    yale

    ['数学', '计算机']

    yale

    None

    未找到

    010-55555555

    {'name': 'andy', 'age': 25, 'course': ['数学', '计算机']}

    {'name': 'andy', 'age': 26, 'course': ['数学', '计算机'], 'phone': '12345678'}

    {'name': 'andy', 'age': 26, 'course': ['数学', '计算机']}

    26

    {'name': 'andy', 'course': ['数学', '计算机']}

    3

    dict_keys(['name', 'age', 'course'])

    dict_values(['yale', 25, ['数学', '计算机']])

    dict_items([('name', 'yale'), ('age', 25), ('course', ['数学', '计算机'])])

    name

    age

    course

    name yale

    age 25

    course ['数学', '计算机']

    好啦,如果项目里需要用到以上内容,大家可以参考上面示例,根据自己的需求,设计代码哦~

    专题推荐:python字典添加数据
    上一篇:Python画图用到哪些库? 下一篇:如何快速掌握使用Python函数嵌套?

    相关文章推荐

    • 如何使用python3字符串格式化输出?• 怎么用python3画烟花?代码是什么?• Python3中的 print 函数有哪些用法?• 怎么快速掌握使用python中if和elif?

    全部评论我要评论

    © 2021 Python学习网 苏ICP备2021003149号-1

  • 取消发布评论
  • 

    Python学习网