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

Case Analysis of Set and dict in Python

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

Share

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

This article mainly explains "case analysis of Set and dict in Python". The content of the explanation is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "case analysis of Set and dict in Python".

I. Set collection type

Set set type (intersection and complement) characteristics: disorder, automatic deduplication

Collections are represented by {} and elements are separated by commas

Create a collection type with {} or set ()

To establish an empty collection type, you must use set

Define a common collection

Setvar = {"shy", "broiler", "gala", "Little Tiger", "Langya Mountain five strong Warriors"} print (setvar, type (setvar)) / / printed out and arranged randomly, can you get the elements out of order? No. Can print (setvar [0]) / / error modify the elements in it? No setvar [0] = "hee hee" / / error setvar = {"shy", "broiler", "gala", "tiger", "Yashan", "shy"} print (setvar)

Define an empty collection

Setvar = {} # dictionary / / {} setvar = set () # empty collection / / srt () print (setvar, type (setvar))

Dict dictionary type key value for the stored data, can be obtained, can be modified on the surface orderly, the actual storage is out of order

Completely disordered before python3.5, superficially ordered after python3.5, and disordered at storage time

Syntax: {key 1: value 1, key 2: value 2, key 3: value 3. }

Mapping is a correspondence between a key (index) and a value (data)

"streetAddr": "55 Zhongguancun South Street"city": "Beijing"

Define an empty dictionary

Dictvar = {} print (dictvar, type (dictvar))

Define a general dictionary

Dictvar = {"top": "shy", "middle": "Broiler", "bottom": "gala", "support": "Xiaoming", "jungle": "wei"} print (dictvar)

Get the data in the dictionary

Res = dictvar ["support"] res = dictvar ["jungle"] print (res)

Modify the data in the dictionary

Dictvar ["bottom"] = "jacklove" print (dictvar) II. Data type restrictions for set and dict

Dictionary keys and collection values have data type restrictions on the use of the following types: (immutable data types hashable data types)

Number (int float bool complex), str, tuple

Variable data: set dict list

The key of the dictionary is unique, and the value of the dictionary has no restriction on the data type.

Dictionary:

Dictvar = {1:1, "22222": 2,3.14 print 4,5-90j:5, False:999, (1 print, type (dictvar))

Collection:

Setvar = {1pc3.4, "sd", (1j2p3), 4-10j dint false} / / if it is True, it will be deduplicated print (setvar)

Hash algorithm:

In order to distribute the data to memory more evenly, the hash algorithm is used to reduce hash collisions. Faster storage of data

The hash algorithm requires that the data type can only be immutable data (Number str tuple), also called hash data type.

The data stored in the hash algorithm is unordered, so dictionaries and collections are out of order.

Thank you for reading, the above is the content of "case Analysis of Set and dict in Python". After the study of this article, I believe you have a deeper understanding of the case analysis of Set and dict in Python, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

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