美文网首页
Python基础数据类型dict

Python基础数据类型dict

作者: 三人行大道 | 来源:发表于2018-11-19 15:07 被阅读0次

    1.通过关键字dict和关键字参数创建

    diction = dict(dasan=1,xudachuan=2,bar=3)
    print(diction)
    print(type(diction))
    
    {'dasan': 1, 'xudachuan': 2, 'bar': 3}
    <class 'dict'>
    

    2.直接创建

    a = {"a":"b","c":"d"}
    
    @还可以这样
    d = {}
    d["a"] ="b"
    d["c"] = "d"
    {'a': 'b', 'c': 'd'}
    
    

    3.把两个列表合成字典(用zip)

    list1 = ['你好', '他好', '大家好']; list2 = [1, 2, 3]; dict1 = dict(zip(list1, list2))
    {'大家好': 3, '他好': 2, '你好': 1}
    
    

    这个还没遇到:dict.fromkeys()

    相关文章

      网友评论

          本文标题:Python基础数据类型dict

          本文链接:https://www.haomeiwen.com/subject/bejofqtx.html