python3 集合set

'''
set集合是python的一个基本数据类型,一般不是很常用,set中的元素是不重复的,无序的。里面的元素必须是可hash的(int, str,
tuple, bool),我们可以这样来记,set就是dict类型的数据但是不保存value,值保存key,set也用{}表示
'''
set1 = {1, 2, "apple", True, ("aa", "bb")}
print(set1)  # {('aa', 'bb'), 1, 2, 'apple'}

# set2 = {'1', 'apple', 2, True, [1, 2, 3]}  # 报错 TypeError: unhashable type: 'list'
# set3 = {'1', 'apple', 2, True, {1: 2}}  # 报错 TypeError: unhashable type: 'dict'
set4 = {'1', 'apple', 2, True, (1, 2, [2, 3, 4])}  # 报错 TypeError: unhashable type: 'list'
{1, 2, ('aa', 'bb'), 'apple'}
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-35-bf02b8b227c0> in <module>()
      8 # set2 = {'1', 'apple', 2, True, [1, 2, 3]}  # 报错 TypeError: unhashable type: 'list'
      9 # set3 = {'1', 'apple', 2, True, {1: 2}}  # 报错 TypeError: unhashable type: 'dict'
---> 10 set4 = {'1', 'apple', 2, True, (1, 2, [2, 3, 4])}  # 报错 TypeError: unhashable type: 'list'

TypeError: unhashable type: 'list'
'''set集合
无序,不重复,元素必须是不可变的,元素必须是可哈希的数据类型,如:int,bool,str,tuple
list,dict,set都不可以
'''
s = {2, "a", True, (3, 4)}
print(s)  # {True, 2, 'a', (3, 4)}
s1 = {{1, 3}, {4, 6}}  # TypeError: unhashable type: 'set'
print(s1)
print(s)  # {1, 'a', (3, 4)}
s2 = {[1, 4, 2], "a"}  # # TypeError: unhashable type: 'set'
print(s2)
{True, 2, 'a', (3, 4)}
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-8-dae596e5f0ad> in <module>()
      5 s = {2, "a", True, (3, 4)}
      6 print(s)
----> 7 s1 = {{1, 3}, {4, 6}}  # TypeError: unhashable type: 'set'
      8 print(s1)
      9 print(s)  # {1, 'a', (3, 4)}

TypeError: unhashable type: 'set'


'''定义一个空集合'''
s = set()
print(type(s)) 
s1 = {}
print(type(s1))
<class 'set'>
<class 'dict'>

'''集合中只要一个元素的情况,必须加逗号'''
s = {1,}
print(type(s))
<class 'set'>

'''集合中包括True和1的情况,哪个在前,保留哪个'''
s = {1, True}
print(s)  # {1}
s1 = {True, 1}
print(s1)  # {True}
{1}
{True}

'''集合中包括False和0的情况,哪个在前,保留哪个'''
s = {0, False}
print(s)  # {0}
s = {False, 0}
print(s)  # {False}
{0}
{False}

'''add给集合添加元素'''
s = {1, "a"}
s.add("b")
print(s)  # {1, 'b', 'a'}
{1, 'b', 'a'}

'''discard删除集合中的元素'''
s = {1, "a", False}
s.discard("a")
print(s)  # {False, 1}
{False, 1}

'''update批量添加集合元素'''
s = {1, "a"}
s2 = {"b", False, (1, 2)}
s3 = s.update(s2)
print(s)  # {False, 1, 'b', (1, 2), 'a'}
{False, 1, 'b', (1, 2), 'a'


'''
使用这个特性,我们可以使用set来去掉重复
'''
lst = ["apple", "banana", "orange", "banana", "banana"]
lst = list(set(lst))
print(lst)  # ['apple', 'banana', 'orange']
['orange', 'apple', 'banana']

'''集合取并集、交集、差集和对称差集
intersection 交集
union 并集
difference 差集
symmetric_difference 对称差集
& 交集
| 并集
- 差集
'''
# &或者intersection取交集
s = {"a", "b", "c"}
s2 = {"b", "c", "d"}
print(s & s2)
print(s.intersection(s2))  
{'b', 'c'}
{'b', 'c'}

# |或者union取并集
s = {"a", "b", "c"}
s2 = {"b", "c", "d"}
print(s | s2)  
print(s.union(s2)) 
{'b', 'd', 'c', 'a'}
{'b', 'd', 'c', 'a'}

# -或者difference取差集
s = {"a", "b", "c"}
s2 = {"b", "c", "d"}
print(s - s2) 
print(s.difference(s2)) 
print(s2 - s) 
print(s2.difference(s)) 
{'a'}
{'a'}
{'d'}
{'d'}

# symmetric_difference取对称差集
s = {"a", "b", "c"}
s2 = {"b", "c", "d"}
print(s.symmetric_difference(s2)) 
{'d', 'a'}
原文地址:https://www.cnblogs.com/lilyxiaoyy/p/11858139.html