set集合只能通过关键字“set()"进行创建,如果是直接通过set()方法创建,里面的字符串中的每个字符都将成为集合中单独的元素。
CreateSet = set("Tesla")
print(CreateSet)
#以下为输出的内容
"""
{'s', 'a', 'T', 'e', 'l'}
"""
可以看到上面的输出,输出为一个集合
CreateList = list(["Tesla","Elon Mask"])
CreateSet = set(CreateList)
print(CreateSet)
print(type(CreateList))
print(type(CreateSet))
"""
{'Tesla', 'Elon Mask'}
"""
CreateList = list(["Tesla","Elon Mask",[1,2]])
CreateSet = set(CreateList)
print(CreateSet)
print(type(CreateList))
print(type(CreateSet))
"""
TypeError: unhashable type: 'list'
"""
在前言中有描述,列表是可变的数据类型,所以不可哈希,所以不能转换成集合。
可添加删除元素,但是集合的整体对象是不可哈希的,所以不能当作 字典的键,也不能做其它集合的元素
与可变集合相反
集合与字典一样,是一个无序的序列,是通过哈希后进行存储的,所以没有下标,不能创建索引或者切片操作,但可以使用for循环,in,not in 来访问或者判断集合的元素
返回为布尔类型、
CreateList = list(["Tesla","Elon Mask",1,2])
CreateSet = set(CreateList)
for i in CreateSet:
print(i)
"""
Elon Mask
2
Tesla
1
"""
print(2 in CreateSet)
print(3 in CreateSet)
print("Tesla" in CreateSet)
"""
True
False
True
"""
CreateList = list(["Tesla","Elon Mask",1,2])
CreateSet = set(CreateList)
print(CreateSet)
CreateSet.add("Space X")
print(CreateSet)
"""
{'Elon Mask', 1, 2, 'Tesla'}
{1, 2, 'Tesla', 'Elon Mask', 'Space X'}
"""
但是如果是添加重复的元素,结果只会显示1个,如下所示,添加了3次重复的元素,但是结果只会有一个。
CreateList = list(["Tesla","Elon Mask",1,2])
CreateSet = set(CreateList)
print(CreateSet)
CreateSet.add("Space X")
CreateSet.add("Space X")
CreateSet.add("Space X")
print(CreateSet)
"""
{'Elon Mask', 1, 2, 'Tesla'}
{1, 2, 'Tesla', 'Elon Mask', 'Space X'}
"""
CreateList = list(["Tesla","Elon Mask",1,2])
CreateSet = set(CreateList)
print(CreateSet)
CreateSet.update("Solar")
CreateSet.update(["Solar System","Solar Roof","Tesla"])
print(CreateSet)
"""
{1, 2, 'Elon Mask', 'Tesla'}
{1, 2, 'Tesla', 'Solar System', 'o', 'a', 'r', 'Solar Roor', 'S', 'Elon Mask', 'l'}
"""
update是更新的意思,之前的还会存在,不会删除,新的内容增加进去,但是与add不一样的是,add是增加的无论多长当成一个元素,但是update会把字符串当成多个元素添加进去。但是可以使用列表,这样可以一次性添加多个元素,且可以把元素当成一个整体。如果有重复的就去重后再增加
CreateList = list(["Tesla","Elon Mask",1,2])
CreateSet = set(CreateList)
print(CreateSet)
CreateSet.remove("Tesla")
print(CreateSet)
"""
{1, 2, 'Elon Mask', 'Tesla'}
{1, 2, 'Elon Mask'}
"""
CreateList = list(["Tesla","Elon Mask",1,2])
CreateSet = set(CreateList)
print(CreateSet)
test = CreateSet.pop()
print(CreateSet)
print(test)
"""
{1, 2, 'Elon Mask', 'Tesla'}
{1, 2, 'Elon Mask'}
"""
pop()方法用于随机删除元素,括号内是不能跟参数的,跟上参数后会报错。
但是pop有返回值,所以可以使用一个变量进行接受弹出的值后可以打印出来,
CreateList = list(["Tesla","Elon Mask",1,2])
CreateSet = set(CreateList)
print(CreateSet)
CreateSet.clear()
print(CreateSet)
"""
{1, 2, 'Tesla', 'Elon Mask'}
set()
"""
清空,清空后就变成一个空集合了。
CreateList = list(["Tesla","Elon Mask",1,2])
CreateSet = set(CreateList)
print(CreateSet)
del CreateSet
print(CreateSet)
"""
NameError: name 'CreateSet' is not defined
{'Elon Mask', 2, 'Tesla', 1}
"""
直接删集合了。集合就不存在了
从集合中删除某个指定的元素,这个元素得是它的集合的成员,如果不是成员则不做任何事情。
"""
Remove an element from a set if it is a member.
If the element is not a member, do nothing.
"""
CreateList = list(["Tesla","Elon Mask",1,2])
CreateList1 = list(["Tesla","Elon Mask",1,2])
CreateList2 = list([1,2])
CreateList3 = list(["Tesla","Elon Mask","Space X"])
CreateSet = set(CreateList)
CreateSet1 = set(CreateList1)
CreateSet2 = set(CreateList2)
CreateSet3 = set(CreateList3)
CreateSet.discard(2)
print(CreateSet)
"""
{'Tesla', 1, 'Elon Mask'}
"""
CreateList = list(["Tesla","Elon Mask",1,2])
CreateSet = set(CreateList)
print(CreateSet)
print("Tesla" in CreateSet)
print("Space X" not in CreateSet)
print(CreateSet)
"""
{1, 2, 'Tesla', 'Elon Mask'}
True
True
{1, 2, 'Tesla', 'Elon Mask'}
"""
CreateList = list(["Tesla","Elon Mask",1,2])
CreateList1 = list(["Tesla","Elon Mask",1,2])
CreateList2 = list([1,2])
CreateList3 = list(["Tesla","Elon Mask"])
CreateSet = set(CreateList)
CreateSet1 = set(CreateList1)
CreateSet2 = set(CreateList2)
CreateSet3 = set(CreateList3)
print(CreateSet == CreateSet1)
print(CreateSet1 == CreateSet2)
print(CreateSet1 != CreateSet2)
"""
True
False
True
"""
如果两个集合是等价的则返回true
如果两个集合是不等价的就返回false
CreateList = list(["Tesla","Elon Mask",1,2])
CreateList1 = list(["Tesla","Elon Mask",1,2])
CreateList2 = list([1,2])
CreateList3 = list(["Tesla","Elon Mask"])
CreateSet = set(CreateList)
CreateSet1 = set(CreateList1)
CreateSet2 = set(CreateList2)
CreateSet3 = set(CreateList3)
print(CreateSet2.issubset(CreateSet))
print(CreateSet2 < CreateSet)
print(CreateSet1 < CreateSet2)
"""
True
True
False
"""
print(CreateSet2.issubset(CreateSet)) # CreateSet2是否是CreateSet的子集,是就返回Ture,不是就返回False
print(CreateSet2 < CreateSet) # < 与 issubset() 是相同的功能
print(CreateSet1 < CreateSet2) # 1与2是等价的,相等的,但是在Python中,并不是子集。
就是求合集,两个集合合并,相同的元素删除,不同的元素放在一起。
CreateList = list(["Tesla","Elon Mask",1,2])
CreateList1 = list(["Tesla","Elon Mask",1,2])
CreateList2 = list([1,2])
CreateList3 = list(["Tesla","Elon Mask","Space X"])
CreateSet = set(CreateList)
CreateSet1 = set(CreateList1)
CreateSet2 = set(CreateList2)
CreateSet3 = set(CreateList3)
print(CreateSet.union(CreateSet3))
print(CreateSet | CreateSet3)
"""
{1, 2, 'Tesla', 'Space X', 'Elon Mask'}
{1, 2, 'Tesla', 'Space X', 'Elon Mask'}
"""
CreateList = list(["Tesla","Elon Mask",1,2])
CreateList1 = list(["Tesla","Elon Mask",1,2])
CreateList2 = list([1,2])
CreateList3 = list(["Tesla","Elon Mask","Space X"])
CreateSet = set(CreateList)
CreateSet1 = set(CreateList1)
CreateSet2 = set(CreateList2)
CreateSet3 = set(CreateList3)
print(CreateSet.intersection(CreateSet3))
print(CreateSet & CreateSet3)
"""
{'Elon Mask', 'Tesla'}
{'Elon Mask', 'Tesla'}
"""
找出两个集合中相同的元素
CreateSet 与CreateSet2 先取交集,然后再更新给CreateSet,这个更新是只保留交集的元素
“”" Update a set with the intersection of itself and another. “”"
CreateList = list(["Tesla","Elon Mask",1,2])
CreateList1 = list(["Tesla","Elon Mask",1,2])
CreateList2 = list([1,2])
CreateList3 = list(["Tesla","Elon Mask","Space X"])
CreateSet = set(CreateList)
CreateSet1 = set(CreateList1)
CreateSet2 = set(CreateList2)
CreateSet3 = set(CreateList3)
CreateSet.intersection_update(CreateSet2)
# CreateSet 与CreateSet2 先取差集,然后再从CreateSet中Remove掉
print(CreateSet)
"""
{1, 2}
"""
跟数学书上讲的一样,A圈里有,B圈里没有的
CreateList = list(["Tesla","Elon Mask",1,2])
CreateList1 = list(["Tesla","Elon Mask",1,2])
CreateList2 = list([1,2])
CreateList3 = list(["Tesla","Elon Mask","Space X"])
CreateSet = set(CreateList)
CreateSet1 = set(CreateList1)
CreateSet2 = set(CreateList2)
CreateSet3 = set(CreateList3)
print(CreateSet.difference(CreateSet3))
print(CreateSet - CreateSet3)
print(CreateSet3.difference(CreateSet))
print(CreateSet3 - CreateSet)
"""
{1, 2}
{1, 2}
{'Space X'}
{'Space X'}
"""
CreateSet 与CreateSet2 先取差集,然后再从CreateSet中Remove掉
“”" Remove all elements of another set from this set. “”"
CreateList = list(["Tesla","Elon Mask",1,2])
CreateList1 = list(["Tesla","Elon Mask",1,2])
CreateList2 = list([1,2])
CreateList3 = list(["Tesla","Elon Mask","Space X"])
CreateSet = set(CreateList)
CreateSet1 = set(CreateList1)
CreateSet2 = set(CreateList2)
CreateSet3 = set(CreateList3)
CreateSet.difference_update(CreateSet2)
# CreateSet 与CreateSet2 先取差集,然后再从CreateSet中Remove掉
CreateSet1.difference_update(CreateSet3)
print(CreateSet)
print(CreateSet1)
"""
{'Tesla', 'Elon Mask'}
{1, 2}
"""
a.symmetric_difference(b) 对称差集,两边都互相没有的。反向交集。
CreateList = list(["Tesla","Elon Mask",1,2])
CreateList3 = list(["Tesla","Elon Mask","Space X"])
CreateSet = set(CreateList)
CreateSet3 = set(CreateList3)
print(CreateSet.symmetric_difference(CreateSet3))
print(CreateSet ^ CreateSet3)
"""
{1, 2, 'Space X'}
{1, 2, 'Space X'}
"""
A与B对称交集后再覆盖更新A
“”" Update a set with the symmetric difference of itself and another. “”"
CreateList = list(["Tesla","Elon Mask",1,2])
CreateList3 = list(["Tesla","Elon Mask","Space X"])
CreateSet = set(CreateList)
CreateSet3 = set(CreateList3)
CreateSet.symmetric_difference_update(CreateSet3)
print(CreateSet)
"""
{1, 2, 'Space X'}
"""
CreateList = list(["Tesla","Elon Mask",1,2])
CreateList1 = list(["Tesla","Elon Mask",1,2])
CreateList2 = list([1,2])
CreateList3 = list(["Tesla","Elon Mask","Space X"])
CreateSet = set(CreateList)
CreateSet1 = set(CreateList1)
CreateSet2 = set(CreateList2)
CreateSet3 = set(CreateList3)
print(CreateSet.issuperset(CreateSet2))
print(CreateSet.issubset(CreateSet2))
# CreateSet 与CreateSet2 先取差集,然后再从CreateSet中Remove掉
"""
True
False
"""
CreateSet2 是否是 CreateSet的子集,是子集就返回True
判断A集合与B集合是否没有相同的元素,如果没有相同的就返回True
“”" Return True if two sets have a null intersection. “”"
CreateList = list(["Tesla","Elon Mask",1,2])
CreateList1 = list(["Tesla","Elon Mask",1,2])
CreateList2 = list([1,2])
CreateList3 = list(["Tesla","Elon Mask","Space X"])
CreateSet = set(CreateList)
CreateSet1 = set(CreateList1)
CreateSet2 = set(CreateList2)
CreateSet3 = set(CreateList3)
print(CreateSet.isdisjoint(CreateSet2))
print(CreateSet2.isdisjoint(CreateSet3))
# CreateSet 与CreateSet2 先取差集,然后再从CreateSet中Remove掉
"""
False
True
"""