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

How to parse Dict in Python

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article will explain in detail how to parse the Dict in Python. The content of the article is of high quality, so the editor shares it for you as a reference. I hope you will have some understanding of the relevant knowledge after reading this article.

What is dict?

The full name of dict is dictionary (dictionary). People, like dictionaries, can locate specific words according to the index.

In python's dict, we can navigate to a specific value through key.

For a pair like key vs value, key3 is also a dict when it is nesting inside.

Over and over again, a dict can become many levels, but the features are still matched one by one by key value, but value can be either a normal type or a dict.

As I said earlier, list is a series of data, and the only way to find elements is through the subscript to look for them one by one. From a certain point of view, list is a dictionary with subscript key. Of course, strictly speaking, list and dict are two completely different data types.

Having said that, let's take a look at the dictionary code example:

Dict1 = dict () dict1 ['location'] =' Guangdong'dict2 = {'name':' Reixue Committee', 'langs': [' Java', 'Scala',' NodeJs', 'Python']}

Dict1 and dict2 in the code are both dictionaries.

Let's take a look at the addition, deletion, check and modification of dict.

Similar to list, the elements in dict are said to be mutable.

We can directly use code similar to the following to reassign the value of a particular key.

Dict ['key1'] = new_value # reassigns the key 1 of the dict object.

Let's look directly at the code and go through the operations of adding / deleting / changing elements and emptying the dictionary.

#! / usr/bin/env python#-*-coding: utf-8-*-# @ Time: 11:34 on 2021-11-3 # @ Author: LeiXueWei# @ CSDN/Juejin/Wechat: Ray academic Committee # @ XueWeiTag: CodingDemo# @ File: _ _ init__.py.py# @ Project: hello# as a dictionary info_dict = dict () print ('info_dict:') Info_dict) # add elements to the information dictionary info_dict ['name'] =' Lei Xue Commission 'print (' info_dict:', info_dict) # updated to the English name info_dict ['name'] =' LEI_XUE_WEI' # or changed to 'levin'info_dict [' blogs'] = '93' # 93 blog posts have been published. Info_dict ['gongzhong_hao'] =' [Ray Committee]'# Welcome to follow the support for print ('info_dict:', info_dict) print (' gongzhong_hao:', info_dict ['gongzhong_hao']) del info_dict [' blogs'] # Delete element print ('info_dict:', info_dict) info_dict.clear () print (' info_dict:') Info_dict) # clear after emptying the element can print dictdel info_dict # delete the entire information dictionary directly # print ('info_dict:', info_dict) can no longer print

The effect is as follows:

In particular, in order to keep the program uninterrupted, we usually write code like this when we get an unfamiliar dict:

# We also often use this method to get the value of a key, so that the program will not be interrupted abnormally gongzhonghao = info_dict ['gongzhong_hao'] if' gongzhong_hao' in info_dict else 'N/A'print (' gongzhong_hao:', gongzhonghao)

Dict is very convenient to use, and it is very easy to locate the value based on a key.

We often use this structure in our development, whether it is Python/Java, almost all developers will use the dictionary type.

On how to parse the Dict in Python to share here, I hope the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.

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