Skip to content

Latest commit

 

History

History
45 lines (31 loc) · 712 Bytes

31.md

File metadata and controls

45 lines (31 loc) · 712 Bytes
@author jackzhenguo
@desc 
@date 2019/3/4

31 转为集合类型

返回一个set对象,集合内不允许有重复元素:

In [1]: a = [1,4,2,3,1]

In [2]: set(a)
Out[2]: {1, 2, 3, 4}

In [3]: b = set(a)

In [4]: b.add(5)

In [5]: b
Out[5]: {1, 2, 3, 4, 5}

In [6]: b.pop()
Out[6]: 1

In [7]: b
Out[7]: {2, 3, 4, 5}

In [8]: b.pop()
Out[8]: 2

In [9]: b
Out[9]: {3, 4, 5}

# 注意pop删除集合内任意一个元素
In [10]: help(b.pop)
Help on built-in function pop:

pop(...) method of builtins.set instance
    Remove and return an arbitrary set element.
    Raises KeyError if the set is empty.
[上一个例子](30.md) [下一个例子](32.md)