前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >python集合(set)类型

python集合(set)类型

作者头像
用户7886150
修改2021-01-26 10:29:53
4280
修改2021-01-26 10:29:53
举报
文章被收录于专栏:bit哲学院

参考链接: Python set集合 intersection()

集合(set) 

python的数据结构的另一种表现形式。作用:自动清除集合类型中的元素重复数据(set),以及元素排序。集合类型的元素排序是无序不重复。 

快速访问集合(set) 

set1=set([1,2,3,4])

set2=set(['A','B','D','C'])

set3=set(['A','C', 'B', 'D'])

set4=set(['A','B','D','C','B'])

set5=set('E')

#set6=set('E','A')

#错误提示TypeError: set expected at most 1 arguments, got 2------所以set6不合理

set7=set('hello')

set8=set(['A','B','D','C','B'])

print('1.',set1)

print('2.',set2)

print('3.',set3)

print('4.',set4)

print('5.',set5)

print('6.',set7)

print('7.',set8)

print('8.',id(set1))

print('9.',id(set2))

print('10.',id(set3))

print('11.',id(set4))

print('12.',id(set5))

print('13.',id(set7))

print('14.',id(set8))

#输出1. {1, 2, 3, 4}

2. {'A', 'C', 'B', 'D'}

3. {'A', 'C', 'B', 'D'}

4. {'A', 'C', 'B', 'D'}

5. {'E'}

6. {'h', 'e', 'l', 'o'}

7. {'A', 'C', 'B', 'D'}

8. 1462456352552

9. 1462470585032

10. 1462470585704

11. 1462456352328

12. 1462470586152

13. 1462470585928

14. 1462470585480

涉及修改集合数据的相关函数以及运算符—程序说明 

set9=set([1,2,3,4,5,6,7])

set20=set([1,2,3,5,8,4,7])

set11=set9

set12=set9

#set13=set9

set14=set9

set15=set9

set16=set9

#set17=set9

set18=set9

set19=set9

#set22=set9

print('18.',set9)

print('19.',set20)

#输出 18. {1, 2, 3, 4, 5, 6, 7}

      19. {1, 2, 3, 4, 5, 7, 8}

set9.add(8)

print('1.',set9)

#输出 1. {1, 2, 3, 4, 5, 6, 7, 8}

#.add 集合类型添加元素

set21=set20.copy()

print('13.',id(set20))

print('14.',id(set21))

print('15.',set21)

print('16.',set20)

#输出 13. 1462470584360

      14. 1462472086472

      15. {1, 2, 3, 4, 5, 7, 8}

      16. {1, 2, 3, 4, 5, 7, 8}

#.copy 集合类型元素复制

set9.discard(2)

set11.discard(99)

print('3.',set9)

print('4.',set11)

#输出 3. {1, 3, 4, 5, 6, 7, 8}

      4. {1, 3, 4, 5, 6, 7, 8}

#.discard 删除集合类型中指定元素,如果指定元素不存在不会报错

set12.pop()

#set13.pop(1)

#错误提示STypeError: pop() takes no arguments (1 given)------所以set13不合理

print('5.',set12)

#print('6.',set13)

#输出 5. {3, 4, 5, 6, 7, 8}

#.pop 随机删除集合类型中元素

set14.remove(7)

set15.remove(99)

#输出 KeyError: 99(出现报错) 

print('7.',set14)

print('8.',set15)

#输出 7. {3, 4, 5, 6, 8}

#.remove 删除集合类型中指定元素,如果指定元素不存在会报错,并终止程序

set16.update([88,99])

#set17.update([88,99,77])

#错误提示SyntaxError: invalid character in identifier------所以set17不合理

set18.update([88])

print('9.',set16)

#print('10.',set17)

print('11.',set18)

#输出 9. {3, 4, 5, 6, 99, 8, 88}

     11. {3, 4, 5, 6, 99, 8, 88}

#.update 更新集合类型的元素

set19.clear()

#del set22()

#错误提示SyntaxError: can't delete function call------所以set22不合理

print('12.',set19)

#print('17.',set22)

#输出 12. set()

#.claer 清空集合类型的元素

 .del set() 清空集合

print('20.',set9.issubset(set20))

print('21.',set9<=set20)

#输出 21. False

#输出 20. False

#setA.issubset(setB) 运算符: setA<=setB 检测setA中的每一个元素是否都在setB中,子集(setB是否是setA的子集)

print('22.',set9.issuperset(set20))

#输出 22. False

#setA.issuperset(setB) 运算符: setA>=setB 检测setB中的每一个元素是否都在setA中,父集(setB是否是setA的父集)

print('23.',set9.union(set20))

#输出 23. {1, 2, 3, 4, 5, 6, 7, 8}

#setA.union(setB) 运算符: setA\setB 创造一个新的集合,元素包含setB和setA所有元素,并集(所有元素:setB+setA)

print('24.',set9.intersection(set20))

#输出 24. {1, 2, 3, 4, 5, 7}

