Python10python内置数据结构之集合
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Python10python内置数据结构之集合相关的知识,希望对你有一定的参考价值。
一、集合
1、集合的定义
In [74]: s = {} In [74]: s = {} # 空大括号是空的字典 In [75]: type(s) Out[75]: dict In [77]: type(s) Out[77]: set In [78]: help(set) Help on class set in module builtins: class set(object) | set() -> new empty set object | set(iterable) -> new set object | | Build an unordered collection of unique elements. | | Methods defined here: In [80]: s = set([1, 2]) In [81]: s Out[81]: {1, 2} In [82]: s = set("xxj") In [83]: s Out[83]: {‘j‘, ‘x‘} In [84]: s = {1, 2, 1, 3} In [85]: s Out[85]: {1, 2, 3}
集合是无序的,元素不能重复,元素要能被哈希(hash,不可变)
二、集合的操作
1、增
z## set.add() In [86]: s Out[86]: {1, 2, 3} In [87]: s.add("a") # 原地增加单个元素,元素要可哈希 In [88]: s Out[88]: {1, 2, 3, ‘a‘} In [89]: s.add(3) In [90]: s Out[90]: {1, 2, 3, ‘a‘} In [93]: s.add([1, 2]) --------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-93-2beaf0c16593> in <module>() ----> 1 s.add([1, 2]) TypeError: unhashable type: ‘list‘ In [94]: help(s.add) In [95]: s.add((1, 2)) In [96]: s Out[96]: {(1, 2), 1, 2, 3, ‘a‘} ## set.update() # 原地增加可迭代对象的元素 In [99]: help(s.update) Help on built-in function update: update(...) method of builtins.set instance Update a set with the union of itself and others. In [127]: s = set() In [128]: s Out[128]: set() In [129]: type(s) Out[129]: set In [101]: s.update(10) ----------------------------------------------------------------------- TypeError Traceback (most recent call l <ipython-input-101-c184888ad9c5> in <module>() ----> 1 s.update(10) TypeError: ‘int‘ object is not iterable In [131]: s.update(["a"]) In [132]: s Out[132]: {‘a‘} In [133]: s.update(["a"], ["b"]) In [134]: s Out[134]: {‘a‘, ‘b‘} In [135]: s.update(["a"], ["b"], 1) ----------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-135-fc556b8d9726> in <module>() ----> 1 s.update(["a"], ["b"], 1) TypeError: ‘int‘ object is not iterable In [136]: s.update(["a"], ["b"], "xj") In [137]: s Out[137]: {‘a‘, ‘b‘, ‘j‘, ‘x‘} In [139]: s.update([["S", "B"]]) ----------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-139-da563f39a191> in <module>() ----> 1 s.update([["S", "B"]]) TypeError: unhashable type: ‘list‘
2、删
## set.remove() In [142]: s Out[142]: {‘a‘, ‘b‘, ‘j‘, ‘x‘} In [143]: s.remove("a") In [144]: s Out[144]: {‘b‘, ‘j‘, ‘x‘} In [151]: s.remove("S") ----------------------------------------------------------------------- KeyError Traceback (most recent call last) <ipython-input-151-332efdd48daa> in <module>() ----> 1 s.remove("S") KeyError: ‘S‘ ## set.pop() In [153]: s = {1, 2, 3, 4} In [154]: s.pop() Out[154]: 1 In [155]: s Out[155]: {2, 3, 4} In [156]: s.pop(5) ----------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-156-23a1c03efc29> in <module>() ----> 1 s.pop(5) TypeError: pop() takes no arguments (1 given) In [157]: s.pop() Out[157]: 2 In [158]: s.pop() Out[158]: 3 In [159]: s.pop() Out[159]: 4 In [160]: s.pop() ----------------------------------------------------------------------- KeyError Traceback (most recent call last) <ipython-input-160-e76f41daca5e> in <module>() ----> 1 s.pop() KeyError: ‘pop from an empty set‘ ## set.discard() In [165]: help(set.discard) Help on method_descriptor: discard(...) Remove an element from a set if it is a member. If the element is not a member, do nothing. In [166]: s = {1, 2, 3} In [167]: s.discard(2) In [168]: s.discard(1, 3) ----------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-168-8702b734cbc4> in <module>() ----> 1 s.discard(1, 3) TypeError: discard() takes exactly one argument (2 given) In [169]: s.discard(2) # 元素不存在时,不会报错 In [170]: s Out[170]: {1, 3} In [32]: s.clear() In [33]: s Out[33]: set() In [47]: del(s) In [48]: s ----------------------------------------------------------------------- NameError Traceback (most recent call last) <ipython-input-48-f4d5d0c0671b> in <module>() ----> 1 s NameError: name ‘s‘ is not defined
小结:
remove 删除给定的元素,元素不存在时,抛出KeyError
discard 删除给定的元素,元素不存在时,什么也不做
pop 随机删除一个元素并返回,集合为空返回KeyError,
clear 清空集合
3、改
set不能修改单个元素
4、查找
集合不能通过索引,集合不是线性结构,没有索引
集合没有访问单个元素的方法
集合没有查找的方法
做成员运算(in和not in)的时候,set的效率远高于list(O(1)和O(n));
O(n)不一定小于O(1),还需要看数据规模
三、集合的集合操作
以上是关于Python10python内置数据结构之集合的主要内容,如果未能解决你的问题,请参考以下文章
what's the python之基本运算符及字符串列表元祖集合字典的内置方法