在Python中, 设置 是一个无序的数据类型集合,它是可编辑的、可变的,并且没有重复的元素。集合中元素的顺序是未定义的,尽管它可能由各种元素组成。
与列表相比,使用集合的主要优点是,它有一种高度优化的方法来检查集合中是否包含特定元素。
创建一个集合
可以使用内置的 set() 通过将序列放在大括号内,用逗号分隔,可以使用iterable对象或序列执行函数。
注—— 集合不能有列表或字典之类的可变元素,因为它是可变的。
Python3
# Python program to demonstrate # Creation of Set in Python # Creating a Set set1 = set () print ( "Initial blank Set: " ) print (set1) # Creating a Set with # the use of a String set1 = set ( "GeeksForGeeks" ) print ( "Set with the use of String: " ) print (set1) # Creating a Set with # the use of Constructor # (Using object to Store String) String = 'GeeksForGeeks' set1 = set (String) print ( "Set with the use of an Object: " ) print (set1) # Creating a Set with # the use of a List set1 = set ([ "Geeks" , "For" , "Geeks" ]) print ( "Set with the use of List: " ) print (set1) |
输出:
Initial blank Set: set() Set with the use of String: {'e', 'r', 'k', 'o', 'G', 's', 'F'} Set with the use of an Object: {'r', 'o', 'e', 'F', 's', 'k', 'G'} Set with the use of List: {'Geeks', 'For'}
集合只包含唯一的元素,但在创建集合时,也可以传递多个重复值。集合中元素的顺序未定义且不可更改。集合中元素的类型不必相同,也可以将各种混合的数据类型值传递给集合。
Python3
# Creating a Set with # a List of Numbers # (Having duplicate values) set1 = set ([ 1 , 2 , 4 , 4 , 3 , 3 , 3 , 6 , 5 ]) print ( "Set with the use of Numbers: " ) print (set1) # Creating a Set with # a mixed type of values # (Having numbers and strings) set1 = set ([ 1 , 2 , 'Geeks' , 4 , 'For' , 6 , 'Geeks' ]) print ( "Set with the use of Mixed Values" ) print (set1) |
输出:
Set with the use of Numbers: {1, 2, 3, 4, 5, 6} Set with the use of Mixed Values {1, 2, 4, 'Geeks', 6, 'For'}
向集合中添加元素
使用add()方法
可以使用内置的 添加() 作用使用add()方法一次只能向集合中添加一个元素,使用add()方法一次可以使用循环添加多个元素。
注—— 列表不能作为元素添加到集合中,因为列表不可散列,而元组可以添加,因为元组是不可变的,因此可以散列。
Python3
# Python program to demonstrate # Addition of elements in a Set # Creating a Set set1 = set () print ( "Initial blank Set: " ) print (set1) # Adding element and tuple to the Set set1.add( 8 ) set1.add( 9 ) set1.add(( 6 , 7 )) print ( "Set after Addition of Three elements: " ) print (set1) # Adding elements to the Set # using Iterator for i in range ( 1 , 6 ): set1.add(i) print ( "Set after Addition of elements from 1-5: " ) print (set1) |
输出:
Initial blank Set: set() Set after Addition of Three elements: {8, 9, (6, 7)} Set after Addition of elements from 1-5: {1, 2, 3, (6, 7), 4, 5, 8, 9}
使用update()方法
对于添加两个或多个元素,使用Update()方法。update()方法接受列表、字符串、元组以及其他集合作为其参数。在所有这些情况下,都避免了重复的元素。
Python3
# Python program to demonstrate # Addition of elements in a Set # Addition of elements to the Set # using Update function set1 = set ([ 4 , 5 , ( 6 , 7 )]) set1.update([ 10 , 11 ]) print ( "Set after Addition of elements using Update: " ) print (set1) |
输出:
Set after Addition of elements using Update: {10, 11, 4, 5, (6, 7)}
访问集合
集合项不能通过引用索引来访问,因为集合是无序的,所以集合项没有索引。但您可以使用for循环遍历集合项,或者使用in关键字询问集合中是否存在指定的值。
Python3
# Python program to demonstrate # Accessing of elements in a set # Creating a set set1 = set ([ "Geeks" , "For" , "Geeks" ]) print ( "Initial set" ) print (set1) # Accessing element using # for loop print ( "Elements of set: " ) for i in set1: print (i, end = " " ) # Checking the element # using in keyword print ( "Geeks" in set1) |
输出:
Initial set: {'Geeks', 'For'} Elements of set: Geeks For True
从集合中删除元素
使用remove()方法或discard()方法
可以使用内置的remove()函数从集合中删除元素,但如果集合中不存在元素,则会出现KeyError。要在没有KeyError的情况下从集合中删除元素,请使用discard(),如果该元素在集合中不存在,它将保持不变。
Python3
# Python program to demonstrate # Deletion of elements in a Set # Creating a Set set1 = set ([ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 , 11 , 12 ]) print ( "Initial Set: " ) print (set1) # Removing elements from Set # using Remove() method set1.remove( 5 ) set1.remove( 6 ) print ( "Set after Removal of two elements: " ) print (set1) # Removing elements from Set # using Discard() method set1.discard( 8 ) set1.discard( 9 ) print ( "Set after Discarding two elements: " ) print (set1) # Removing elements from Set # using iterator method for i in range ( 1 , 5 ): set1.remove(i) print ( "Set after Removing a range of elements: " ) print (set1) |
输出:
Initial Set: {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12} Set after Removal of two elements: {1, 2, 3, 4, 7, 8, 9, 10, 11, 12} Set after Discarding two elements: {1, 2, 3, 4, 7, 10, 11, 12} Set after Removing a range of elements: {7, 10, 11, 12}
使用pop()方法
Pop()函数也可用于从集合中移除并返回元素,但它只移除集合中的最后一个元素。 注—— 如果集合是无序的,那么使用pop()函数无法确定要弹出哪个元素。
Python3
# Python program to demonstrate # Deletion of elements in a Set # Creating a Set set1 = set ([ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 , 11 , 12 ]) print ( "Initial Set: " ) print (set1) # Removing element from the # Set using the pop() method set1.pop() print ( "Set after popping an element: " ) print (set1) |
输出:
Initial Set: {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12} Set after popping an element: {2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12}
使用clear()方法
要从集合中删除所有元素,请使用clear()函数。
Python3
#Creating a set set1 = set ([ 1 , 2 , 3 , 4 , 5 ]) print ( " Initial set: " ) print (set1) # Removing all the elements from # Set using clear() method set1.clear() print ( "Set after clearing all the elements: " ) print (set1) |
输出:
Initial set: {1, 2, 3, 4, 5} Set after clearing all the elements: set()
冷冻套装 在Python中,是不可变的对象,它们只支持产生结果的方法和运算符,而不影响应用它们的冻结集。虽然可以随时修改集合的元素,但冻结集合的元素在创建后保持不变。
如果没有传递任何参数,它将返回一个空的frozenset。
Python3
# Python program to demonstrate # working of a FrozenSet # Creating a Set String = ( 'G' , 'e' , 'e' , 'k' , 's' , 'F' , 'o' , 'r' ) Fset1 = frozenset (String) print ( "The FrozenSet is: " ) print (Fset1) # To print Empty Frozen Set # No parameter is passed print ( "Empty FrozenSet: " ) print ( frozenset ()) |
设定方法
作用 | 描述 |
---|---|
添加() | 向集合中添加元素 |
删除() | 从集合中删除元素。如果该元素不在集合中,则引发KeyError |
清除() | 移除集合中的所有元素 |
复印件() | 返回集合的浅层副本 |
pop() | 删除并返回任意集合元素。如果集合为空,则Raise KeyError(升起钥匙错误) |
更新() | 使用自身和其他人的联合更新集合 |
工会() | 返回新集合中集合的并集 |
差异() | 将两个或多个集合的差作为新集合返回 |
差异_更新() | 从此集合中删除另一集合的所有元素 |
丢弃 | 如果元素是成员,则从集合中移除该元素。(如果元素不在集合中,则不执行任何操作) |
交叉口() | 将两个集合的交集作为新集合返回 |
交叉口_更新() | 使用集合自身和另一个集合的交集更新集合 |
isdisjoint() | 如果两个集合有空交集,则返回True |
issubset() | 如果另一个集合包含此集合,则返回True |
issuperset() | 如果此集合包含另一个集合,则返回True |
对称_差() | 将两个集合的对称差作为新集合返回 |
对称_差异_更新() | 使用集本身和另一个集的对称差异更新集 |
最近关于Python集的文章
设置程序
- 程序接受包含所有元音的字符串
- Python程序使用集合在三个列表中查找公共元素
- 在两个列表中查找缺失值和附加值
- 两组完整字符串对
- 检查给定字符串是否为异形
- 集合中的最大值和最小值
- 从集合中删除项目
- Python设置差异以从重复的数组中查找丢失的元素
- 使用计数器的具有不同元素的最小子集数
- 检查两个列表是否至少有一个公共元素
- 使用给定字符串中的集合计算元音数的程序
- 两个列表之间的差异
- Python设置为检查字符串是否为泛型
- Python集合操作(并集、交集、差集和对称差集)
- Python中带有不常见字符的串联字符串
- Python字典,设置和计数器以检查频率是否相同
- 在Python Pangram检查中使用Set()
- 在Python中设置update()以进行n个数组的并集