python基础知识笔记-集合
集合使用花括号将元素之间用逗号隔开即可。集合的输出里不会出现相同的元素。集合中有并集、交集、差集等,以下为一些简单的表达:
def main(): list1=[1,1,2,2,3,3] print(list1) set1={1,1,2,2,3,3} print(set1) set1.add(4) set1.add(5) print(set1) set2={1,3,5,7,9} print(set2) set3=set1 & set2 #set3=set1.intersection(set2) print(set3) set3=set1 | set2 #st3=set1.union(set2) print(set3) set3=set1-set2 #set3=set1.difference(set2) print(set3) set3 = set2.difference(set1) print(set3) set3=set1 ^ set2 #set3=set1.symmetric_difference(set2) print(set3) for val in set2: print(val) print(set2.pop()) if 3 in set2: set2.remove(3) print(set2) print(set2<=set1) #print(set2.issubset(set1)) print(set1>=set2) #print(set1.issuperset(set2)) if __name__ == '__main__': main() 结果: [1, 1, 2, 2, 3, 3] {1, 2, 3} {1, 2, 3, 4, 5} {1, 3, 5, 7, 9} {1, 3, 5} {1, 2, 3, 4, 5, 7, 9} {2, 4} {9, 7} {2, 4, 7, 9} 1 3 5 7 9 1 {5, 7, 9} False False
千行代码,Bug何处藏。 纵使上线又怎样,朝令改,夕断肠。