python set集合使用

描述

set() 函数创建一个无序不重复元素集,可进行关系测试,删除重复数据,还可以计算交集、差集、并集等。

语法

set 语法:

class set([iterable])

参数说明:

iterable – 可迭代对象对象;
返回值
返回新的集合对象。

实例

以下实例展示了 set 的使用方法:

>>>x = set('runoob')
>>> y = set('google')
>>> x, y
(set(['b', 'r', 'u', 'o', 'n']), set(['e', 'o', 'g', 'l']))   # 重复的被删除
>>> x & y         # 交集
set(['o'])
>>> x | y         # 并集
set(['b', 'e', 'g', 'l', 'o', 'n', 'r', 'u'])
>>> x - y         # 差集
set(['r', 'b', 'u', 'n'])
>>>

方法

   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.
import numpy as np
l = [1,2,3,4]
lr = np.array(l)
lset = set(lr)
lset.add(5)
lset
Out[27]: {1, 2, 3, 4, 5}

注意numpy.ndarray只有一维的时候能直接set,多维会报错

arr
Out[16]: 
array([[1, 2, 3],
       [4, 5, 6]])
set(arr)
Traceback (most recent call last):

  File "<ipython-input-17-ba21c71e79f9>", line 1, in <module>
    set(arr)

TypeError: unhashable type: 'numpy.ndarray'

lset.discard(1);lset
Out[28]: {2, 3, 4, 5}

lset.pop();lset #这里pop帮助文档里说是任意删除一个元素,但是发现会从左开始删除
Out[29]: {3, 4, 5}

lset = {1, 2, 3, 4, 5}
lset.discard(0);lset
Out[31]: {1, 2, 3, 4, 5}

lset.discard(1)
lset.pop()
Out[33]: 2

lset
Out[34]: {3, 4, 5}

lset.pop()
Out[35]: 3

lset.pop()
Out[36]: 4

还有discard和remove都是移除,但是discard可以包含不存在的元素,remove的参数必须是set中存在的元素。

猜你喜欢

转载自blog.csdn.net/qq_21567767/article/details/82344709
今日推荐