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

简介: 学习笔记 - 天池龙珠计划 - Python 训练营 Task2 Day6(集合、序列)

天池龙珠计划 Python训练营

所记录的知识点

  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_set
Out[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_a
Out[7]: {1, 2, 3, 4, 5}

In [8]: set_b
Out[8]: {3, 4, 5, 6, 7}

In [9]: set_c
Out[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_a
Out[11]: {1, 2, 3, 4, 5}

In [12]: set_b
Out[12]: {3, 4, 5, 6, 7}

In [13]: set_a.intersection_update(set_b)

In [14]: set_a
Out[14]: {3, 4, 5}

In [15]: set_b
Out[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_a
Out[31]: {1, 2}

In [32]: set_b
Out[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_a
Out[38]: {1, 2, 3, 4, 5}

In [39]: set_b
Out[39]: {3, 4, 5, 6, 7}

In [40]: set_c
Out[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_a
Out[41]: {1, 2, 3, 4, 5}

In [42]: set_b
Out[42]: {3, 4, 5, 6, 7}

In [43]: set_a.symmetric_difference_update(set_b)

In [44]: set_a
Out[44]: {1, 2, 6, 7}

In [45]: set_b
Out[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_a
Out[64]: {1, 2, 3, 4, 5, 6, 7}

In [65]: set_b
Out[65]: {1, 2, 3, 4}

In [66]: set_c
Out[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]: iter
Out[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.


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

目录
相关文章
|
1月前
|
搜索推荐 Python
Leecode 101刷题笔记之第五章:和你一起你轻松刷题(Python)
这篇文章是关于LeetCode第101章的刷题笔记,涵盖了多种排序算法的Python实现和两个中等难度的编程练习题的解法。
21 3
|
1月前
|
Python
Python 中常见的数据结构(二)
Python 中常见的数据结构(二)
|
1月前
|
存储 索引 Python
Python 中常见的数据结构(一)
Python 中常见的数据结构(一)
|
1月前
|
开发者 Python
Python 常用的数据结构
Python 常用的数据结构
|
1月前
|
存储 开发工具 Python
【Python项目】外星人入侵项目笔记
【Python项目】外星人入侵项目笔记
38 3
|
1月前
|
存储 Python
【免费分享编程笔记】Python学习笔记(二)
【免费分享编程笔记】Python学习笔记(二)
42 0
【免费分享编程笔记】Python学习笔记(二)
|
1月前
|
存储 索引 Python
python数据结构之列表详解
列表是Python中极为灵活和强大的数据结构,适合于存储和操作有序数据集合。掌握其基本操作和高级特性对于编写高效、清晰的Python代码至关重要。通过本回答,希望能帮助你全面理解Python列表的使用方法,从而在实际编程中更加游刃有余。
23 0
|
1月前
|
算法 C++ Python
Leecode 101刷题笔记之第四章:和你一起你轻松刷题(Python)
这篇博客是关于LeetCode上使用Python语言解决二分查找问题的刷题笔记,涵盖了从基础到进阶难度的多个题目及其解法。
15 0
|
1月前
|
算法 C++ Python
Leecode 101刷题笔记之第三章:和你一起你轻松刷题(Python)
本文是关于LeetCode算法题的刷题笔记,主要介绍了使用双指针技术解决的一系列算法问题,包括Two Sum II、Merge Sorted Array、Linked List Cycle II等,并提供了详细的题解和Python代码实现。
13 0
|
1月前
|
算法 C++ 索引
Leecode 101刷题笔记之第二章:和你一起你轻松刷题(Python)
本文是关于LeetCode 101刷题笔记的第二章,主要介绍了使用Python解决贪心算法题目的方法和实例。
11 0