Python dict Usage

Source: Internet
Author: User

#字典的添加, delete, modify operations

Dict = {"A": "Apple", "B": "Banana", "G": "Grape", "O": "Orange"}

dict["W"] = "Watermelon"

Del (dict["a"])

Dict["g"] = "Grapefruit"

Print Dict.pop ("B")

Print Dict

Dict.clear ()

Print Dict

#字典的遍历

Dict = {"A": "Apple", "B": "Banana", "G": "Grape", "O": "Orange"}

For K in Dict:

Print "dict[%s] ="% k,dict[k]

Use of #字典items ()

Dict = {"A": "Apple", "B": "Banana", "C": "Grape", "D": "Orange"}

#每个元素是一个key和value组成的元组, output as a list

Print Dict.items ()

#调用items () Implementing a dictionary traversal

Dict = {"A": "Apple", "B": "Banana", "G": "Grape", "O": "Orange"}

For (K, V) in Dict.items ():

Print "dict[%s] ="% K, V

#调用iteritems () Implementing a dictionary traversal

Dict = {"A": "Apple", "B": "Banana", "C": "Grape", "D": "Orange"}

Print Dict.iteritems ()

For K, V in Dict.iteritems ():

Print "dict[%s] ="% K, V

For (K, v) in Zip (Dict.iterkeys (), Dict.itervalues ()):

Print "dict[%s] ="% K, V


#使用列表, dictionary as the value of the dictionary

Dict = {"A": ("apple",), "Bo": {"B": "Banana", "O": "Orange"}, "G": ["Grape", "Grapefruit"]}

Print dict["a"]

Print dict["a"][0]

Print dict["Bo"]

Print dict["Bo" ["O"]

Print dict["G"]

Print dict["G"][1]

Dict = {"A": "Apple", "B": "Banana", "C": "Grape", "D": "Orange"}

#输出key的列表

Print Dict.keys ()

#输出value的列表

Print dict.values ()

#每个元素是一个key和value组成的元组, output as a list

Print Dict.items ()

Dict = {"A": "Apple", "B": "Banana", "C": "Grape", "D": "Orange"}

it = Dict.iteritems ()

Print it

#字典中元素的获取方法

Dict = {"A": "Apple", "B": "Banana", "C": "Grape", "D": "Orange"}

Print Dict

Print Dict.get ("C", "Apple")

Print Dict.get ("E", "Apple")

Equivalent statement of #get ()

D = {"Key1": "Value1", "Key2": "Value2"}

If "Key1" in D:

Print d["Key1"]

Else

Print "None"

#字典的更新

Dict = {"A": "Apple", "B": "Banana"}

Print Dict

Dict2 = {"c": "Grape", "D": "Orange"}

Dict.update (DICT2)

Print Dict

Equivalent statement of #udpate ()

D = {"Key1": "Value1", "Key2": "Value2"}

E = {"Key3": "Value3", "Key4": "Value4"}

For K in E:

D[K] = E[k]

Print D

#字典E中含有字典D中的key

D = {"Key1": "Value1", "Key2": "Value2"}

E = {"Key2": "Value3", "Key4": "Value4"}

For K in E:

D[K] = E[k]

Print D

#设置默认值

Dict = {}

Dict.setdefault ("a")

Print Dict

Dict["a"] = "apple"

Dict.setdefault ("A", "default")

Print Dict

Sort #调用sorted ()

Dict = {"A": "Apple", "B": "Grape", "C": "Orange", "D": "Banana"}

Print Dict

#按照key排序

Print sorted (Dict.items (), Key=lambda d:d[0])

#按照value排序

Print sorted (Dict.items (), Key=lambda d:d[1])

#字典的浅拷贝

Dict = {"A": "Apple", "B": "Grape"}

Dict2 = {"c": "Orange", "D": "Banana"}

Dict2 = Dict.copy ()

Print Dict2


#字典的深拷贝

Import Copy

Dict = {"A": "Apple", "B": {"G": "Grape", "O": "Orange"}}

Dict2 = Copy.deepcopy (dict)

Dict3 = Copy.copy (dict)

dict2["B" ["g"] = "Orange"

Print Dict

dict3["B" ["g"] = "Orange"

Print Dict


This article is from the "My Ops Time" blog, so be sure to keep this source http://aaronsa.blog.51cto.com/5157083/1783324

Python dict Usage

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.