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 implement the bottom layer of the python collection

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

Share

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

This article will explain in detail how to implement the bottom layer of the python collection. The editor thinks it is very practical, so I share it for you as a reference. I hope you can get something after reading this article.

1. The underlying implementation of the collection type is based on the hash table, and the output order of the keys depends on the order in which the keys are stored in the hash table.

2. The data types of keys in the collection must be static data types, such as simple data types, strings, and tuples.

The collection type is the same as the dictionary type and is a data structure based on a hash table.

Example

#-*-coding: utf-8-*-"" @ Time: 2021-8-15 19:20@Author: LYP@FileName: set_python.py@SoftWare: PyCharm "" print ("creating a collection") a = {1, (1, 2, 3), 'print (a) set1 = set ("c.biancheng.net") set2 = set ([1, 2, 3, 4]) set3 = set 5) print ("set1:", set1) print ("set2:", set2) print ("set3:", set3) print ("common operations between collections") set4=set ([1, 2, 4, 4, 5, 6, 7]) set5=set ([6, 7, 8, 8, 9, 12, 23, 45, 67]) print (set4) print (set5) print ("set4 adds element 10", set4.add (10) print ("add element 10 to set5", set5.add (10) print ("remove elements from set4") Set4.pop () # discard () Delete specific elements print ("remove elements in set5", set5.discard (6)) print ("remove elements 10 from set4", set4.remove (10)) print ("remove elements from set5", set5.remove (10)) print ("Collection Common Operations") print ("intersection of set4 and set5", set4 & set5) print ("Union of set4 and set5", set4 | set5) print ("difference between set4 and set5" Set4-set5) print ("symmetric difference of set4 and set5", set4 ^ set5) print ("Union of two sets", set4.union (set5)) print ("difference of multiple sets", set4.difference (set5)) print ("intersection of sets", set4.intersection (set5)) print ("determine whether two sets contain the same element" ", set4.isdisjoint (set5)) print (" judging whether a collection has an element, ", 7 in set5) this is the end of the article on" how to implement the underlying python collection ". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please 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