set

Python 从 2.4 版引入了一种集合类型 --- set 。

Python 2.4 also introduced a new collection type, the set—an unordered collection of
unique and immutable objects
 that supports operations corresponding to mathematical set theory.

If you think sets are “cool,” they recently became noticeably cooler.

Sets are powerful and flexible objects, but they do have one constraint in both 3.0 and
2.6 that you should keep in mind—largely because of their implementation, sets can only contain immutable (a.k.a “hashable”) object types.

Sets themselves are mutable too, and so cannot be nested in other sets directly.

 


1 集合中的元素是唯一的,即集合中不能可找到两个相同的元素。

集合中的元素是 immutable ( 不可变的),因此:

可以用作集合元素的数据类型:

      int , float, str, bool ,tuple

      但 tuple的元素中有 list 或 dict 元素时不能用作集合的元素。

不能用作集合元素的数据类型:

     list , dict

 

 

2 求集合中元素的个数:len( aSetName )

例:
a = { 1,2,3,3,4,4,5,5,5,5,1,1,1,1,2,2,2 }
print( len( a ))
print( a )

--- 结果 ---
5

{ 1,2,3,4,5 }

3 向集合中添加元素 : aSetName.add( 一个新元素 )

a = { 1,2,3,4,5 }
a.add( 10 )
print ( a )
a.add( 6 )
print ( a )
----结果----
{ 1,2,3,4,5,10 }
{ 1,2,3,4,5,6,10 }

向集合中添加同一个元素,不管加入多少次,该集合中只保存一个该元素。

4 从集合中删除一个元素 aSetName.discard ( 一个元素x )

如果集合中这个元素,则从集合中删除它,
如果集合中不存这个元素,则什么也不做。

a = { 1,2,3 }
print ( a )
a.discard( 1 )
print ( a )
a.discard( 1 )
print ( a ) 
--- 结果 ---
{ 1,2,3 }
{ 2,3 }
{ 2,3 }

5 判断一个集合是否另一个集合的子集合: aSetName.issubset( anotherSet )

a = { 1,2,3,4,5 }
b = { 1,2,3 }
print (b, "是否",a, "的集合:", b.issubset( a ) )
b.add( 8 )
print (b, "是否",a, "的集合:", b.issubset( a ) )

---结果----
{1, 2, 3} 是否 {1, 2, 3, 4, 5} 的集合: True
{8, 1, 2, 3} 是否 {1, 2, 3, 4, 5} 的集合: False


6 从一个集合中删除另一个集合的所有元素:

a = { 1,2,3,4,5 }
b = { 1,2,3 }
c = a - b
d = b - a
print ( a,"-", b,"=", c)
print ( b,"-", a,"=", d)
----结果----
{1, 2, 3} 是否 {1, 2, 3, 4, 5} 的集合: True
{8, 1, 2, 3} 是否 {1, 2, 3, 4, 5} 的集合: False
{1, 2, 3, 4, 5} - {1, 2, 3} = {4, 5}
{1, 2, 3} - {1, 2, 3, 4, 5} = set()

注意: 
空集合 显示的是 "set() ",而不是 " {} "。
这提醒我们: 构造一个空集的方法应写作:
b = set()
而不能写作: 
b = { }

 

 

 

7 求一个集合与其他集合的交集:

a = { 1,2,3,4,5 }
b = { 0,1,2,3,8 }
c = { 3, 8,7 }
x = a.intersection( b )
y = a.intersection( b , c)
print ( a,"与", b,"的交集是:/n",x)
print ( a,"与", b,c,"的交集是:/n",y)

----结果----
1, 2, 3, 4, 5} 与 {8, 1, 2, 3, 0} 的交集是:
{1, 2, 3}
{1, 2, 3, 4, 5} 与 {8, 1, 2, 3, 0} {8, 3, 7} 的交集是:
{3}

 

print (   {1,2,3} & {1,2,4,5} )

--- 结果 --

{1, 2}

 

 


8 求一集合与其他集合的并集:

a = { 1,2,3,4,5 }
b = { 0,1,2,3,8 }
c = { 3, 8,7 }
x = a.union( b )
y = a.union( b,c )
print ( a,"与", b,"的并集是:/n",x)
print ( a,"与", b,c,"的并集是:/n",y)

--- 结果 ----
{1, 2, 3, 4, 5} 与 {8, 1, 2, 3, 0} 的并集是:
{0, 1, 2, 3, 4, 5, 8}
{1, 2, 3, 4, 5} 与 {8, 1, 2, 3, 0} {8, 3, 7} 的并集是:
{0, 1, 2, 3, 4, 5, 7, 8}

 

a={1,2,3}

b={3,4,5}

c= a b

print ( c )

-- 结果--

{1,2,3,4,5}

 

9 构造一个集合的常用方法举例

a=set()
print ( a)
a={ 1,2,3}
print ( a )
a = set( "abcdfgabbcdd112233.56")
print ( a )
a = set( [ 1,2,"abcd" ])
print ( a )
a = set( ( 1,2,"abcd" ))
print ( a )

--结果 --
set()
{1, 2, 3}
{'a', 'c', 'b', 'd', 'g', 'f', '.', '1', '3', '2', '5', '6'}
{'abcd', 1, 2}
{'abcd', 1, 2}

 

10 集合转化为列表:

a={ 1,2,3,4}
b= list( a)
print( a,b)

---结果--

{1, 2, 3, 4} [1, 2, 3, 4]

11 集合转化为字符串

a= { 1,2,3,4}
c= [ str(x) for x in a ]
s= "".join( c )
print ( s )

-- 结果 --

1234

 

 

 

12 判断一个元素是否属于某个集合?

a = { 1,2,3}

print ( 1 in a )

print ( 10 in a )

--- 结果 --

True

False

 

 

13 集合运算符小结:

集合的并集:

   { 1,2,3 } | { 3,4,5 }   结果为 { 1,2,3,4,5}

集合的交集:

{ 1,2,3 } & { 3,4,5 }   结果为 { 3 }

集合的差集:

{ 1,2,3 } - { 3,4,5 }   结果为 {   1,2 }

判断一个元素是否属于集合:

in { 1,2,3}   结果   True

判断是否子集合:

A = { 1,2};print( A.issubset( { 1,2,3,4 } )) 结果是   True

两个集合的 Symmetric difference :

{1,2,3} {1,2,4,5} 结果是 {3, 4, 5}

相当于并集减交集:    即: ({ 1,2,3} { 1,2,4,5 }) ( { 1,2,3} & { 1,2,4,5 })

 

 

Why Sets?

>>> engineers = {'bob', 'sue', 'ann', 'vic'}
>>> managers = {'tom', 'sue'}
>>> 'bob' in engineers # Is bob an engineer?
True
>>> engineers & managers # 
Who is both engineer and manager?

{'sue'}
>>> engineers | managers # All people in either category
{'vic', 'sue', 'tom', 'bob', 'ann'}
>>> engineers – managers # Engineers who are not managers
{'vic', 'bob', 'ann'}
>>> managers – engineers # Managers who are not engineers
{'tom'}
>>> engineers > managers # Are all managers engineers? (superset)
False
>>> {'bob', 'sue'} < engineers # Are both engineers? (subset)
True
>>> (managers | engineers) > managers # All people is a superset of managers
True
>>> managers ^ engineers # Who is in one but not both?
{'vic', 'bob', 'ann', 'tom'}
>>> (managers | engineers) - (managers ^ engineers) # Intersection!
{'sue'}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值