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

A detailed introduction to the set collection in Python

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

Share

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

This article mainly explains the "detailed introduction of the set collection in Python". The content in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "the detailed introduction of the set collection in Python".

Catalogue

Set collection in Python

First, what is the assembly?

Second, how to use the set collection?

1. Create a set collection

2. Delete the set collection

3. Access set collection elements

4. Delete elements in the collection

5. Add elements to the collection

3. Intersection and union complement of set sets

1. Intersection

2. Union

3. Difference set

4. Other methods in set

5. Frozenset set

What are the set sets and collections in Python?

What is the collection? I believe that readers, even if they have not used the data type of collection. Must have heard the term set in math class. Set in mathematics is a basic concept, which makes it clear that a bunch of non-repetitive numbers can form a set. Go back to the data type of the collection in Python, which can be used to hold multiple data that are not duplicated, and the data type of that data is immutable. These immutable types include numbers, strings, and tuples. In other words, variable data types such as lists and dictionaries cannot be used as elements of the collection.

There are two types of collections in Python, one of type set and the other of type fronzenset. The difference between these two types of collections is that the set collection is variable, that is, elements within the collection can be added and deleted, while the fronzenset collection is immutable. That is, you cannot add or delete elements within the collection.

The definition of the set collection is {element1,element2,...,elementn}, where element1~elementn represents an unlimited number of elements in the collection. Each element is unique.

Second, how to use the set collection? 1. Create a set collection

There are two ways to create set collections:

The first is created by {}, and its syntax format is {element1,element2,...,elementn}.

Second: through the set function, its syntax format is: set (iterable) in which if iterable is not passed, an empty set object will be created. The incoming iterable must be a sequence that can be traversed, such as strings, lists, and so on. For example:

Set_demo = {'1test', 2, (' programmer', 'Brother Fei')} print (set_demo) list = [1, 'test', 1, (' programmer', 'Brother Fei')] print ('list list result =', list) set_demo1 = set (list) print ('generated set result =', set_demo1)

The result of the operation is:

{2, ('programmer', 'Brother Fei'),'1'} list list result = [1, 'test', 1, (' programmer', 'Brother Fei')] generated set result = {1, ('programmer', 'Brother Fei'), 'test'}

You can see that even if there are two 1 elements in the list list, there is still only one 1 element after the set collection is generated, which means that elements with the same value in the collection will only be stored once.

2. Delete the set collection

Deleting the set collection can be deleted through the del (setname) function, where setname is the name of the set collection to be deleted.

3. Access set collection elements

Because the elements in the collection are unordered, five spices use subscripts to access elements like lists. In Python, the most common way to access collection elements is to use a loop to read out the elements in the collection one by one.

