zoukankan      html  css  js  c++  java
  • 字典

    
    

    1.字典创建:


    字典的每个键值(key=>value)对用冒号(:)分割,每个对之间用逗号(,)分割,整个字典包括在花括号({})中,键必须是唯一的,但值则不必

    dic = {key1:value1,key2,value2}



    2.访问字典:

    dict1 = {'name':'花花','age':23,'address':'sichuan'}
    print(dict1['name']) # 花花

    for key in dict1:print(key)



    3.修改字典:

    dict1 = {'name':'花花','age':23,'address':'sichuan'}
    dict1['name'] = 'bobo' #,修改字典name的值
    dict1['sex'] = 'girl' # 添加sex键值对
    print(dict1['name']) # bobo
    print(dict1['sex']) # girl



    4. 删除字典:

    dict1 = {'name':'花花','age':23,'address':'sichuan'}

    del dict1['name'] #删除键'name'
    print(dict1) # {'age': 23, 'address': 'sichuan'}
    dict1.clear() #删除字典内所有元素 {}
    del dict1 # 删除字典



    5.函数与方法:

    dict.keys() # 以列表返回一个字典所有的键
    dict.values() # 以列表返回字典中的所有值
    dict.items() # 以列表返回可遍历的(键, 值) 元组数组
    dict.update(dict2) # 把字典dict2的键/值对更新到dict里
    dict.get(key, default=None)   # 返回指定键的值,如果值不在字典中返回默认值
    dict.pop(self, k, d=None)    # 如果键值key存在与字典中,删除dict[key],返回 dict[key]的value值。key值必须给出。否则,返回default值。如果default值没有过出,就会报出KeyError异常。
    dict.fromkeys(seq[, value])) # fromkeys() 函数用于创建一个新字典,以序列seq中元素做字典的键,value为字典所有键对应的初始值
    dict.setdefault(key, default=None) # 如果键不已经存在于字典中,将会添加键并将值设为默认值
    key -- 字典中要查找的键
    default -- 如果指定键的值不存在时,返回该默认值值
    seq -- 字典键值列表
    value -- 可选参数, 设置键序列(seq)的值

    dict1 = {'name':'花花','age':23,'address':'sichuan'}
    seq = ['name','age','sex','weight']
    dict2 = {'hobby':'reading'}

    print(dict1.pop('name')) # 花花
    print(dict1.values()) # dict_values([23, 'sichuan'])
    print(dict1.keys()) # dict_keys(['age', 'address'])
    print(dict1.items()) # dict_items([('age', 23), ('address', 'sichuan')])
    print(dict1.get('age')) # 23

    dict1.update(dict2) # {'age': 23, 'address': 'sichuan', 'hobby': 'reading'}
    print(dict1)

    dict1.setdefault('honghong','none') # {'name': '花花', 'age': 23, 'address': 'sichuan', 'honghong': 'none'}
    print(dict1)

    dict4 = dict.fromkeys(seq,'huahua') # {'name': 'huahua', 'age': 'huahua', 'sex': 'huahua', 'weight': 'huahua'}
    print(dict4)

    
    
    6.other

    1.for:循环可迭代对象中的内容,continue,break

    li = [1,2,3,4,5]
    for each in li:print(each)


    2.range

    range(stop) -> range object
    range(start, stop[, step]) -> range object

    for i in range(10):print(i)
    for each in range(1,10):print(each)
    for item in range(1,10,2):print(item)
    for j in range(10,1,-1):print(j)


    3. enumerate(iterable[, start]) -> iterator for index, value of iterable

    为可迭代的对象添加序号
    li = ['google','firefox','IE','edge']
    for index,value in enumerate(li,0):
    print(index,value)
  • 相关阅读:
    Sage CRM升级后的问题跟进
    js 控制滚动条位置
    初学Jquery插件制作:在SageCRM的查询屏幕隐藏部分行的功能
    [转摘] Reporting Service 200
    js数组去重复项
    JavaScript tips 1024 * 768
    javascript 的 replace 函数
    the secret of sagecrm urls
    Sage CRM 自增ID的方案和遇到的问题
    EXTJS 可编辑列表的时间编辑问题
  • 原文地址:https://www.cnblogs.com/lovuever/p/6580161.html
Copyright © 2011-2022 走看看