Collection: In my world, you are the only exercises + review

## Collection: In my world, you're the one!
            (As can be seen when the braces are not privileged dictionary, in Python3 inside, starting with braces if not a bunch of digital mapping relationship, which is a collection of a bunch of numbers.)
           Here Insert Picture Description
            (In python, a collection of almost played the only role is unique, following repeated element will be automatically removed, the elements of the collection are unique, the collection will directly help us clean out the duplicate elements, it is convenient, but be aware that the sets are unordered)
Here Insert Picture Description
            ( However, to note that the sets are unordered, that is not an element of the index to the collection, as follows :)
            Here Insert Picture Description

## How to create a set of
           ※ One is directly to the pile of elements enclosed in braces
           Here Insert Picture Description
           ※ One is to use set () factory function
            (can be passed in a list, you can also pass into a tuple, you can even pass into a string, as long as the sequence is OK)
            Here Insert Picture Description

## classroom mess things to see
Here Insert Picture Description
            (did not learn before collection)
            Here Insert Picture Description
            (after learning the set, the first set with a function to return a collection of elements is not repeated, and then converted into a list with the list, but here, due to the set created the sets are unordered, so call list into the unordered list, we can not guarantee the original order of the list inside, and below the 0 brought to the front to go. go, came in order to help us automatically, so, your program Watchlist elements order problem, then use set this function must be vigilant, he got a set of unordered)
            Here Insert Picture Description

## How to access the value of the collection
            (you can use a for loop to a collection of elements of a read out, can also be used in and not in determining whether the elements in the collection)
            Here Insert Picture Description
           ※ the Add (): forgot to add elements collection
           Here Insert Picture Description

           ※remove():移出集合里指定的一个元素
           Here Insert Picture Description
##不可变集合
           ※frozenset():用来定义不可变集合
            (有时候我们希望我们集合中的数据具有一定的稳定性,不能够被随意地增加或删除集合里边的元素)
Here Insert Picture Description

##温故知新之习题
            0. 请问集合的唯一作用是什么呢?
           答:集合几乎所有的作用就是确保里边包含的元素的唯一性,就像世界上没有两片完全相同的树叶一样,集合内不可能存在两个相同的元素!

            1. 如果你希望创建的集合是不变的,应该怎么做?
           答:frozenset()

            2. 请问如何确定一个集合里边有多少个元素?
           答:没错,len()函数正好可以满足你此刻的需求_

            3. 请目测以下代码会打印什么内容?

>>> num_set = set([1, 2, 3, 4, 5])

>>> num_set[0]

           答:会报错,因为集合是无序的。无法通过索引的方式访问到集合里的元素

            4. 请问 set1 = {[1, 2]} 和 set1 = set([1, 2]) 执行的结果一样吗?
           答:不一样,set1 = set([1, 2]) 会生成一个集合{1, 2},但set1 = {[1, 2]}却会报错。

>>> set1 = {[1, 2]}
Traceback (most recent call last):
  File "<pyshell#17>", line 1, in <module>
    set1 = {[1, 2]}
TypeError: unhashable type: 'list'

           从报错信息上我们看到“列表不是可哈希类型”,没错,列表是可变的,它怎么可以哈希呢?!咦,等等,这句话好像在那听过……呃,敢情集合跟字典的存储方式一样的丫!

           其实你再想想就会觉得很有道理,利用哈希函数计算,相同的元素得到的哈希值(存放地址)是相同的,所以在集合中所有相同的元素都会覆盖掉,因此有了集合的唯一性。

           然后你继续接着想就觉得更有道理了,通过哈希函数计算的地址不可能是按顺序排放的,所以集合才强调是无序的!

            5. 打开你的IDLE,输入set1 = {1, 1.0},你发现了什么?
           答:没错, 集合内容是{1.0},其实你弄懂了上一题,这一题一样容易:因为在Python的哈希函数会将相同的值的元素计算得到相同的地址,所以1和1.0是等值的_

            6. 请问如何给集合添加和删除元素?
           答:使用add()方法可以为集合添加元素,使用remove()方法可以删除集合中已知的元素。

>>> num1.add(6)
>>> num1
{0, 1, 2, 3, 4, 5, 6}
>>> num1.remove(6)
>>> num1
{0, 1, 2, 3, 4, 5}

##动动手
           0. 自学扩展:自己花点时间看下这个表格(http://bbs.fishc.com/thread-45276-1-1.html),今后会用上的^_^
           由于集合类型不是我们教学的重点,所以课堂中小甲鱼仅强调基本的使用方法,这里帮大家把Python集合类型的所有内置方法做成一个总结表,以便供大家使用时参考。

           答:

集合(s).方法名

等价符号

方法说明

s.issubset(t) s <= t 子集测试(允许不严格意义上的子集):s 中所有的元素都是 t 的成员
s < t 子集测试(严格意义上):s != t 而且 s 中所有的元素都是 t 的成员
s.issuperset(t) s >= t 超集测试(允许不严格意义上的超集):t 中所有的元素都是 s 的成员
s > t 超集测试(严格意义上):s != t 而且 t 中所有的元素都是 s 的成员
s.union(t) s | t 合并操作:s "或" t 中的元素
s.intersection(t) s & t 交集操作:s "与" t 中的元素
s.difference s - t 差分操作:在 s 中存在,在 t 中不存在的元素
s.symmetric_difference(t) s ^ t The symmetric difference operation: s "or" t the elements, but not the elements common to s and t
s.copy() Returns a copy of s (shallow copy)

The following method applies only to variable collection

s.update s |= t Adding the elements to t in s
s.intersection_update(t) s &= t Intersection modification operations: s s and t includes only some members of the Chinese Communist Party
s.difference_update(t) s -= t Differential modification operations: s comprises only part of t s, but that are not members of the
s.symmetric_difference_update(t) s ^= t The symmetric difference operation Review: s included, or only part of s only are members of t
s.add(obj) Add: Add obj to s
s.remove(obj) Delete operations: delete the obj from s, if s obj does not exist, the exception is raised
s.discard(obj) Discard operation: s removed from the obj if obj s absence, it is also all right ^ _ ^
s.pop() Eject operation: removal and return of an arbitrary element s
s.clear() Clear: Clear all elements in s
Published 171 original articles · won praise 70 · views 50000 +

Guess you like

Origin blog.csdn.net/w15977858408/article/details/104071951