写点什么

Python 训练营笔记 数据结构大汇总 Day6

发布于: 2021 年 06 月 16 日

环境说明

Python 3.8.5

IPython 7.19.0


天池龙珠计划 Python 训练营

https://tianchi.aliyun.com/specials/promotion/aicamppython


所记录的知识点

  1. set()

  2. set(str)

  3. set 不支持索引访问

  4. intersection intersection_update

  5. union

  6. difference difference_update

  7. symmetric_difference symmetric_difference_update

  8. issubset

  9. issuperset

  10. isdisjoint

  11. reversed(seq)

  12. zip


1、set()

创建空集合,只能使用 set()


In [1]: empty_set = set()
In [2]: empty_setOut[2]: set()
In [3]: {}Out[3]: {}
In [4]: type({}) # dict!Out[4]: dict
In [5]: 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. | | __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. | | ---------------------------------------------------------------------- | Static methods defined here: | | __new__(*args, **kwargs) from builtins.type | Create and return a new object. See help(type) for accurate signature. | | ---------------------------------------------------------------------- | Data and other attributes defined here: | | __hash__ = None
复制代码


2、set(str)

set(str) 可以对字符串中的元素进行去重


In [4]:  set("hello world")Out[4]: {' ', 'd', 'e', 'h', 'l', 'o', 'r', 'w'}
复制代码


3、set 不支持索引访问

'set' object is not subscriptable


In [1]:  set("hello world")Out[1]: {' ', 'd', 'e', 'h', 'l', 'o', 'r', 'w'}
In [2]: set("hello world")[0]---------------------------------------------------------------------------TypeError Traceback (most recent call last)<ipython-input-2-9d9ce92dfea5> in <module>----> 1 set("hello world")[0]
TypeError: 'set' object is not subscriptable
复制代码


4、intersection intersection_update

集合论中,设 A,B 是两个集合,由所有属于集合 A 且属于集合 B 的元素所组成的集合,叫做集合 A 与集合 B 的交集

intersection : 交集,返回一个新的集合

intersection_update : 交集,更新现有的集合


In [4]: set_a = set([1,2,3,4,5])
In [5]: set_b = set([3,4,5,6,7])
In [6]: set_c = set_a.intersection(set_b)
In [7]: set_aOut[7]: {1, 2, 3, 4, 5}
In [8]: set_bOut[8]: {3, 4, 5, 6, 7}
In [9]: set_cOut[9]: {3, 4, 5}
In [10]: help(set().intersection)Help on built-in function intersection:
intersection(...) method of builtins.set instance Return the intersection of two sets as a new set.
(i.e. all elements that are in both sets.)
复制代码


In [11]: set_aOut[11]: {1, 2, 3, 4, 5}
In [12]: set_bOut[12]: {3, 4, 5, 6, 7}
In [13]: set_a.intersection_update(set_b)
In [14]: set_aOut[14]: {3, 4, 5}
In [15]: set_bOut[15]: {3, 4, 5, 6, 7}
In [16]: help(set().intersection_update)Help on built-in function intersection_update:
intersection_update(...) method of builtins.set instance Update a set with the intersection of itself and another.
复制代码


5、union

给定两个集合 A,B,把他们所有的元素合并在一起组成的集合,叫做集合 A 与集合 B 的并集

union:并集,返回一个新的集合


In [19]: set_a = set([1,2,3,4,5])
In [20]: set_b = set([3,4,5,6,7])
In [21]: set_a.union(set_b)Out[21]: {1, 2, 3, 4, 5, 6, 7}
In [22]: help(set().union)Help on built-in function union:
union(...) method of builtins.set instance Return the union of sets as a new set.
(i.e. all elements that are in either set.)
复制代码


6、difference difference_update

设 A,B 是两个集合,由所有属于 A 且不属于 B 的元素组成的集合,叫做集合 A 减集合 B(或集合 A 与集合 B 之差)

difference:差集,返回一个新的集合

difference_update: 差集,更新现有的集合


In [24]: set_a = set([1,2,3,4,5])
In [25]: set_b = set([3,4,5,6,7])
In [26]: set_a.difference(set_b)Out[26]: {1, 2}
In [27]: help(set().difference)Help on built-in function difference:
difference(...) method of builtins.set instance 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.)
复制代码


