Skip to content

Latest commit

 

History

History
295 lines (185 loc) · 6.35 KB

File metadata and controls

295 lines (185 loc) · 6.35 KB

第6节 python的集合(set)

❤️💕💕python是一种动态的解释形语言,由于python的普遍性,学会python能更快的解决问题,以及学习其他的知识。Myblog:http:https://nsddd.top


[TOC]

集合set

myset = {"apple", "banana", "cherry"}

集合用于将多个项目存储在单个变量中。

集合是无序、不可更改和未索引的集合。

💡特点:

  • 无序
  • 不可更改
  • 不允许重复

set构造集合

In [1]: thisset = set(("apple", "banana", "cherry")) # note the double round-brackets
   ...: print(thisset)
{'apple', 'banana', 'cherry'}

访问集合

您不能通过引用索引或键来访问集合中的项目。

但是您可以使用循环遍历集合项,或者使用关键字for 询问集合中是否存在指定值 。in

In [2]: thisset = {"apple", "banana", "cherry"}
   ...: for x in thisset:
   ...:   print(x)
   ...:
apple
banana
cherry

In [3]: thisset[1] #不能直接访问集合
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Input In [3], in <cell line: 1>()
----> 1 thisset[1]

TypeError: 'set' object is not subscriptable

添加到集合

💡我们上面说过集合是不可以更改的,但是没说不可以添加元素把

In [7]: a
Out[7]: {2, 12, 31, 321}

In [8]: a.add(12)	# 本身a就有12

In [9]: a
Out[9]: {2, 12, 31, 321}

In [11]: a.add("12asd")

In [12]: a
Out[12]: {12, '12asd', 2, 31, 321}

上面我们添加了一个元素12,但是为什么没动静?因为我们添加的元素原本就已经有了,所以我们需要就是再添加一次不同的

还可以添加一个集合

要将另一个集合中的项目添加到当前集合中,请使用该update() 方法。

In [13]: thisset = {"apple", "banana", "cherry"}
    ...: tropical = {"pineapple", "mango", "papaya"}
    ...:
    ...: thisset.update(tropical)
    ...:
    ...: print(thisset)
{'papaya', 'cherry', 'mango', 'pineapple', 'apple', 'banana'}

移除项

要删除集合中的项目,请使用remove()discard()方法。

💡注意:移除元素的时候,如果元素不存在,就会报错哦

In [14]: thisset = {"apple", "banana", "cherry"}
    ...:
    ...: thisset.remove("banana")
    ...:
    ...: print(thisset)
{'apple', 'cherry'}

In [15]: thisset.remove("as")  # 不存在就会报错
---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
Input In [15], in <cell line: 1>()
----> 1 thisset.remove("as")

KeyError: 'as'

使用ddiscard移除元素

In [16]: thisset.discard("apple")

In [17]: thisset
Out[17]: {'cherry'}

pop弹出

您也可以使用该pop()方法删除一个项目,但此方法将删除最后一个项目。请记住,集合是无序的,因此您将不知道要删除的项目。

pop()方法的返回值是被移除的项目。

In [18]: thisset = {"apple", "banana", "cherry"}

In [19]: thisset.pop()
Out[19]: 'apple'

In [20]: thisset
Out[20]: {'banana', 'cherry'}

不知道弹出哪个,感觉用处不大

chear()清除

In [21]: thisset.clear()

In [22]: thisset
Out[22]: set()

del删除

del thisset

循环集合

循环集合用for + in来遍历集合元素

In [26]: thisset = {"apple", "banana", "cherry"}
    ...:
    ...: for x in thisset:
    ...:   print(x)
    ...:
apple
banana
cherry

连接集合

集合是不可以直接相加

In [30]: set1
Out[30]: {'a', 'b', 'c'}

In [31]: set2
Out[31]: {1, 2, 3}

In [32]: set1 + set2
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Input In [32], in <cell line: 1>()
----> 1 set1 + set2

TypeError: unsupported operand type(s) for +: 'set' and 'set'

In [33]: set1 + set2

在 Python 中有几种方法可以连接两个或多个集合。

您可以使用union()返回包含两个集合中所有项目的新集合的方法,或将一个集合中的所有项目update()插入另一个集合的方法:

In [27]: # 该union()方法返回一个新集合,其中包含两个集合中的所有项目:
    ...: set1 = {"a", "b" , "c"}
    ...: set2 = {1, 2, 3}
    ...:
    ...: set3 = set1.union(set2)
    ...: print(set3)
{1, 2, 3, 'b', 'a', 'c'}

update()方法将 set2 中的项目插入到 set1 中:

set1 = {"a", "b" , "c"}
set2 = {1, 2, 3}

set1.update(set2)
print(set1)

只保留重复项

intersection_update()方法将仅保留两个集合中都存在的项目。

x保留 set和 set中存在的项目y

In [34]: x = {"apple", "banana", "cherry"}
    ...: y = {"google", "microsoft", "apple"}
    ...:
    ...: x.intersection_update(y)
    ...:
    ...: print(x)
{'apple'}

intersection()方法将返回一个集合,该集合仅包含两个集合中都存在的项目。

返回一个集合,其中包含存在于 setx和 set中的项目y

保留所有,但不保留重复项

symmetric_difference_update()方法将仅保留两个集合中都不存在的元素。

保留两组中都不存在的项目:

In [35]: x = {"apple", "banana", "cherry"}
    ...: y = {"google", "microsoft", "apple"}
    ...:
    ...: x.symmetric_difference_update(y)
    ...:
    ...: print(x)
{'banana', 'microsoft', 'cherry', 'google'}

symmetric_difference()方法将返回一个新集合,其中仅包含两个集合中都不存在的元素。

END 链接