Python's dict dictionary structure operation method
This article mainly introduces the operation method of Python's dict dictionary structure. Learning notebook. The operation of dictionary is the basic knowledge for introductory learning of Python. Friends who need it can refer to
1. Dictionary Basic method
1. Create a new dictionary
1) Create an empty dictionary
1 2 3 4 |
|
2), initialize a value when creating a new one
1 2 3 |
|
3), use tuple
1 2 3 |
|
2. Obtaining method
1), get(key) Get the value corresponding to a key from the dictionary and return value
1 2 3 |
|
If it does not exist in the dictionary, return a NoneType
1 2 |
|
If the required key value does not exist, specify another value to return
1 2 |
|
2), keys () Get all the key values in the dictionary and return a list
1 2 |
|
3), values() corresponds to the keys() method, and returns the keys in the dictionary List of all values
1 2 |
|
4), items() returns a tuple corresponding to (key, value)
1 2 |
|
5), iterkeys(), itervalues(), iteritems() also obtain all key, value, (key, value) ancestors respectively, but they no longer return a list, but an iterator
1 2 3 4 5 |
|
3. Method of setting dictionary value
1) The direct method is
1 2 3 |
|
However, this method is that if the key value I want to add is already in the dictionary, then the original one will be overwritten. The value
1 2 3 |
|
2), setdefault(key, value) The advantage of this method is that if the inserted key does not exist in the dictionary, then insert it into the dictionary and Return the value. Otherwise, if it exists in the dictionary, then the existing value will be returned and will not be overwritten.
1 2 3 4 5 6 7 8 |
|
4. Delete the dictionary
1) pop(key) deletes an item of the specified key and successfully returns the value of the deleted item. If it does not exist, an exception will be thrown. Therefore, when using this method, you must judge whether the key is Exists, or catch this exception
1 2 3 4 5 6 7 8 9 10 |
|
1 2 3 4 5 6 7 8 |
|
1 2 3 4 5 |
|
1 2 3 4 5 6 7 8 9 10 11 |
|
1 2 3 4 5 6 |
|
1 ) has_key(key) Determine whether the key is in the dictionary
2) copy() returns a copy of the dictionary (the copy is a shallow copy)
1 2 3 4 5 |
|
If you want to deep copy, you need to use copy.deepcopy(a)
1 2 3 4 5 6 |
|
3) clear() clear dict
4) update(d) uses one dictionary to update another dictionary, which is somewhat similar to merging two dictionaries
1 2 3 4 5 6 |
|
There are many ways to traverse a dictionary
1. Directly use dict1 2 3 4 5 6 7 8 |
|
1 2 3 4 5 6 |
|
##Of course you can also do this
1 2 3 4 5 6 |
|
3. iteritems()
(I think it’s a better method)
1 2 3 4 5 6 |
|
1. One-click multi-value
Generally, dictionaries are mapped one-to-one, but if we need one-to-many mapping, such as a book, we need to count the number of pages where some words appear. Then, you can use list as the value of dict. This can be accomplished by using the setdefault() method
1 2 3 4 5 6 7 8 9 10 |
|
当然,如果写成一个函数话,就可以更方便的使用,
我们也可以利用set来代替list
1 2 3 4 5 6 7 8 9 |
|
2、利用字典完成简单工厂模式
字典的value不单单只是一些常见的字符串,数值,还可以是类和方法,比如我们就可以这样来实现简单工厂模式
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
|
另外一个例子,利用变量来控制执行的函数
1 2 3 4 5 6 7 8 9 10 |
|
更多Python的dict字典结构操作方法相关文章请关注PHP中文网!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

When using Python's pandas library, how to copy whole columns between two DataFrames with different structures is a common problem. Suppose we have two Dats...

Alternative usage of Python parameter annotations In Python programming, parameter annotations are a very useful function that can help developers better understand and use functions...

How do Python scripts clear output to cursor position at a specific location? When writing Python scripts, it is common to clear the previous output to the cursor position...

Why can't my code get the data returned by the API? In programming, we often encounter the problem of returning null values when API calls, which is not only confusing...

How does Uvicorn continuously listen for HTTP requests? Uvicorn is a lightweight web server based on ASGI. One of its core functions is to listen for HTTP requests and proceed...

In Python, how to dynamically create an object through a string and call its methods? This is a common programming requirement, especially if it needs to be configured or run...

How to use Go or Rust to call Python scripts to achieve true parallel execution? Recently I've been using Python...

Python binary library (.whl) download method explores the difficulties many Python developers encounter when installing certain libraries on Windows systems. A common solution...
