今天学习集合方面的东西 先对之前学过的数据类型做一个归纳
·能够进行索引的:list/str,其中的元素可以重复 ·可变的,如List/Dict,其中的元素或键值对可以进行原地修改 ·不可变的,str/int,其中的元素不能进行原地修改 ·无索引序列的,如dict,其中的元素没有排列顺序,以key查找value
1.1创建集合语法 什么是集合? 高中数学中想必学过集合,集合具有三大特性,确定性,互异性(元素是唯一的),无序性 如果说tuple是str和list的组合,那么set就是list和dict的组合。 集合的特点 可以用一对花括号{ }来进行定义 其中的元素是没有序列的,即非序列类的数据 集合中的元素不可重复,类似于dict中的key,不可以重复 开始创建集合 方法一,通过引用创建
[root@oracle ~]# python Python 2.6.6 (r266:84292, Jan 22 2014, 09:42:36) [GCC 4.4.7 20120313 (Red Hat 4.4.7-4)] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> tset = set("https://www.baidu.com") >>> tset set(['a', 'o', 'c', 'b', 'd', 'i', 'h', 'm', '/', '.', 'p', 's', 'u', 't', 'w', ':']) >>>如果在创建集合的过程中,发现了有重复的元素,那么就会自动过滤,剩下不重复的。而且,我们也可以看得到,集合的次序是没有排列次序的。这说明集合中的元素是没有序列的。 方法二、通过 { }来创建
>>> kset = {"https://www.baidu.com","baidu"} >>> kset set(['baidu', 'https://www.baidu.com']) >>>我们发现创建集合的方法有两种,但是并不提倡使用第二种方法进行创建集合,因为Python在某些情况下是分不清是字典还是集合 比如:
>>> test = {"google",[1,2,'baidu'],{"name":"baidu","website":"https://www.baidu.com"},123421} Traceback (most recent call last): File "<pyshell#2>", line 1, in <module> test = {"google",[1,2,'baidu'],{"name":"baidu","website":"https://www.baidu.com"},123421} TypeError: unhashable type: 'list' >>> demo = {"google",[1,2],123} Traceback (most recent call last): File "<pyshell#3>", line 1, in <module> demo = {"google",[1,2],123} TypeError: unhashable type: 'list' >>>看到里面的错误了吧,通过{ }无法创建含有列表或者字典类型对象元素的集合 因为列表和字典能够进行原地修改,而字符串是不可进行原地修改的,所以会报错,说unhashable,就这样简单的介绍一下就可以了。 另外一种情况解释
>>> tset set(['a', 'o', 'c', 'b', 'd', 'i', 'h', 'm', '/', '.', 'p', 's', 'u', 't', 'w', ':']) >>> tset[1] = "y" Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: 'set' object does not support item assignment >>>说明集合不能够像列表和字典那样可以进行原地修改
集合和列表之间的转换
从集合转换为列表
>>> tset set(['a', 'o', 'c', 'b', 'd', 'i', 'h', 'm', '/', '.', 'p', 's', 'u', 't', 'w', ':']) >>> lset = list(tset) >>> lset ['a', 'o', 'c', 'b', 'd', 'i', 'h', 'm', '/', '.', 'p', 's', 'u', 't', 'w', ':'] >>> lset[1] = "y" >>> lset ['a', 'y', 'c', 'b', 'd', 'i', 'h', 'm', '/', '.', 'p', 's', 'u', 't', 'w', ':'] >>>我们发现,当我们将集合转换为列表之后,可以通过索引值对其进行原地修改了。 从列表转换为集合
>>> listest = ["name","https","www","baidu",".","com"] >>> listest ['name', 'https', 'www', 'baidu', '.', 'com'] >>> listest[3] = "google" >>> listest ['name', 'https', 'www', 'google', '.', 'com'] >>> setest = set(listest) >>> setest set(['www', 'google', 'name', '.', 'https', 'com']) >>> setest[3] = "baidu" Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: 'set' object does not support item assignment >>>找出元素中的所有函数
dir(set) [‘and‘, ‘class‘, ‘cmp‘, ‘contains‘, ‘delattr‘, ‘doc‘, ‘eq‘, ‘format‘, ‘ge‘, ‘getattribute‘, ‘gt‘, ‘hash‘, ‘iand‘, ‘init‘, ‘ior‘, ‘isub‘, ‘iter‘, ‘ixor‘, ‘le‘, ‘len‘, ‘lt‘, ‘ne‘, ‘new‘, ‘or‘, ‘rand‘, ‘reduce‘, ‘reduce_ex‘, ‘repr‘, ‘ror‘, ‘rsub‘, ‘rxor‘, ‘setattr‘, ‘sizeof‘, ‘str‘, ‘sub‘, ‘subclasshook‘, ‘xor‘, ‘add’, ‘clear’, ‘copy’, ‘difference’, ‘difference_update’, ‘discard’, ‘intersection’, ‘intersection_update’, ‘isdisjoint’, ‘issubset’, ‘issuperset’, ‘pop’, ‘remove’, ‘symmetric_difference’, ‘symmetric_difference_update’, ‘union’, ‘update’]
add函数 直接上代码,废话不多说
>>> tset = {} >>> tset = { } >>> tset.add("https") Traceback (most recent call last): File "<stdin>", line 1, in <module> AttributeError: 'dict' object has no attribute 'add' >>> type(tset) <type 'dict'> >>>上述代码中,以为能够使用{ }创建集合,发现被默认为dict类型 只能使用创建集合方法中的一种了
>>> tset = {"https","baidu"} >>> tset set(['baidu', 'https']) >>> type(tset) <type 'set'> >>> tset.add(".com") >>> tset set(['baidu', '.com', 'https']) >>> tset.add("google") >>> tset set(['baidu', 'google', '.com', 'https']) >>>发现在集合中增加一个元素的次序是不定的 注意,假设我们再集合中增加一个列表类型的元素可不可以呢?
>>> tset.add(['name','age','gender']) Traceback (most recent call last): File "<pyshell#15>", line 1, in <module> tset.add(['name','age','gender']) TypeError: unhashable type: 'list'我们发现不可以,但是可以使用下面的方法,让列表变为字符串
' >>> tset.add("['name','age','gender']") >>> tset set(['baidu', "['name','age','gender']", 'google', '.com', 'https']) >>>update函数
>>> tset set(['baidu', "['name','age','gender']", 'google', '.com', 'https']) >>> tset1 = {"this","is","page"} >>> tset.update(tset1) >>> tset set(['baidu', "['name','age','gender']", 'google', 'this', 'is', '.com', 'https', 'page']) >>>pop函数
help(set.pop) Help on built-in function pop:
pop(…) Remove and return an arbitrary set element. Raises KeyError if the set is empty.
解释:删除并返回集合中的任意一个元素,如果集合是空集,就会引发KeyError错误
>>> tset set(['baidu', "['name','age','gender']", 'google', 'this', 'is', '.com', 'https', 'page']) >>> tset.pop() 'baidu' >>> tset.pop() "['name','age','gender']" >>> tset set(['google', 'this', 'is', '.com', 'https', 'page']) >>> s = set("") #是一个空集,所以在使用Pop函数的时候就会引发KeyError >>> s set([]) >>> s.pop() Traceback (most recent call last): File "<stdin>", line 1, in <module> KeyError: 'pop from an empty set' >>>思考:是否可以删除指定元素的值呢?
>>> tset set(['baidu', "['name','age','gender']", 'google', 'this', 'is', '.com', 'https', 'page']) >>> tset.pop("baidu") Traceback (most recent call last): File "<pyshell#43>", line 1, in <module> tset.pop("baidu") TypeError: pop() takes no arguments (1 given) >>>发现是不行的,在对集合进行操作的时候,pop函数只能使用无参形式,随机进行删除,思考,那么有没有可以删除集合中指定的元素的函数呢? 答案是有的 那就是remove函数 remove函数
help(set.remove) Help on built-in function remove:
remove(…) Remove an element from a set; it must be a member.
If the element is not a member, raise a KeyError.解释:从集合中删除一个元素,但必须指定成员,即参数,如果元素中没有指定参数,那么就会引发KeyError错误
>>> tset set(['baidu', "['name','age','gender']", 'google', 'this', 'is', '.com', 'https', 'page']) >>> tset.remove("baidu") >>> tset set(["['name','age','gender']", 'google', 'this', 'is', '.com', 'https', 'page']) >>> tset.remove("python") Traceback (most recent call last): File "<pyshell#54>", line 1, in <module> tset.remove("python") KeyError: 'python' >>>discard函数
help(set.discard) Help on built-in function discard:
discard(…) Remove an element from a set if it is a member.
If the element is not a member, do nothing.解释:也是从集合中删除一个元素,需要注意的是,它是如果删除的元素是集合中的成员,那么就会删除,如果删除的元素不存在于集合中,将什么都不会做
>>> tset set(["['name','age','gender']", 'google', 'this', 'is', '.com', 'https', 'page']) >>> tset.discard("this") >>> tset set(["['name','age','gender']", 'google', 'is', '.com', 'https', 'page']) >>> tset.discard("baidu") >>> tset set(["['name','age','gender']", 'google', 'is', '.com', 'https', 'page']) >>>总结 在对集合中的元素进行删除操作的时候,有三种方法,但是每一种方法又有自己的特点,我们对比一下,有没有发现,当我们在学习的时候,不断的作对比才是最有意思的,就像新闻一样,对比看才会觉得有意思。 1、pop函数,随机删除集合中的一个元素,不能含有参数,如果集合是一个空集就会报错 2、remove函数,从集合中删除指定的元素,如果元素不存在于集合中就会报错 3、discard韩式,从集合中删除一个元素,如果元素不存在,将什么都不会做 当然,还有一种删除集合的方法,那就是clear,将元素扫地出门,我们一起来试试 clear函数
>>> tset set(['baidu', "['name','age','gender']", 'google', 'is', '.com', 'https', 'page']) >>> tset.clear() >>> tset set([]) >>>发先清除完了。
今天学习的是集合和集合中的函数,Python基础就算是告一段落了,接下来学习的是语句和文件操作。 明天更新运算符和简单的语句。敬请期待。