当前位置 博文首页 > sunjianxi1996的博客:python学习笔记——字典生成式

    sunjianxi1996的博客:python学习笔记——字典生成式

    作者:[db:作者] 时间:2021-07-28 11:45

    今日金句: 今天你敲代码了吗,社畜?

    author : Jack Sun

    date : 2021/4/7 11:48

    items = [‘Fruits’, ‘Books’, ‘Others’]
    prices = [100, 96, 98, 150] # 只会匹配三个
    d = {item: price for item, price in zip(items, prices)}
    print(d)
    ‘’’

    字典无序,也会生成无序的字典,也只匹配前三个

    items = {‘Fruits’, ‘Books’, ‘Others’}
    prices = {100, 96, 98, 150} # 以少的为基准,只会匹配三个
    d = {item: price for item, price in zip(items, prices)}
    print(d)
    ‘’’

    ‘’’
    总结:
    1.字典的创建:
    {}
    内置函数dict()
    字典生成式
    2.常用操作
    获取value
    字典名key
    字典名.get(key)
    删除key-value对
    del 字典名[key]
    修改/新增
    字典名[key] = value
    in / not in
    ‘’’

    cs
    下一篇:没有了