Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the usage of python dictionary

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

What is the use of python dictionary, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain for you in detail, people with this need can come to learn, I hope you can gain something.

Create a dictionary

Python can create dictionaries using the dict function, or curly braces, which are more common.

D = {"a": "Athens", "b": "Belgrade", "c": "Cairo"}

Or:

D = dict (a = "Athens", b = "Belgrade", c = "Cairo") traversal

Python can easily traverse the dictionary's keys, values, or get key and value when traversing. The following code demonstrates how to traverse:

For item in d.items (): print (item)

In this way, each item is a tuple, including key and value.

('averse,' Athens') ('baked,' Belgrade') ('clocked,' Cairo')

You can also get key and value through two variables when traversing:

For k, v in d.items (): print (k, v)

If you only care about keys:

For k in d.keys (): print (k)

If you only care about values:

For v in d.values (): print (v) access

Use the syntax of d [k] to get a value with a key of k, and throw a KeyError if key does not exist. For example, print (d ["a"]) in the dictionary just now gets Athens, but if it is d ["d"], because there is no entry with key of d, KeyError is thrown:

D = dict (a = "Athens", b = "Belgrade", c = "Cairo") print (d ["d"])

Run the code and throw an exception as follows:

Traceback (most recent call last): File "d:/temporary/python dict demo/test.py", line 4, in print (d ["d"]) KeyError:'d'

If you want to avoid throwing an exception, there are two ways: one is to use the get () method, and no corresponding Key returns None:

D = dict (a = "Athens", b = "Belgrade", c = "Cairo") print (d.get ("d"))

The second method is to judge by in before using it:

D = dict (a = "Athens", b = "Belgrade", c = "Cairo") if "a" in d: print (d ["a"]) modification

The easiest way is to modify it with the d [key] = value method, and create a new dictionary entry if key does not exist:

D ["a"] = "Atlanta"

The update () method updates the existing dictionary with a dictionary, and if the updated dictionary already contains the corresponding key, the original value is overwritten; if the updated dictionary does not contain the corresponding key, the key-value pair is added. For example, the following code:

D = dict (a = "Athens", b = "Belgrade", c = "Cairo") d.update ({"a": "Atlanta", "b": "Belgrade", "d": "Dehli"}) print (d)

To run the code, an is replaced, b remains the same, c is retained, d is added:

{'Cairo',:' Atlanta', 'baked:' Belgrade', 'cased:' Cairo', 'dumped:' Dehli'} delete

The clear () method deletes all dictionary entries

Del deletes the dictionary entry and throws KeyError if key does not exist

Pop () deletes the corresponding key value, and throws KeyError if key does not exist. You can set the default value if the key does not exist.

Example:

Del d ["a"] print (d.pop ("d", "none")) # returns none instead of the KeyErrorsetdefault () method

The setdefault () method returns the corresponding value when the key already exists, and if the key does not exist, increment the key-value pair according to the second parameter (default), such as:

D.setdefault ("a", "Atlanta") # because an already exists, return Athensd.setdefault ("d", "Dehli") # because d does not exist, set d to Dehli

Summary of dict usage and knowledge points after version 3.7, dict can save the location of key. Before 3.7, key is unordered, and SortedDict data structures can be used to care about the location of key.

The three methods pop () / get () / setdefault () can set the default value to avoid KeyError

The key of the dictionary cannot be repeated.

Is it helpful for you to read the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report