爱因斯坦在美国演讲,有人问:“你可记得声音的速度是多少?你如何记下许多东西?” 爱因斯坦轻松答道:“声音的速度是多少,我必须查辞典才能回答。因为我从来不记在辞典上已经印着的东西,我的记忆力是用来记忆书本上没有的东西。”
>>> s1 = set("qiwsir") #把str中的字符拆解开,形成set.特别注意观察:qiwsir中有两个i
>>> s1 #但是在s1中,只有一个i,也就是不能重复
set(['q', 'i', 's', 'r', 'w'])
>>> s2 = set([123,"google","face","book","facebook","book"]) #通过list创建set.不能有重复,元素可以是int/str
>>> s2
set(['facebook', 123, 'google', 'book', 'face']) #元素顺序排列不是按照指定顺序
>>> s3 = {"facebook",123} #通过{}直接创建
>>> s3
set([123, 'facebook'])
>>> s3 = {"facebook",[1,2,'a'],{"name":"python","lang":"english"},123}
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unhashable type: 'dict'
>>> s3 = {"facebook",[1,2],123}
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unhashable type: 'list'
>>> s1 set(['q', 'i', 's', 'r', 'w']) >>> s1[1] = "I" Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: 'set' object does not support item assignment >>> s1 set(['q', 'i', 's', 'r', 'w']) >>> lst = list(s1) >>> lst ['q', 'i', 's', 'r', 'w'] >>> lst[1] = "I" >>> lst ['q', 'I', 's', 'r', 'w']
>>> 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']
>>> help(set.add) Help on method_descriptor: add(...) Add an element to a set. This has no effect if the element is already present.
>>> a_set = {} #我想当然地认为这样也可以建立一个set
>>> a_set.add("qiwsir") #报错.看看错误信息,居然告诉我dict没有add.我分明建立的是set呀.
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'dict' object has no attribute 'add'
>>> type(a_set) #type之后发现,计算机认为我建立的是一个dict
<type 'dict'>
>>> a_set = {'a','i'} #这回就是set了吧
>>> type(a_set)
<type 'set'> #果然
>>> a_set.add("qiwsir") #增加一个元素
>>> a_set #原处修改,即原来的a_set引用对象已经改变
set(['i', 'a', 'qiwsir'])
>>> b_set = set("python")
>>> type(b_set)
<type 'set'>
>>> b_set
set(['h', 'o', 'n', 'p', 't', 'y'])
>>> b_set.add("qiwsir")
>>> b_set
set(['h', 'o', 'n', 'p', 't', 'qiwsir', 'y'])
>>> b_set.add([1,2,3]) #这样做是不行滴,跟前面一样,报错.
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unhashable type: 'list'
>>> b_set.add('[1,2,3]') #可以这样!
>>> b_set
set(['[1,2,3]', 'h', 'o', 'n', 'p', 't', 'qiwsir', 'y'])
>>> help(set.update) update(...) Update a set with the union of itself and others. >>> s1 set(['a', 'b']) >>> s2 set(['github', 'qiwsir']) >>> s1.update(s2) #把s2的元素并入到s1中. >>> s1 #s1的引用对象修改 set(['a', 'qiwsir', 'b', 'github']) >>> s2 #s2的未变 set(['github', 'qiwsir'])
>>> help(set.pop)
pop(...)
Remove and return an arbitrary set element.
Raises KeyError if the set is empty.
>>> b_set
set(['[1,2,3]', 'h', 'o', 'n', 'p', 't', 'qiwsir', 'y'])
>>> b_set.pop() #从set中任意选一个删除,并返回该值
'[1,2,3]'
>>> b_set.pop()
'h'
>>> b_set.pop()
'o'
>>> b_set
set(['n', 'p', 't', 'qiwsir', 'y'])
>>> b_set.pop("n") #如果要指定删除某个元素,报错了.
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: pop() takes no arguments (1 given)
>>> help(set.remove)
remove(...)
Remove an element from a set; it must be a member.
If the element is not a member, raise a KeyError.
set.remove(obj)中的obj,必须是set中的元素,否则就报错.试一试:
>>> a_set
set(['i', 'a', 'qiwsir'])
>>> a_set.remove("i")
>>> a_set
set(['a', 'qiwsir'])
>>> a_set.remove("w")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
KeyError: 'w'
>>> a_set.discard('a')
>>> a_set
set(['qiwsir'])
>>> a_set.discard('b')
>>>
>>> a_set set(['qiwsir']) >>> a_set.clear() >>> a_set set([]) >>> bool(a_set) #空了,bool一下返回False. False
在计算机科学中,集合是一组可变数量的数据项(也可能是0个)的组合,这些数据项可能共享某些特征,需要以某种操作方式一起进行操作。一般来讲,这些数据项的类型是相同的,或基类相同(若使用的语言支持继承)。列表(或数组)通常不被认为是集合,因为其大小固定,但事实上它常常在实现中作为某些形式的集合使用。 集合的种类包括列表,集,多重集,树和图。枚举类型可以是列表或集。
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有