In [28]: set_a = set([1,2,3,4,5])
In [29]: set_b = set([3,4,5,6,7])
In [30]: set_a.difference_update(set_b)
In [31]: set_aOut[31]: {1, 2}
In [32]: set_bOut[32]: {3, 4, 5, 6, 7}
In [33]: help(set().difference_update)Help on built-in function difference_update:
difference_update(...) method of builtins.set instance Remove all elements of another set from this set.
复制代码


7、symmetric_difference symmetric_difference_update

对称差集:集合 A 与集合 B 中所有不属于 A∩B 的元素的集合(个人理解:A 并 B 后,再除去 A 交 B 的元素)

symmetric_difference:对称差集,返回一个新的集合

symmetric_difference_update:对称差集,更新现有的集合


In [35]: set_a = set([1,2,3,4,5])
In [36]: set_b = set([3,4,5,6,7])
In [37]: set_c = set_a.symmetric_difference(set_b)
In [38]: set_aOut[38]: {1, 2, 3, 4, 5}
In [39]: set_bOut[39]: {3, 4, 5, 6, 7}
In [40]: set_cOut[40]: {1, 2, 6, 7}
In [46]: help(set().symmetric_difference)Help on built-in function symmetric_difference:
symmetric_difference(...) method of builtins.set instance Return the symmetric difference of two sets as a new set.
(i.e. all elements that are in exactly one of the sets.)
复制代码


In [41]: set_aOut[41]: {1, 2, 3, 4, 5}
In [42]: set_bOut[42]: {3, 4, 5, 6, 7}
In [43]: set_a.symmetric_difference_update(set_b)
In [44]: set_aOut[44]: {1, 2, 6, 7}
In [45]: set_bOut[45]: {3, 4, 5, 6, 7}
In [47]: help(set().symmetric_difference_update)Help on built-in function symmetric_difference_update:
symmetric_difference_update(...) method of builtins.set instance Update a set with the symmetric difference of itself and another.
复制代码


8、issubset

a.issubset(b):被包含,即 a 是 b 的子集吗?


In [49]: set_a = {1,2,3,4,5,6,7}
In [50]: set_b = {1,2,3,4}
In [51]: set_c = {0,1,2}
In [52]: set_c.issubset(set_a)Out[52]: False
In [53]: set_b.issubset(set_a)Out[53]: True
In [54]: set_a.issubset(set_b)Out[54]: False
In [55]: set_a.issubset(set_c)Out[55]: False
复制代码


9、issuperset

a.issuperset(b):超集,即 a 是 b 的超集吗?


In [57]: set_a = {1,2,3,4,5,6,7}
In [58]: set_b = {1,2,3,4}
In [59]: set_c = {0,1,2}
In [60]: set_a.issuperset(set_b)Out[60]: True
In [61]: set_a.issuperset(set_c)Out[61]: False
In [62]: set_b.issuperset(set_a)Out[62]: False
In [63]: set_c.issuperset(set_a)Out[63]: False
复制代码


10、isdisjoint

isdisjoint:判断两个集合是否没有相同元素


In [64]: set_aOut[64]: {1, 2, 3, 4, 5, 6, 7}
In [65]: set_bOut[65]: {1, 2, 3, 4}
In [66]: set_cOut[66]: {0, 1, 2}
In [67]: set_d = {9}
In [68]: set_d.isdisjoint(set_a)Out[68]: True
In [69]: set_c.isdisjoint(set_a)Out[69]: False
In [70]: set_b.isdisjoint(set_a)Out[70]: False
In [71]: set_a.isdisjoint(set_a)Out[71]: False
复制代码

11、reversed(seq)

reversed(seq):返回一个反转的迭代器