Set_demo = {'1mm, 2, (' programmer', 'Brother Fei')} for value in set_demo: print (value)

The result of the operation is:

2 ('programmer', 'Brother Fei') 1

The output of the element is also unordered.

4. Delete elements in the collection

There are two ways to delete elements in a collection:

First: through the remove () method, its syntax structure is: setname.remove (element), where setname is the set collection to be operated, element is the element to be deleted, and KeyError error will be reported if deletion fails.

The second: through the discard () method, its use is the same as the remove () method, except that it will not report an error if the deletion of the element fails.

Set_demo = {'1marker, 2, (' programmer', 'Flying Brother')} set_demo.remove ('1') print ('set after deleting element 1 =', set_demo) set_demo.discard ('5') print ('set after deleting element 5 =', set_demo)

The result of the operation is:

Collection after deleting element 1 = {2, ('programmer', 'Brother Fei')} set after deleting element 5 = {2, ('programmer', 'Brother Fei')} 5, add elements to the collection

Through the add function to add elements to the collection, its syntax structure is: setname.add (element), in which setname is the set collection to be operated, element is the element to be added, only immutable elements can be added, such as numbers, tuples or strings, can not add lists, dictionaries, set collections and other variable data.

For example:

Set_demo.add ('6') print ('the result after adding element 6 is =', set_demo)

The result of the operation is:

The result after adding element 6 is = {2, '6percent, (' programmer', 'Brother Fei')} 3. Intersection and complement of set sets.

In mathematics, sets have intersection, union and complement. Collections in Python also have these concepts.

1. Intersection

By & concatenating two set collections, you can generate a new collection, which takes the common elements of the two original collections.

2. Union

A new collection can be generated by connecting two set collections via |. The new collection takes all the elements of the two original collections.

3. Difference set

By concatenating two set collections, you can generate a new collection that takes elements that exist in one collection but not in the other.

Take a chestnut:

Set_demo = {1,3,5,6} set_demo1 = {2,4,6} print ("result of intersection =", set_demo & set_demo1) print ("result of union =", set_demo | set_demo1) print ("result of difference set =", set_demo-set_demo1)

The result of running is

Take the result of intersection = {6} the result of union = {1, 2, 3, 4, 5, 6} the result of difference = {1, 3, 5} 4, other methods in set

You can view all the methods in the set collection through dir (set). There are the following ways:

['_ _ and__','_ _ class__','_ _ contains__','_ _ delattr__','_ _ dir__','_ _ doc__','_ _ eq__','_ _ format__','_ _ ge__','_ _ getattribute__','_ gt__','_ hash__','_ _ iand__','_ _ init__' '_ _ init_subclass__',' _ _ ior__','_ _ isub__','_ _ iter__','_ _ ixor__','_ _ le__','_ _ len__','_ _ lt__','_ _ ne__','_ _ new__','_ or__','_ rand__','_ reduce__','_ _ reduce_ex__' '_ _ repr__',' _ _ ror__','_ _ rsub__','_ _ rxor__','_ _ setattr__','_ _ sizeof__','_ _ str__','_ _ sub__','_ subclasshook__','_ _ xor__', 'add',' clear', 'copy',' difference', 'difference_update',' discard', 'intersection' 'intersection_update', 'isdisjoint',' issubset', 'issuperset',' pop', 'remove',' symmetric_difference', 'symmetric_difference_update',' union', 'update']

The naming of the method is relatively standard, and the function of the method can be guessed according to the English name, so I will not repeat the meaning of each method here. Give a direct example:

Set_demo = {1,3,5,6} set_demo1 = {2,4,6} set_demo.update ({12, (2,3)}) print ('after set_demo calls update method', set_demo) set_demo = {1,3,5,6} print ('after set_demo calls union method', set_demo.union (set_demo1)) print ('after set_demo calls difference method' Set_demo.difference (set_demo1)) print ('after set_demo calls the _ _ sub__ method', set_demo.__sub__ (set_demo1))

The result of the operation is:

After set_demo calls update method {1, 3, (2, 3), 5, 6, 12} set_demo calls union method, {1, 2, 3, 4, 5, 6} set_demo calls difference method, {1, 3, 5} set_demo calls _ sub__ method, {1, 3, 5} 5, frozenset collection

The frozenset collection is the immutable version of the set collection. Compared with the set collection, the elements in the frozenset collection are immutable, that is, there is no way to add elements and delete elements.

The way to create a frozenset collection is through the frozenset () method. Its syntax format is: frozenset (iterable) where if iterable is not passed, an empty set object will be created. The incoming iterable must be a sequence that can be traversed, such as strings, lists, and so on.

Frozenset_demo = frozenset (list) print ("Type of frozenset_demo is =", type (frozenset_demo)) print ("result of frozenset_demo =", frozenset_demo)

The result of the operation is:

The type of frozenset_demo is = frozenset_demo results = frozenset ({('programmer', 'Feige'), 1, 'test'}) Thank you for your reading, the above is the "detailed introduction of the set collection in Python", after the study of this article, I believe you have a deeper understanding of the detailed introduction of the set collection 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