python set

作者: little_short | 来源:发表于2018-05-17 18:21 被阅读0次
可变集合set   集合对象是一组无序排列的可哈希的值,集合成员可以做字典的键
s = set('12313 2')
print(s)

s1 = [1,2,3,1]

print(list(set(s1)))

报错
li = [[1,2],1,2]
set(li)
不可变集合 frozenset()
li = [1,'a','b']
s = set(li)
dic = {s:'123'}

集合取值/判断
遍历取值
li = [1,'ab','b']
s = set(li)
s.add('www')
print(s)
s.update([12,2])
print(s)
s.remove(1)
print(s)
s.pop()
print(s)
s.clear()
print(s)
清空 为空大空号都没了和删除不一样

in
li = [1,'a','b']
s = set(li)
print(2 in li)
not in

集合等价不等价

s = set('alex') == set('alexexex')

包含
print(set('alex') < set('alexw'))

a = set([1, 2, 3, 4, 5])
b = set([4, 5, 6, 7, 8])

交集
s = a.intersection(b)
s = a&b
print(s)

并集  |
s1 = a.union(b)
s1 = a | b
print(s1)

差集  a里面有b里面没有 -
s2 = a.difference(b)
s2 = a - b
print(s2)

对称差集
s3 = a.symmetric_difference(b)
s3 = a ^ b
print(s3)

父级  超集  >
print(a.issuperset(b))

子集 <
print(a.issubset(b))


短路
a = set([1, 2, 3, 4, 5, 9])
b = set([4, 5, 6, 7, 8])

print(a and b)

print(a or b)

相关文章

  • python list与set的区别

    python list与set的区别 python数据类型:列表List, Set集合; 列表List: 1.创...

  • Python ☞ day 4

    Python学习笔记之 (set)集合 & 迭代器 & 函数 & 匿名函数 & 高阶函数 set set:类似di...

  • gyp ERR! stack Error: Can't find

    Error: Can’t find Python executable “python”, you can set...

  • Python: set实例透析

    Python基础文章集合请移步。 Python里的 set数据类型 set是无序unique值的集合,常用来去重,...

  • 属性查找

    python set get 解释: python descriptor 详解:正文descriptor简介 de...

  • you can run: npm install --save

    1. Can't find Python executable "python", you can set the...

  • Python Set

    https://www.jianshu.com/p/f60fabfefc09 https://www.cnblog...

  • python set

  • Python:set

    set和dict类似,也是一组key的集合,但不存储value。由于key不能重复,所以,在set中,没有重复的k...

  • python - set

    set set的应用 找到list,file中的unique elements 找到various combina...

网友评论

    本文标题:python set

    本文链接:https://www.haomeiwen.com/subject/ymridftx.html