python中集合类型创建、更新、删除、修改以及操作符使用实例代码

来源:互联网 发布:无线网址域名注册 编辑:程序博客网 时间:2024/05/16 11:00
#coding=utf8'''python中的集合对象是一组无序排列的可哈希的值。集合成员可以做字典中的键。集合支持用in和not in操作符检查成员,由len()内建函数得到集合的大小,用for循环迭代集合的成员。集合本身是无序的,不可以为集合创建索引或执行切片操作,也没有键可用来获取集合中的值。集合有两种不同类型:可变集合(set)和不可变集合(frozenset).对于可变集合,可以添加和删除元素;对于不可变集合只能读取元素,不能进行修改,可变集合不是可哈希的,因此不能用做字典的键也不能做其他集合中的元素。不可变集合它们有哈希值,能被用做字典的值或作为集合中的一个成员。集合操作符:---------------------------------------------------------------------------python符号                                        说明in                                            是...的成员not in                                    不是...的成员==                                            等于!=                                            不等于<                                            是...的(严格)子集<=                                         是...的子集(包括非严格子集)>                                            是...的(严格)超集>=                                        是...的超集(包括非严格超集)&                                          交集|                                            合集-                                            差补或相对补集^                                            对称差分---------------------------------------------------------------------------集合与列表和字典不同,没有特别的语法格式。列表和字典可以分别用工厂方法list()和dict()创建。集合被创建的方法只能通过工厂方法set()与frozenset()进行创建。可以通过遍历查看集合成员或检查某项元素是否是一个集合中的成员。用各种集合内建的方法和操作符添加和删除集合的成员:add():在集合中添加元素update():更新集合中的元素remove():删除集合的成员以上方法使用与可变集合,也就是通过set()创建的集合使用del删除集合的命令空间,如果引用计数为零,则被垃圾回收。'''#调用set()创建可变集合from __builtin__ import set, frozensetfrom _abcoll import Setset1=set("hello")#调用frozenset()创建不可变集合frozenset1=frozenset("world")#调用type函数print "The type of set1 :",type(set1)print  "The type of frozenset1 :",type(frozenset1)#判断两个集合是否相等if set1==set("hello"):    print set1if set1==frozenset1:    print frozenset1else:    print set1    print frozenset1    #检查某项元素是否上集合中的成员if 'w'  in frozenset1:    print "w"if "g"  not in set1:    print "g"#遍历查看集合成员#相同的元素只打印第一次出现的for el in set1:    print el, #元素一行打印print  for el in frozenset1:    print el, #元素一行打印#调用add函数给可变集合添加元素set1.add("demo")print "add demo to set1 :",set1#调用update函数更新集合#把dema中set1中不存在的元素放入set1中set1.update("dema")print "update  set1 :",set1#调用remove删除集合中某一项set1.remove("demo")print "remove demo from set1 :",set1#使用差集或相对补集操作符(-)删除元素print "before set1", set1set1 -=set("demo")print "remove d e m o from set1 :",set1#适用所有集合类型#使用交集符号(&)求两个集合的公共部分intersection=set1 & set("hello")print "intersection is :",intersection#适用所有集合类型#使用合集符号(|)求两个集合的合集collection=set1 | set("god")print "collection is :",collection# 子集判断适用于所有的集合类型if set("hello")<set("hello world"):    print set("hello")if set("hello")<=set("hello"):    print set("hello")    if set("hello world")>set("hello"):    print set("hello world")if set("hello world")>=set("hello world"):    print set("hello world")#适用所有集合类型#对称差分,把两个集合中不相同的部分构成一个集合ymmetricalDifference=set("hello kitty") ^ set("hello world")print "ymmetrical difference :",ymmetricalDifference     #适用可变集合集合类型#把set1中的元素更新为交集部分的元素set1=set("hello")set1 &=set("ellen")print "update set1 value :",set1#把set2中的元素更新为差集部分的元素set2=set("hello")set2 -=set("ellen")print "update set2 value :",set2#把set3中的元素更新为对称差分的元素set3=set("hello")set3 ^=set("ellen")print "update set3 value :",set3

0 0
原创粉丝点击