• python xx003字典


    将2个列表根据元素位置合并成字典:

    方法一:

    >>> a
    ['shanghai', 'chengdu', 'shandong']
    >>> b
    ['wo', 'chun', 'bing']
    >>> c = dict(zip(a,b))
    >>> c
    {'shanghai': 'wo', 'chengdu': 'chun', 'shandong': 'bing'}
    >>> c['shandong']
    'bing'

    取2个列表同样位置的元素:

    >>> a
    ['shanghai', 'chengdu', 'shandong']
    >>> b
    ['wo', 'chun', 'bing']
    >>> b[a.index('shandong')]
    'bing'
    >>> 

    创建列表

    #方法一
    >>> dict1 = dict((('a' ,20), ('b' ,30)))
    >>> dict1
    {'a': 20, 'b': 30}
    #方法二:
    >>> dict2 = dict('i'='shandong', 'she'='chengdu')
    SyntaxError: keyword can't be an expression
    >>> dict2 = dict(i='shandong', she='chengdu')
    >>> dict2
    {'i': 'shandong', 'she': 'chengdu'}
    #列表增加元素
    >>> dict2['he'] = 'chongqing'
    >>> dict2
    {'i': 'shandong', 'she': 'chengdu', 'he': 'chongqing'}
    #创建字典,指定values为kong
    >>> c = dict.fromkeys(('1', '2'), 'kong') >>> c {'1': 'kong', '2': 'kong'} >>> c.values() dict_values(['kong', 'kong']) >>> c.items() dict_items([('1', 'kong'), ('2', 'kong')]) >>> c.keys() dict_keys(['1', '2']) #c字典增加key和value >>> c['3']='duo' >>> c {'1': 'kong', '2': 'kong', '3': 'duo'} >>>
    >>> d = dict.fromkeys((range(5)), 'a')
    >>> d
    {0: 'a', 1: 'a', 2: 'a', 3: 'a', 4: 'a'}
    >>> for key in d.keys():
        print(key)
    
        
    0
    1
    2
    3
    4
    >>> for value in d.values():
        print(value)
    
        
    a
    a
    a
    a
    a
    >>> for k, v in d.items():
        print(k, v)
    
        
    0 a
    1 a
    2 a
    3 a
    4 a
    >>> for k in d.items():
        print(k)
    
        
    (0, 'a')
    (1, 'a')
    (2, 'a')
    (3, 'a')
    (4, 'a')
    >>> d
    {0: 'a', 1: 'a', 2: 'a', 3: 'a', 4: 'a'}
    # get取值时如果取的值不存在则返回none,不报错
    >>> d.get('5')
    >>> print(d.get(5))
    None
    >>> 4 in d
    True
    >>> 5in d
    False
    >>> d.clear()
    >>> d
    {}
    >>> a = {'1':'one', '2':'two', '3':'three'}
    # 浅拷贝
    >>> b = a.copy()
    >>> c = a
    >>> a
    {'1': 'one', '2': 'two', '3': 'three'}
    >>> b
    {'1': 'one', '2': 'two', '3': 'three'}
    >>> c
    {'1': 'one', '2': 'two', '3': 'three'}
    >>> c['4']='four'
    >>> a
    {'1': 'one', '2': 'two', '3': 'three', '4': 'four'}
    >>> b
    {'1': 'one', '2': 'two', '3': 'three'}
    >>> c
    {'1': 'one', '2': 'two', '3': 'three', '4': 'four'}
    >>> e ={'xiaobai':'dog'}
    # 将字典e的元素update到b字典中
    >>> b.update(e)
    >>> b
    {'1': 'one', '2': 'two', '3': 'three', 'xiaobai': 'dog'}
    >>> b
    {'1': 'one', '2': 'two', '3': 'three', 'xiaobai': 'dog'}
    >>> cc =10
    >>> dd = 'key'
    # update格式为括号内加字典,即d.update({key:value})
    >>> b.update({cc:dd})
    >>> b
    {'1': 'one', '2': 'two', '3': 'three', 'xiaobai': 'dog', 10: 'key'}
  • 相关阅读:
    ul做导航栏
    论布局,bfc,margin塌陷和合并,经典bug
    mon-hom
    新浪下拉菜单模仿
    JQ筛选方法,筛选父子元素
    JQuery筛选选择器
    JQuery隐式迭代
    python 和 C# DES加密
    交互设计[1]--设计心理学
    javascript学习(9)——[设计模式]单例
  • 原文地址:https://www.cnblogs.com/joeshang/p/12570843.html
Copyright © 2020-2023  润新知