#setA.intersection(setB) 运算符: setA&setB 检创造一个新的集合,元素包含setB和setA共同元素,交集(共同元素:setB+setA)

print('25.',set20.difference(set9))

#输出 25. {8}

#setB.difference(setA) 运算符: setB-setA 检创造一个新的集合,元素包含setB和setA集合元素中,只setB所拥有的元素,差集(差异元素:setB-setA)

print('26.',set9.symmetric_difference(set20))

#输出 26. {6, 8}

#setA.symmetric_difference(setB) 运算符: setA^setB 检创造一个新的集合,元素包含setB和setA集合元素中,不重复的元素,对称差集(差异元素:setA^setB)

集合(set)帮助文档 

print(help(set))

Help on class set in module builtins:

class set(object)

 |  set() -> new empty set object

 |  set(iterable) -> new set object

 |  

 |  Build an unordered collection of unique elements.

 |  

 |  Methods defined here:

 |  

 |  __and__(self, value, /)

 |  Return self&value.

 |  

 |  __contains__(...)

 |  x.__contains__(y) <==> y in x.

 |  

 |  __eq__(self, value, /)

 |  Return self==value.

 |  

 |  __ge__(self, value, /)

 |  Return self>=value.

 |  

 |  __getattribute__(self, name, /)

 |  Return getattr(self, name).

 |  

 |  __gt__(self, value, /)

 |  Return self>value.

 |  

 |  __iand__(self, value, /)

 |  Return self&=value.

 |  

 |  __init__(self, /, *args, **kwargs)

 |  Initialize self.  See help(type(self)) for accurate signature.

 |  

 |  __ior__(self, value, /)

 |  Return self|=value.

 |  

 |  __isub__(self, value, /)

 |  Return self-=value.

 |  

 |  __iter__(self, /)

 |  Implement iter(self).

 |  

 |  __ixor__(self, value, /)

 |  Return self^=value.

 |  

 |  __le__(self, value, /)

 |  Return self<=value.

 |  

 |  __len__(self, /)

 |  Return len(self).

 |  

 |  __lt__(self, value, /)

 |  Return self<value.

 |  

 |  __ne__(self, value, /)

 |  Return self!=value.

 |  

 |  __new__(*args, **kwargs) from builtins.type

 |  Create and return a new object.  See help(type) for accurate signature.

 |  

 |  __or__(self, value, /)

 |  Return self|value.

 |  

 |  __rand__(self, value, /)

 |  Return value&self.

 |  

 |  __reduce__(...)

 |  Return state information for pickling.

 |  

 |  __repr__(self, /)

 |  Return repr(self).

 |  

 |  __ror__(self, value, /)

 |  Return value|self.

 |  

 |  __rsub__(self, value, /)

 |  Return value-self.

 |  

 |  __rxor__(self, value, /)

 |  Return value^self.

 |  

 |  __sizeof__(...)

 |  S.__sizeof__() -> size of S in memory, in bytes

 |  

 |  __sub__(self, value, /)

 |  Return self-value.

 |  

 |  __xor__(self, value, /)

 |  Return self^value.

 |  

 |  add(...)

 |  Add an element to a set.

 |  

 |  This has no effect if the element is already present.

 |  

 |  clear(...)

 |  Remove all elements from this set.

 |  

 |  copy(...)

 |  Return a shallow copy of a set.

 |  

 |  difference(...)

 |  Return the difference of two or more sets as a new set.

 |  

 |  (i.e. all elements that are in this set but not the others.)

 |  

 |  difference_update(...)

 |  Remove all elements of another set from this set.

 |  

 |  discard(...)

 |  Remove an element from a set if it is a member.

 |  

 |  If the element is not a member, do nothing.

 |  

 |  intersection(...)

 |  Return the intersection of two sets as a new set.

 |  

 |  (i.e. all elements that are in both sets.)

 |  

 |  intersection_update(...)

 |  Update a set with the intersection of itself and another.

 |  

 |  isdisjoint(...)

 |  Return True if two sets have a null intersection.

 |  

 |  issubset(...)

 |  Report whether another set contains this set.

 |  

 |  issuperset(...)

 |  Report whether this set contains another set.

 |  

 |  pop(...)

 |  Remove and return an arbitrary set element.

 |  Raises KeyError if the set is empty.

 |  

 |  remove(...)

 |  Remove an element from a set; it must be a member.

 |  

 |  If the element is not a member, raise a KeyError.

 |  

 |  symmetric_difference(...)

 |  Return the symmetric difference of two sets as a new set.

 |  

 |  (i.e. all elements that are in exactly one of the sets.)

 |  

 |  symmetric_difference_update(...)

 |  Update a set with the symmetric difference of itself and another.

 |  

 |  union(...)

 |  Return the union of sets as a new set.

 |  

 |  (i.e. all elements that are in either set.)

 |  

 |  update(...)

 |  Update a set with the union of itself and others.

 |  

 |  ----------------------------------------------------------------------

 |  Data and other attributes defined here:

 |  

 |  __hash__ = None

本文系转载,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文系转载前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档