和列表非常相似 不点同
s = {10,2,3,4,1,2,3,1,1,2,10}
print(s)
s= {[1,2,3],[4,5,6]}
print(s)
#运行报错
s = set([1,2,3,4,5,1,2,3,4,5])
s = set({'a':1,'b':2,'c':3})
s = {'a','b',1,2,3}
print(s,type(s))
print(s[0])
此时运行会报错,因为集合不支持通过索引的方法取值,而如果想要通过索引的方式来取值的话,可以将其转成列表再取,方法如下:
print(list(s)[0])
s2 = set('python')
s.update(s2)
{'o', 1, 2, 3, 'y', 10, 'a', 'b', 'n', 'h', 't', 30, 'p'}
result = s.pop() # 返回的是删除的元素信息
print(result)
s1 = {1,2,3,4,5}
s2 = {3,4,5,6,7}
result = s1 & s2
print(result)
# {3, 4, 5}
result = s1 | s2
print(result)
# {1, 2, 3, 4, 5, 6, 7}
result = s1 - s2
print(result)
#{1, 2}
result = s1 ^ s2
print(result)
# {1, 2, 6, 7}
a = {1,2,3}
b = {1,2,3,4,5,6}
result = a <= b # a集合是b集合的子集 b集合是a集合的超集 结果为 True
result = {1,2,3} < {1,2,3}
print('result = ',result,s1,s2)