Methods for defining sets in Python [Daquan]

Posted by nova on Sun, 05 Dec 2021 04:57:21 +0100

Python collection is a new data type. There are two forms of collection: variable collection set() and immutable collection frozenset(). These two collection operation methods are similar, but there are obvious differences in the underlying properties. Set is an unordered, non repetitive and non randomly accessible set of elements. It is similar to the set in Mathematics in concept and operation. Set is divided into variable and immutable.

1, Comparison data type

The following are some data types we have learned. The following comments are the results of comparing these data types for reference before learning the set.

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: can be accessed by subscript
dict1 = {'name': 'Tom', 'age': 18, 'love': 'python'} # Variable, unordered: but can be accessed through keys

2, Variable set construction method

1. Direct construction

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

Return result:

{19, 'python', 'name'} <class 'set'>

2. 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(set6))

Return result:

{'t', 'n', 'p', 'o', 'h', 'y'} <class 'set'>
{1, 2, 3} <class 'set'>
{1, 2, 3} <class 'set'>
{'love', 'name', 'age'} <class 'set'>

3. Construct a set using a derivation

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 result:

{1, 2, 3, 4} <class 'set'>
{1, 2, 3} <class 'set'>
{1, 2, 3} <class 'set'>

3, Construction method of immutable set

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

1. Use 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))

Return result:

frozenset({'p', 'n', 't', 'h', 'y', 'o'}) <class 'frozenset'>
frozenset({1, 2, 3}) <class 'frozenset'>
frozenset({1, 2, 3}) <class 'frozenset'>
frozenset({'name', 'age', 'love'}) <class 'frozenset'>

2. Derivation structure

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

Return result:

frozenset({1, 2, 3, 4}) <class 'frozenset'>

4, Collection construction considerations

1. The set cannot be constructed with special symbols like other data sets. The syntax symbol used by the set is {}, which is the same as that of the dictionary. At this time, it is constructed directly with {}. The system cannot judge whether the data type is a dictionary or a set, and will default to the set.

set9 = {}
print(type(set9)) # The default is Dictionary: < class' dict '>

The correct method can only be implemented using a constructor.

set9 = set()
set99 = frozenset()

2. The collection cannot contain variable type elements such as dictionary and list

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

List is not hashable: TypeError: unhashable type: 'list'

The above is Construction method of variable set and immutable set Of course, there are supporting videos to explain. Maybe novices will better absorb and digest the videos. The videos are on the python self-study network (www.wakey.com.cn). Those who are interested can go and have a look.

Topics: Python