zoukankan      html  css  js  c++  java
  • 字典

    字典常用方法

    字典的表示方法:
    
    info_dic={'name':'haiyna','age':18,'sex':'female'}
    # 常用操作:
    #
    1.存/取
    info_dic={'name':'egon','age':18,'sex':'male'}
      print(info_dic['name11111111'])#找不到则报错了
      print(info_dic.get('name',None))
      print(info_dic.get('name222222',None))#get方法找不到不报错,可以自己设定默认值
    
    #pop:key存在则弹出值,不存在则返回默认值,如果没有默认值则报错
    # print(info_dic.pop('nam123123123123123123e',None))
    # print(info_dic)
    
    # print(info_dic.popitem())
    # print(info_dic)
    
    # info_dic['level']=10
    # print(info_dic)
    
    #
    #     删除
    info_dic={'name':'egon','age':18,'sex':'male'}
    # info_dic.pop()
    # info_dic.popitem()
    
    # del info_dic['name']
    
    
    #
    #     键s,值s,键值对
    info_dic={'name':'egon','age':18,'sex':'male'}
    # print(info_dic.keys())
    # print(info_dic.values())
    # print(info_dic.items())
    
    # for k in info_dic:
    #     # print(k,info_dic[k])
    #     print(k)
    
    # print('========>')
    # for k in info_dic.keys():
    #     print(k)
    
    # for val in info_dic.values():
    #     print(val)
    
    # for k,v in info_dic.items(): #k,v=('name', 'egon')
    #     print(k,v)
    
    
    
    #     长度
    # info_dic={'name':'egon','age':18,'sex':'male'}
    # print(len(info_dic))
    #
    #     循环
    #
    #     包含in
    
    # info_dic={'name':'egon','age':18,'sex':'male'}
    # print('name' in info_dic)
    # print('name' in info_dic.keys())
    # print('egon' in info_dic.values())
    # print(('name','egon') in info_dic.items())
    
    
    
    #掌握
    info_dic={'name':'egon','age':18,'sex':'male'}
    # info_dic.update({'a':1,'name':'Egon'})
    # print(info_dic)
    
    # info_dic['hobbies']=[]
    # info_dic['hobbies'].append('study')
    # info_dic['hobbies'].append('read')
    # print(info_dic)
    
    #setdefault:key不存在则设置默认值,并且放回值默认值
    #key存在则不设置默认,并且返回已经有的值
    
    # info_dic.setdefault('hobbies',[1,2])
    # print(info_dic)
    # info_dic.setdefault('hobbies',[1,2,3,4,5])
    # print(info_dic)
    
    # info_dic={'name':'haiyan','age':18,'sex':'male'}
    
    # {'name':'egon','age':18,'sex':'male','hobbies':['study']}
    # info_dic.setdefault('hobbies',[]).append('study')
    
    # {'name':'egon','age':18,'sex':'male','hobbies':['study','read']}
    # info_dic.setdefault('hobbies',[]).append('read')
    
    # {'name':'egon','age':18,'sex':'male','hobbies':['study','read','sleep']}
    # info_dic.setdefault('hobbies',[]).append('sleep')
    # l=info_dic.setdefault('hobbies',[])
    # print(l,id(l))
    # print(id(info_dic['hobbies']))
    
    # print(info_dic)
    
    
    
    #了解
    # d=info_dic.copy()
    # print(d)
    # info_dic.clear()
    # print(info_dic)
    
    
    # d=info_dic.fromkeys(('name','age','sex'),None)
    # print(d)
    # d1=dict.fromkeys(('name','age','sex'),None)
    # d2=dict.fromkeys(('name','age','sex'),('egon',18,'male'))
    # print(d1)
    # print(d2)
    
    
    
    
    # info=dict(name='haiyan',age=18,sex='male')
    # print(info)
    
    #
    # info=dict([('name','haiyan'),('age',18)])
    # print(info)
      
  • 相关阅读:
    1105 Spiral Matrix (25分)(蛇形填数)
    1104 Sum of Number Segments (20分)(long double)
    1026 Table Tennis (30分)(模拟)
    1091 Acute Stroke (30分)(bfs,连通块个数统计)
    1095 Cars on Campus (30分)(排序)
    1098 Insertion or Heap Sort (25分)(堆排序和插入排序)
    堆以及堆排序详解
    1089 Insert or Merge (25分)
    1088 Rational Arithmetic (20分)(模拟)
    1086 Tree Traversals Again (25分)(树的重构与遍历)
  • 原文地址:https://www.cnblogs.com/haiyan123/p/7213894.html
Copyright © 2011-2022 走看看