字典类型详细方法,写作业,2小时半
集合,半小时:
集合也是无序的,与字典不同的区别在于集合是这样的 set = {3, 2, 1, 4}
注意:集合存在的意义就在于去重和关系运算
确定性,无序性,去重性
1. 交集 set1 & set2 或者 set1.intersection(set2),除了合并之外还有去重功能
2. 并集 set1 | set2 或者 set1.union(set2)
3. 差集 set1.difference(set2) 或者 set1 - set2,即在set1中而不在set2中的集合
4. 对称差集 set1.symmetric_difference(set2) 或者 set1 ^ set2,去掉兼有的,留下独自的,合并成一个集合
set1 in set2
set1 not in set2
set1 == set2
set1 != set2
两个集合之间一般有三种关系,相交、包含、不相交。在Python中分别用下面的方法判断:
- set.isdisjoint(s):判断两个集合是不是不相交
- set.issuperset(s):判断集合是不是包含其他集合,等同于a>=b
- set.issubset(s):判断集合是不是被其他集合包含,等同于a<=b
s.add()
s.discard()
s.remove()
s.pop()
s.clear()