Python data container: set (set)

my_list=['黑马程序员', '传智播客', "黑马程序员", '传智播客', 'itheima', 'itcast',
'itheima', 'itcas', 'best']
New_set = set()
for element in my_list:
    New_set.add(element)
print(f"最终得到的集合为:{New_set}")
# 输出结果:最终得到的集合为:{'itcas', 'best', 'itheima', 'itcast', '黑马程序员', '传智播客'}

1. The significance of the existence of sets (why do we need sets?)

Analyze by characteristics:
Lists can be modified, support repeated elements, and are ordered; tuples and strings cannot be modified, support repeated elements, and are ordered.

The above data containers all support repeated elements, but if the scenario requires deduplication of content, lists, tuples, and strings are inconvenient.

The main feature of a collection is that it does not support duplication of elements (even if the input elements are repeated, it will have its own deduplication function) and the content is unordered. If you define a set with duplicate internal elements and output it, you will find that it has been automatically deduplicated.

2. Definition of set

Define collection variable variable name = {element 1, element 2, element 3...}

Define an empty set variable name = set(())

List: []
Tuple: ()
String: ""
Collection: {}

 3. Sets are not sequences

When saying that a set is not a sequence, it means that a set has different properties and behavior than a sequence. A sequence is a data structure containing ordered elements, such as lists, tuples, and strings. Sequences have the following properties:

        1. Orderliness: The elements in the sequence are arranged in a certain order, and the elements can be accessed through indexes.
        2. Indexability: Indexes can be used to access elements at specified positions.
        3. Slicability: Subsequences of a sequence can be obtained through slicing operations.
        4. Iterability: You can use iterators to access elements in a sequence one by one.
        5. Element repetition: Sequences are allowed to contain repeated elements.

In contrast, a set is an unordered data structure that does not contain duplicate elements. Collections have the following properties:

        1. Disorder: The elements in the collection have no fixed order, and elements cannot be accessed through indexes.
        2. Non-indexability: The elements in the collection cannot be accessed through indexing because the collection does not define the order of the elements.
        3. Unslicability: Collections do not support slicing operations because the range of subcollections cannot be defined due to disorder.
        4. Non-iterability: Collections are not iterable objects and cannot use iterators to access elements one by one.
        5. Uniqueness: The elements in the set are unique and duplicate elements are not allowed.

In summary, while sets are similar to sequences in some ways, they have clear differences in behavior and properties.

 4. Collection of common operations

operate grammar result
subscript index access Collection does not support this operation Collections do not support this operation (because collections are unordered and collections are not sequences)
Traverse the collection Does not support while loop, but supports for loop Get the elements in the collection
Add new element collection.add() The collection itself is modified, new elements are added
Remove element
(remove the specified element from the collection)
Collection.remove(element) The collection itself is modified, elements are removed
Randomly remove an element from the set collection.pop()

will get the result of one element

At the same time the collection itself is modified and elements are removed

Clear collection set.clear() Collection is cleared
Get the difference between two sets
(only sets have this operation)
Set3 = Set1.difference(set2)

Take the difference set between set 1 and set 2 (set 1 has it but set 2 does not)

Get a new set, set 1 and set 2 unchanged

Eliminate the difference between two sets set 3 = set 1.difference _update(set 2)

Set 1 is modified and set 2 remains unchanged.
The same elements in set 1 as set 2 are deleted.

Set 3 is set 1

2 sets merged

Combine set 1 and set 2 into a new set

set3 = set1.unior(set2) Get a new set,  set 1 and set 2 remain unchanged
Find the length of the set len (set) Get the length of the set

Sample code:
 

my_set = {"1", "2", "3", "4", "5", "1", "2", "3", "4"}
my_set_empty = set()

print(my_set)
print(my_set_empty)

"""
    输出结果:
    {'4', '2', '1', '3', '5'} 
    set()
"""

# 集合添加元素 add()
my_set = {"1", "2", "3", "4", "5", "1", "2", "3", "4"}
my_set.add("Python")
print(f"增加元素后的集合为{my_set}")
# python 的位置是不固定的。
# 输出:增加元素后的集合为{'4', '5', '1', 'Python', '2', '3'}

# 集合移除元素 remove()
my_set.remove("1")
print(f"移除元素后的集合为{my_set}")
# 输出:移除元素后的集合为{'4', '5', 'Python', '2', '3'}

# 集合随机移除一个元素
element = my_set.pop() #element是被移除的元素
print(f"随机移除一个元素后,集合为{my_set}")
print(f"被随机移除的元素是:{element}")
"""
    输出结果:
    随机移除一个元素后,集合为{'Python', '5', '4', '2'}
    被随机移除的元素是:3 
"""

# 集合的差集
set1 = {"1", "2", "3", "4", "5", "Hello", "World"}
set2 = {"1", "2", "3", "4", "5"}
set3 = set1.difference(set2)
print(f"集合1与集合2的差集为{set3}")
# 输出:集合1与集合2的差集为{'World', 'Hello'}

set4 = {"1", "2", "3", "4", "5"}
set5 = {"1", "2", "3", "4", "5", "Hello", "World"}
set6 = set4.difference(set5)
print(f"集合5与集合6的差集为{set6}")
# 输出:集合5与集合6的差集为set() (表明差集是空集)

# 消除2个集合的差集
set1.difference_update(set2)
print(f"消除2个集合的差集后的集合1为:{set1}")
# 输出:消除2个集合的差集后的集合1为:{'World', 'Hello'}

# 集合合并
print(f"合并前的set1为{set1},合并前的my_set为{my_set}")
set7 = set1.union(my_set)
print(f"合并后的集合为{set7}")
'''
    输出:
    合并前的set1为{'World', 'Hello'},合并前的my_set为{'2', 'Python', '4', '5'}
    合并后的集合为{'Python', 'World', 'Hello', '2', '4', '5'}
'''

# 遍历集合
print(f"my_set中的元素分别为", end=":  ")
for element in my_set:
    print(element,end="  ")
# 输出:my_set中的元素分别为:  Python  3  4  5

5. Code practice - information deduplication

There are the following list objects:
my_list=['Dark Horse Programmer', 'Chuanzhi Podcast', "Dark Horse Programmer", 'Chuanzhi Podcast', 'itheima', 'itcast', 'itheima', 'itcas', 'best ']
Please:
1. Define an empty collection
2. Traverse the list through a for loop
3. Add the elements of the list to the collection in the for loop
4. Finally get the collection object after deduplicating the elements and print it out

my_list=['黑马程序员', '传智播客', "黑马程序员", '传智播客',    'itheima',
  'itcast','itheima', 'itcas', 'best']
New_set = set()

# 其实这一步不用for循环,直接转换就可以实现
for element in my_list:
    New_set.add(element)
print(f"最终得到的集合为:{New_set}")
"""
输出结果:最终得到的集合为:{'itcas', 'best', 'itheima', 'itcast', 
'黑马程序员', '传智播客'}
""" 

Guess you like

Origin blog.csdn.net/weixin_48060069/article/details/132357693