Get the App
SLTechnology News&Howtos  ›  Development  › 

How to implement the bottom layer of the python collection

Shulou Source: shulou.com Published: 2022-06-03 12:11:06 10月01日 Update

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.

Tags: Element type data underlying subtraction article hash same two intersection common more order good practical symmetrical between content depends on multiple Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno macOS Shulou Information Shulou Tech Info NVidia Microsoft