zoukankan      html  css  js  c++  java
  • python 字典中的Update()函数

    简介 
    Python 字典 update() 方法用于更新字典中的键/值对,可以修改存在的键对应的值,也可以添加新的键/值对到字典中。

    语法 
    d.update(e)

    参数说明 
    将e中键-值对添加到字典d中,e可能是字典,也可能是键-值对序列。详见实例。

    返回值 
    该方法没有任何返回值。

    实例 
    以下实例展示了 update() 方法的使用方法:

    d = {‘one’:1,’two’:2}

    d.update({‘three’:3,’four’:4}) # 传一个字典 
    print(d)

    d.update(five=5,six=6) # 传关键字 
    print(d)

    d.update([(‘seven’,7),(‘eight’,8)]) # 传一个包含一个或多个元组的列表 
    print(d)

    d.update(([‘nice’,9],[‘ten’,10])) # 传一个包含一个或多个列表的元组 
    print(d)

    d.update(zip([‘eleven’,’twelve’],[11,12])) # 传一个zip()函数 
    print(d)

    d.update(one=111,two=222) # 使用以上任意方法修改存在的键对应的值 
    print(d)

    以上实例输出结果为:

    {‘one’: 1, ‘four’: 4, ‘three’: 3, ‘two’: 2} 
    {‘one’: 1, ‘four’: 4, ‘three’: 3, ‘five’: 5, ‘two’: 2, ‘six’: 6} 
    {‘seven’: 7, ‘one’: 1, ‘four’: 4, ‘three’: 3, ‘five’: 5, ‘two’: 2, ‘six’: 6, ‘eight’: 8} 
    {‘seven’: 7, ‘one’: 1, ‘four’: 4, ‘three’: 3, ‘ten’: 10, ‘five’: 5, ‘nice’: 9, ‘two’: 2, ‘six’: 6, ‘eight’: 8} 
    {‘one’: 1, ‘four’: 4, ‘three’: 3, ‘twelve’: 12, ‘ten’: 10, ‘seven’: 7, ‘six’: 6, ‘eleven’: 11, ‘two’: 2, ‘nice’: 9, ‘five’: 5, ‘eight’: 8} 
    {‘one’: 111, ‘four’: 4, ‘three’: 3, ‘twelve’: 12, ‘ten’: 10, ‘seven’: 7, ‘six’: 6, ‘eleven’: 11, ‘two’: 222, ‘nice’: 9, ‘five’: 5, ‘eight’: 8}

    作者:蛛丝马迹
        
    no copyright, 欢迎转载
  • 相关阅读:
    教你分分钟学会用python爬虫框架Scrapy爬取心目中的女神
    那些年,我们在Django web开发中踩过的坑(一)——神奇的‘/’与ajax+iframe上传
    刷题记录:[De1CTF 2019]Giftbox && Comment
    刷题记录:[强网杯 2019]Upload
    刷题记录:[XNUCA2019Qualifier]EasyPHP
    [RoarCTF 2019]simple_uplod
    [RoarCTF 2019]Online Proxy
    [RoarCTF]Easy Java
    [RoarCTF]Easy Calc
    刷题记录:[DDCTF 2019]homebrew event loop
  • 原文地址:https://www.cnblogs.com/python-wang/p/9443325.html
Copyright © 2011-2022 走看看