In [75]:  s = "hello world"
In [76]: iter = reversed(s)
In [77]: iterOut[77]: <reversed at 0x20a46771df0>
In [78]: type(iter)Out[78]: reversed
In [79]: next(iter)Out[79]: 'd'
In [80]: next(iter)Out[80]: 'l'
In [81]: next(iter)Out[81]: 'r'
In [82]: next(iter)Out[82]: 'o'
In [83]: next(iter)Out[83]: 'w'
In [84]: next(iter)Out[84]: ' '
In [85]: next(iter)Out[85]: 'o'
In [86]: next(iter)Out[86]: 'l'
In [87]: next(iter)Out[87]: 'l'
In [88]: next(iter)Out[88]: 'e'
In [89]: next(iter)Out[89]: 'h'
In [90]: next(iter)---------------------------------------------------------------------------StopIteration Traceback (most recent call last)<ipython-input-90-1a5d8c630f55> in <module>----> 1 next(iter)
StopIteration:
In [91]: help(reversed)Help on class reversed in module builtins:
class reversed(object) | reversed(sequence, /) | | Return a reverse iterator over the values of the given sequence. | | Methods defined here: | | __getattribute__(self, name, /) | Return getattr(self, name). | | __iter__(self, /) | Implement iter(self). | | __length_hint__(...) | Private method returning an estimate of len(list(it)). | | __next__(self, /) | Implement next(self). | | __reduce__(...) | Return state information for pickling. | | __setstate__(...) | Set state information for unpickling. | | ---------------------------------------------------------------------- | Static methods defined here: | | __new__(*args, **kwargs) from builtins.type | Create and return a new object. See help(type) for accurate signature.
复制代码


12、zip

zip:打包 zip(*list):将元祖解压为列表


In [93]: a = [1,2,3]
In [94]: b = [4,5,6]
In [95]: c = [7,8,9,0]
In [96]: zip(a,b)Out[96]: <zip at 0x20a4693fe40>
In [97]: list(zip(a,b))Out[97]: [(1, 4), (2, 5), (3, 6)]
In [98]: list(zip(a,c))Out[98]: [(1, 7), (2, 8), (3, 9)]
In [99]: list(zip(c,a))Out[99]: [(7, 1), (8, 2), (9, 3)]
复制代码


In [103]: d = [(7, 1), (8, 2), (9, 3)]
In [104]: list(zip(*d))Out[104]: [(7, 8, 9), (1, 2, 3)]
In [105]: d = [(7, 1, 3), (8, 2,4), (9, 3,5)]
In [106]: list(zip(*d))Out[106]: [(7, 8, 9), (1, 2, 3), (3, 4, 5)]
In [107]: d = [(7, 1, 3), (8, 2,4), (9, 3,5,6)]
In [108]: list(zip(*d))Out[108]: [(7, 8, 9), (1, 2, 3), (3, 4, 5)]
In [109]: d = [(7, 1, 3,7), (8, 2,4), (9, 3,5,6)]
In [110]: list(zip(*d))Out[110]: [(7, 8, 9), (1, 2, 3), (3, 4, 5)]
In [111]: d = [(7, 1, 3,7), (8, 2,4,8,0), (9, 3,5,6)]
In [112]: list(zip(*d))Out[112]: [(7, 8, 9), (1, 2, 3), (3, 4, 5), (7, 8, 6)]
复制代码


In [116]: help(zip)Help on class zip in module builtins:
class zip(object) | zip(*iterables) --> A zip object yielding tuples until an input is exhausted. | | >>> list(zip('abcdefg', range(3), range(4))) | [('a', 0, 0), ('b', 1, 1), ('c', 2, 2)] | | The zip object yields n-length tuples, where n is the number of iterables | passed as positional arguments to zip(). The i-th element in every tuple | comes from the i-th iterable argument to zip(). This continues until the | shortest argument is exhausted. | | Methods defined here: | | __getattribute__(self, name, /) | Return getattr(self, name). | | __iter__(self, /) | Implement iter(self). | | __next__(self, /) | Implement next(self). | | __reduce__(...) | Return state information for pickling. | | ---------------------------------------------------------------------- | Static methods defined here: | | __new__(*args, **kwargs) from builtins.type | Create and return a new object. See help(type) for accurate signature.
复制代码



欢迎各位同学一起来交流学习心得!

欢迎关注微信公众号:悟为生心



发布于: 2021 年 06 月 16 日阅读数: 13
用户头像

己所不欲,勿施于人。 2021.05.29 加入

一位喜欢生命科学和计算机科学,崇尚开源精神的码农。

评论

发布
暂无评论
Python训练营笔记 数据结构大汇总 Day6