❤️💕💕
python
是一种动态的解释形语言,由于python
的普遍性,学会python
能更快的解决问题,以及学习其他的知识。Myblog:http:https://nsddd.top
[TOC]
myset = {"apple", "banana", "cherry"}
集合用于将多个项目存储在单个变量中。
集合是无序、不可更改和未索引的集合。
💡特点:
- 无序
- 不可更改
- 不允许重复
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()
方法的返回值是被移除的项目。
In [18]: thisset = {"apple", "banana", "cherry"}
In [19]: thisset.pop()
Out[19]: 'apple'
In [20]: thisset
Out[20]: {'banana', 'cherry'}
不知道弹出哪个,感觉用处不大
In [21]: thisset.clear()
In [22]: thisset
Out[22]: set()
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()
方法将返回一个新集合,其中仅包含两个集合中都不存在的元素。
-
✴️版权声明 © :本书所有内容遵循CC-BY-SA 3.0协议(署名-相同方式共享)©