zoukankan      html  css  js  c++  java
  • Python 数据类型-3

    字典 (dict)

    Python中唯一的映射的类型(哈希表)
    字典对象是可变的,但是字典的键必须使用不可变的对象,一个字典可以使用不同类型的键值
    

    定义字典:

    
    
    In [68]: dic = {}   #使用 {}创建空字典
    In [69]: dic
    Out[69]: {}
    In [70]: type(dic)
    Out[70]: dict
    
    In [71]: dic = {'a':1, 1:'abc'}   #使用 {} 创建字典
    In [72]: dic
    Out[72]: {1: 'abc', 'a': 1}   #无序
    In [73]: type(dic)
    Out[73]: dict
    
    In [77]: list1 = ['name', 'age'] #将列表合并为字典
    In [78]: list2 = ['tom', 20]
    In [79]: dict(zip(list1, list2))  #dict()函数创建字典
    Out[79]: {'age': 20, 'name': 'tom'}
    
    
    In [81]: dict(x=10,y=11)   ##使用dict()函数创建字典
    Out[81]: {'x': 10, 'y': 11}
     
    In [82]: dict.fromkeys('abce',100) ##使用dict.fromkeys()方法创建字典
    Out[82]: {'a': 100, 'b': 100, 'c': 100, 'e': 100}
    
    

    方法:

    get() 根据键获取值

    k = {"name":"lijunjiang", "age":20}
    
    print(k.get("name"))
    print(k.get("age"))
    print(k.get("address"))
    执行:
    C:Python27python.exe D:/Python/type-of-data1.py
    lijunjiang
    20
    None  #没有的键值返回 None
    
    Process finished with exit code 0
    
    

    setdefault() 设置默认值,当KEY有值时,保持原值,当key没有值则设置该key的值

    k = {"name":"lijunjiang", "age":20}
    print(k.setdefault("name", "libai"))
    print(k.setdefault("age", 19))
    print(k.setdefault("address", "beijing"))
    
    print(k)
    
    执行:
    C:Python27python.exe D:/Python/type-of-data1.py
    lijunjiang
    20
    beijing
    {'age': 20, 'name': 'lijunjiang', 'address': 'beijing'}
    
    Process finished with exit code 0
    
    

    keys() 获的字典的Key (一次性取出所有)

    iterkeys() 获得一个对象 (逐个取出) 效率高

    k = {"name":"lijunjiang", "age":20}
    
    print(k.keys())
    
    执行:
    C:Python27python.exe D:/Python/type-of-data1.py
    ['age', 'name']
    
    Process finished with exit code 0
    
    

    values() 获取所有key的值(一次性取出所有)
    iteritems() 获得一个对象(逐个取出) 效率高

    k = {"name":"lijunjiang", "age":20}
    
    print(k.values())
    
    执行:
    C:Python27python.exe D:/Python/type-of-data1.py
    [20, 'lijunjiang']
    
    Process finished with exit code 0
    
    

    iterkey iteritems items(逐个将key:values取出返回一个列表)

    k = {"name":"lijunjiang", "age":20}
    
    for x, y in k.iteritems():
        print( x , y)
    
    print(k.items())
    执行:
    
    C:Python27python.exe D:/Python/type-of-data1.py
    ('age', 20)
    ('name', 'lijunjiang')
    [('age', 20), ('name', 'lijunjiang')]
    
    Process finished with exit code 0
    
    

    pop() 删除某个在的键值,必须输入一个存在的key,否则会报错

    k = {"name":"lijunjiang", "age":20}
    k.setdefault("addr", "beijing")
    print(k)
    
    k.pop("name")
    print(k)
    k.pop("addr")
    print(k)
    
    
    执行:
    C:Python27python.exe D:/Python/type-of-data1.py
    {'age': 20, 'name': 'lijunjiang', 'addr': 'beijing'}
    {'age': 20, 'addr': 'beijing'}
    {'age': 20}
    
    Process finished with exit code 0
    
    

    fromkeys()

    list1 = ["a", "b", "c", "d"]
    k = {}
    n = k.fromkeys(list1, 123)
    print(n)
    
    执行:
    C:Python27python.exe D:/Python/type-of-data1.py
    {'a': 123, 'c': 123, 'b': 123, 'd': 123}
    
    Process finished with exit code 0
    

    zip()

    list1 = ["a", "b", "c", "d"]
    list2 = [12, 234, 32, 23]
    n = zip(list1, list2)
    print(n)
    
    执行:
    C:Python27python.exe D:/Python/type-of-data1.py
    [('a', 12), ('b', 234), ('c', 32), ('d', 23)]
    
    Process finished with exit code 0
    
    #########################################################33
    
    dict() 
    
    列表转换成字典
    
    list1 = ["a", "b", "c", "d"]
    list2 = [12, 234, 32, 23]
    n = dict(zip(list1, list2))
    print(n)
    
    执行:
    C:Python27python.exe D:/Python/type-of-data1.py
    {'a': 12, 'c': 32, 'b': 234, 'd': 23}
    
    Process finished with exit code 0
    
    

    dict.updata()

    list1 = ["a", "b", "c", "d"]
    list2 = [12, 234, 32, 23]
    n = dict(zip(list1, list2))
    print(n)
    
    k = {"name": "lijunjiang", "age": 20}
    
    k.update(n)
    print(k)
    
    执行:
    C:Python27python.exe D:/Python/type-of-data1.py
    {'a': 12, 'c': 32, 'b': 234, 'd': 23}
    {'a': 12, 'c': 32, 'b': 234, 'name': 'lijunjiang', 'age': 20, 'd': 23}
    
    Process finished with exit code 0
    

    sorted()

    mm = dict(a=1, b=120, c=5, d=50)
    print sorted(mm.iteritems(), key = lambda d: d[1], reverse=True)
    print sorted(mm.iteritems(), key = lambda a: a[0], reverse=True)
    
    执行:
    C:Python27python.exe D:/Python/type-of-data1.py
    [('b', 120), ('d', 50), ('c', 5), ('a', 1)]
    [('d', 50), ('c', 5), ('b', 120), ('a', 1)]
    
    Process finished with exit code 0
    
    
  • 相关阅读:

    k
    通过类名调用类方法
    类Area的getArea方法是一个重载方法
    构造cry
    两个lader对象共享bottom
    向一个方法的基本数据类型参数传值
    Circle
    常量的用法
    显示本机时间
  • 原文地址:https://www.cnblogs.com/lijunjiang2015/p/7720248.html
Copyright © 2011-2022 走看看