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

What are the construction methods of Python variable sets and immutable sets

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

Share

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

This article mainly introduces the construction methods of Python variable sets and immutable sets, which have a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let Xiaobian take you to understand.

Python set is a new data type. There are two forms of set: mutable set (set ()) and immutable set (frozenset ()). The operation methods of these two sets are similar, but there are distinct differences in the underlying properties. A set is a disordered, non-repeatable and inaccessible set of elements, which is similar to sets in mathematics in concept and operation. Sets are divided into mutable and immutable.

First, compare data types

Here are some of the data types we have learned. the comments below are the results of comparing these data types for reference before learning the collection.

Str1 = 'pythonpython' # immutable, ordered: can be accessed by subscript list1 = [1, 2, 3, 2] # variable, ordered: can be accessed by subscript tup1 = (1, 2, 3, 2) # immutable, ordered: dict1 = {' name': 'Tom',' age': 18, 'love':' python'} # variable Unordered: but you can access the variable set construction method through the key.

1. Direct construction

Set2 = {'name', 19,' python'} print (set2, type (set2))

Return the result:

{19, 'python',' name'}

two。 Use function construction

Str1 = 'pythonpython'list1 = [1,2,3,2] tup1 = (1,2,3,2) dict1 = {' name': 'Tom',' age': 18, 'love':' python'} set3 = set (str1) print (set3, type (set3)) set4 = set (list1) print (set4, type (set4) set5 = set (tup1) print (set5, type (set5)) set6 = set (dict1) print (set6, type (type))

Return the result:

{'tween,' nasty, 'paired,' oiled, 'hacked,' y'}

{1, 2, 3}

{1, 2, 3}

{'love',' name', 'age'}

3. Construct a set using deductions

Set7 = set (i for i in range (1,5)) print (set7, type (set7)) set8 = {i for i in list1} print (set8, type (set8)) set8 = {i for i in tup1} print (set8, type (set8))

Return the result:

{1, 2, 3, 4}

{1, 2, 3}

{1, 2, 3}

Third, the construction method of immutable sets.

Immutable set construction (similar to mutable sets, just change set to frozenset).

1. Using the frozenset () function to construct

Set3 = frozenset (str1) print (set3, type (set3)) set4 = frozenset (list1) print (set4, type (set4)) set5 = frozenset (tup1) print (set5, type (set5)) set6 = frozenset (dict1) print (set6, type (set6) frozenset ({'packs,' nails, 'tweets,' hacks, 'yearly,' o'}) frozenset ({1,2,3}) frozenset ({1,2,3}) frozenset ({name', 'age', 'love'})

two。 Derivation construction

Set7 = frozenset (i for i in range (1,5)) print (set7, type (set7))

Return the result:

Frozenset ({1,2,3,4})

IV. matters needing attention in set construction

1. The collection cannot be constructed with unique symbols like other data sets. The syntax symbol used by the collection is {}, which is the same as the dictionary. In this case, {} is directly used to construct the collection. The system cannot determine whether the data type is a dictionary or a collection. Will default to the collection.

Set9 = {} print (type (set9)) # defaults to a dictionary:

The correct way to do this is to use a constructor.

Set9 = set () set99 = frozenset ()

two。 A collection cannot contain variable type elements such as dictionaries and lists

Set10 = {'name', 19, [1,2,3,2]}

List cannot be hashed: TypeError: unhashable type: 'list'

Thank you for reading this article carefully. I hope the article "what are the construction methods of Python variable sets and immutable sets" shared by the editor will be helpful to you. At the same time, I also hope you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

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