Learning notes for Python/002-5 (2018-5-21)

Source: Internet
Author: User

dictionaries and their operations
The dictionary is used to store the two corresponding keys and values, that is, the dict type; When a dictionary is created, each key is used to get the corresponding value, and the key in the dictionary cannot be duplicated; it is characterized by a Key-value key value type, unordered, and no subscript can be found by subscript.
(1) Dictionary replacement, add, delete, find

infor={    ‘2014132001‘:"DiaoCan",    ‘2014132002‘:"DaQiao",    ‘2014132003‘:"HuanhYueYing"}print(infor)infor[‘2014132002‘]=‘XiaoQiao‘      # 替换print(infor)infor[‘2014132089‘]=‘wuzhetain‘    # 添加print(infor)# del infor[‘2014132089‘]     # 删除#infor.popitem()     # 随机删除infor.pop (‘2014132089‘)print(infor)print(infor[‘2014132001‘])   # 查找print(infor.get(‘2014132001‘) )  #  更为安全的查找,当键不存在的时候不会报错print(‘2014132001‘ in infor)   #  查某是否存在,返回的是布尔类型

(2) Updating of dictionaries

infor={    ‘2014132001‘:"DiaoCan",    ‘2014132002‘:"DaQiao",    ‘2014132003‘:"HuanhYueYing"}print(infor)a={    ‘2014132001‘:‘LuBu‘,    ‘2014132004‘:‘Guanyu‘,    ‘2014132005‘:‘Dongzhuo‘}infor.update(a)   # 有的就更新,没有的就添加print(infor)

(3) Conversion and extension of dictionaries

infor={    ‘2014132001‘:"DiaoCan",    ‘2014132002‘:"DaQiao",    ‘2014132003‘:"HuanhYueYing"}print(infor)print(infor.items() )   # 将字典转化为列表b=dict.fromkeys([1,2,3,4],[1,{‘name‘:‘zhangfei‘},‘Zhaoyun‘])  # 这里要注意的就是1,2,3,4所对应的值是同一个print(b)b[2][1][‘name‘]=‘Xiahoudun‘print(b)

(4) Output of the dictionary

infor={    ‘2014132001‘:"DiaoCan",    ‘2014132002‘:"DaQiao",    ‘2014132003‘:"HuanhYueYing"}print(infor)for i in infor:     # 建议使用这个    print(i,infor[i])for k,v in infor.items():  # 多了一步转换为列表    print(k,v)

(5) Multilevel specified nesting

Site_of_world={    ‘American‘:{        ‘www.barrett.net‘:["Barrett Firearms Manufacturing","巴雷特×××公司"],        ‘sands.com‘:["Las Vegas Sands","拉斯×××金沙集团 "],        ‘www.omnicomgroup.com‘:["Omnicom Group","奥米康集团"],        ‘www.wyethnutrition.com.hk ‘:["Wyeth","惠氏"]    }    ,    ‘Japan‘:{        ‘www.unicharm.com.cn‘:["Unicharm Group","尤妮佳集团"],        ‘www.klab.com/cn‘:["KLab","可来"]    }    ,    ‘Chain‘:{        ‘hknd-group.com/cn/‘:["HKND Group","HKND集团"],        ‘www.eegmusic.com‘:["Emperor Entertainment","英皇娱乐"]    }}print(Site_of_world.keys())print(Site_of_world.values())print(Site_of_world)Site_of_world.setdefault(‘Chain‘,{‘www.baidu.com‘:"百度 "})  # 键存在的情况下,不改变value的值print(Site_of_world)Site_of_world.setdefault(‘South Korea‘,{‘www.smtown.com‘:"SM娱乐"}) # 键不存在的情况下,创建新的key-valueprint(Site_of_world)

Learning notes for Python/002-5 (2018-5-21)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.