JackLi07

python之集合操作

1.集合的增操作

  add(item):增加至集合中

  update(set):  若不存在,更新至集合中

2.集合的删操作(五种)

  pop():  随机删除并返回值

  remove(item): 删除value值,若不存在Raise KeyError

  discard(item): 删除value值,若不存在,do nothing

  clear(): 清空集合

  del: 删除集合

3.集合的改操作

  无直接方法,先进行删除操作,再添加

4.集合的查操作

  for循环遍历

5.常用操作

s = {1, 2, 3, (55, 4776, 110), 21, "asxsa"}
s1 = {1, 55, 90, (445, 77), "zhang"}
# 并集
>>print(s & s1)
>>print(s.intersection(s2))
{1}
# 交集
>>print(s | s1)
>>print(s.union(s2))
{1, 2, 3, (55, 4776, 110), 'asxsa', 'zhang', 21, (445, 77), 55, 90}
# 反交集 交集-并集
>>print(s ^ s1)
>>print(s1.symmetric_difference(s2))
{2, 3, (55, 4776, 110), 21, 90, 'asxsa', 'zhang', (445, 77), 55}
# 差集 s-并集
>>print(s - s1)
>>print(s.difference(s1))
{2, 3, (55, 4776, 110), 'asxsa', 21}
# 子集
>>print(s < s1)
>>print(s1.issubset(s2))
False
# 超集
>>print(s > s1)
>>print(s1.issuperset(s2))
False

6.frozenset

s = frozenset(["赵本⼭", "刘能", "⽪⻓⼭", "⻓跪"])
dic = {s:'123'} # 可以正常使⽤了
print(dic)

 

posted @ 2018-10-29 19:35  JackLi07  阅读(325)  评论(1编辑  收藏  举报