• 字典内置


    字典内置:

    作用:

    键值对存储

    定义方法:

    {}内有多个值

    使用方法:

    1.按key取值

    2.按key修改

    3.按key增加值

    4.keys()提取所有key

    5.values()提取所有value

    6.items()提取所有的item

    7.get()

    8.setdefault()

    9.update()

    # 字典内置方法:字典自己使用
    
    banzhuren_info_dic = {'name': 'guolaoshi', 'height': 150, 'weight': 130,
                          'hobby_list': ['jiao', 'tiao', 'music']}
    
    # 1. 按key取值/按key修改值/按key增加值
    print(banzhuren_info_dic['height'])
    banzhuren_info_dic['height'] = banzhuren_info_dic['height'] + 1
    print(banzhuren_info_dic['height'])
    banzhuren_info_dic['age'] = 30
    print(banzhuren_info_dic)
    
    # 2. 成员运算(比较的是key)
    print('age' in banzhuren_info_dic)  # key里没有18
    
    # 3. for循环(对key循环)
    for i in banzhuren_info_dic:
        print(i)
    
    # 4. keys()/values()/items()  --> 当作列表
    print(banzhuren_info_dic.keys())  # 所有的key
    print(banzhuren_info_dic.values())  # 所有的值
    print(banzhuren_info_dic.items())  # 所有的键值对(以列表存储)
    
    for i in banzhuren_info_dic.items():
        print(i[0] + '*****' + str(i[1]))
    
    # 5. get(): 取值
    # print(banzhuren_info_dic['height1'])
    print(banzhuren_info_dic.get('height', 150))  # 找到了就找了;没有值返回None,如果给定了150,没有找到就150
    
    # # 购物车没有的话为1,有的话加1
    # shopping_car = {}
    #
    # if shopping_car.get('wawa'): # none
    #     shopping_car['wawa'] = shopping_car['wawa'] + 1
    # else:
    #     shopping_car['wawa'] = 1
    # print(shopping_car)
    
    # # 6. update(): 扩展字典
    # dic1 = {'a':1}
    # dic2 = {'b':2}
    # dic1.update(dic2)
    # print(dic1)
    
    # 7. setdefault(): 有则不更改,没有则增加
    dic1 = {'a': 1}
    dic1.setdefault('a', 2)
    print(dic1)
    
    # 能够刷leetcode的时候想到
    dic1.update()
    
    # 考察一个人平常在干什么-->每天在敲代码--》快捷键使用的熟练度
    # file edit view navigate code refactor run tools vsc window help
    print('zieniubi,woduoniubi')
    
    
    
    
  • 相关阅读:
    线段树
    数学建模中的excel操作
    POJ 3666 Making the Grade
    POJ 1742 Coins
    CF 55D
    POJ 3280 Cheapest Palindrome
    牛客 处女座与复读机
    牛客 处女座的约会
    牛客 小a与星际探索
    POJ 2229 递推
  • 原文地址:https://www.cnblogs.com/shaozheng/p/11435251.html
Copyright © 2020-2023  润新知