集合使用{}表示,它是无序的,因此不能索引取出元素,也无法使用切片,一个集合不能出现重复的元素
>>> {1,2,3,4,5,6,1} {1, 2, 3, 4, 5, 6}
定义一个空的集合
set()
那么集合对于列表或者元素有什么优势呢?
比如两个集合{1,2,3,4,5,6} {3,4,7}
差集
>>> {1,2,3,4,5,6} - {3,4,7} {1, 2, 5, 6} >>> >>> {3,4,7} - {1,2,3,4,5,6} {7}
交集
>>> {1,2,3,4,5,6} & {3,4,7} {3, 4} >>> >>> {3,4,7} & {1,2,3,4,5,6} {3, 4}
并集
>>> {1,2,3,4,5,6} | {3,4,7} {1, 2, 3, 4, 5, 6, 7} >>> >>> {3,4,7} | {1,2,3,4,5,6} {1, 2, 3, 4, 5, 6, 7}