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 knowledge about dictionaries in Python

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

Share

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

This article mainly explains "what is the knowledge about dictionaries in Python". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Now let the editor take you to learn "what is the knowledge about dictionaries in Python"?

Dictionary (dict)

Dic is a mapping type, consisting of key-value pairs enclosed by {}. Key is unique in dict. At the time of saving, a unique memory address is calculated according to key. Then save the key-value in this address. This algorithm is called the hash algorithm, so the key in the key-value stored in dict must be hash. If you don't understand what a hash is, you can remember it for the time being. What can be changed is immutable, but hashing means immutable. This is specified in order to accurately calculate the memory address. Known hash (immutable) data types: int, str, tuple, bool

Non-hashing (mutable) data types: list, dict, set

Syntax: {key1: value1, key2: value2....}

The data saved by dict is not saved in the order we added it. It is saved in the order of the hash table. The hash table is not continuous. Therefore, slicing work cannot be carried out. It can only get the data in dict through key, before 3.6. The order of key-value pairs in the result of printing a dictionary is out of order. After 3.6, the order of key-value pairs is the same as the order of input, but it is still out of order when saved.

Add:

1.dict [a key0 = value that does not exist in the dictionary, a new key-value pair is added.

2.setdefalt (): set the default value. When value is not set for a key, key is equal to the default value. After setting it in method 1, value will be the set value.

Delete:

1.pop (key): similar to list's pop, except that list uses subscript and dict uses key

The usage of 2.del keyword is the same as list.

3.popitem (): randomly deletes a key-value pair

4.clear (): empty, no one left

Modify:

1.dict [an existing key] = value will reassign the existing key-value pair, overwriting the original value

2.dict1.update (dict2): update the key-value pair in dict2 to dict1, and the same key-value pair in key will be overwritten, and the key-value pair without will be added.

Query:

1..dict [an existing key] an error is reported when key does not exist in dict

2.get (key,defalt=None): unlike method 1, None is returned if .key does not exist. This None can be modified by parameter defalt.

Other related actions:

Example: dic = {"id": 123, "name": 'sylar', "age": 18, "ok": "Kobe"}

1.print (dic.keys) # dict_keys (['id',' name', 'age',' ok']) is similar to list, but it is not the result of list,print (type (dic.keys)), but it can be used as list, for loop can be performed.

For key in dic.keys:

Print (key)

2.print (dic.values ()) # is basically the same as keys and has the same usage

For value in dic.values:

Print (value)

3.print (dic.items ()) # dict_items ([('id', 123), (' name', 'sylar'), (' age',18), ('ok',' Kobe')])

For key, value in dic.items:

Print (key, value)

* A special case: directly traversing dic, printing is also key.

For i in dic:

Print (I)

At this point, I believe you have a deeper understanding of "what is the knowledge of dictionaries in Python?" you might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

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