1."霸氣"引言
讀者可能有疑問了,出了這么多的類型,我也記不住呀殃饿,特別是里面還有不少方法渔嚷。
不要擔(dān)心記不住污茵,你只要記住愛因斯坦說的就好了撵溃。
愛因斯坦在美國演講邢笙,有人問:“你可記得聲音的速度是多少芋齿?你如何記下許多東西腥寇?”
愛因斯坦輕松答道:“聲音的速度是多少,我必須查辭典才能回答觅捆。因為我從來不記在辭
典上已經(jīng)印著的東西赦役,我的記憶力是用來記憶書本上沒有的東西≌こ矗”
多么霸氣的回答掂摔。
這回答不僅僅霸氣术羔,更告訴我們一種方法:只要能夠通過某種方法查找到的,就不需要記
憶乙漓。
所以级历,再多的數(shù)據(jù)類型及其各種方法,都不需要記憶叭披。因為它們都可以通過下述方法但不限
于這些方法查到(這句話的邏輯還是比較嚴密的,包括但不限于...)
交互模式下用dir()或者help()
google(不推薦Xdu,原因自己體會啦)
還有寥殖,如果你經(jīng)常練習(xí),會發(fā)現(xiàn)很多東西自然而然就記住了涩蜘。
2.集合的基礎(chǔ):
# 使用set()創(chuàng)建,重復(fù)的字符"i"
# 集合的元素是不可重復(fù)的
>>> set1=set('qiweisir')
# 字符串被拆分了
>>> set1
{'w', 'i', 'r', 'e', 'q', 's'}
>>> set2=set(['King','King','Jim Green','facebook'])
>>> set2
{'facebook', 'King', 'Jim Green'}
# 沒有index屬性,說明集合沒有索引
>>> dir(set)
['__and__', '__class__', '__contains__', '__delattr__', '__dir__', '__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']
# 不能執(zhí)行索引操作
>>> set2[1]
Traceback (most recent call last):
File "<pyshell#5>", line 1, in <module>
set2[1]
TypeError: 'set' object does not support indexing
# 利用大括號直接創(chuàng)建集合,現(xiàn)在不推薦這種方式了
>>> set3={'King','facebook'}
>>> set
<class 'set'>
- 用大括號創(chuàng)建的集合,不能傳入列表和字典(使用set可以):
>>> set1={'123',[1,2,3],'King'}
Traceback (most recent call last):
File "<pyshell#0>", line 1, in <module>
set1={'123',[1,2,3],'King'}
TypeError: unhashable type: 'list'
>>> set2={'123',{'name':'Jim'},'King'}
Traceback (most recent call last):
File "<pyshell#1>", line 1, in <module>
set2={'123',{'name':'Jim'},'King'}
TypeError: unhashable type: 'dict'
# set()
>>> set1=set(['a','b'])
>>> set1
{'a', 'b'}
>>> set2=set([1,2,3])
>>> set2
{1, 2, 3}
- "哈希":就是不可變的,不能原地修改,比如字符串和元組
"不可哈希":就是可變的,可以原地修改,比如列表和字典
3.set.add()---添加元素,add()傳入的參數(shù)必須是不可變的,比如字符串,數(shù)字;list和dict會哈希報錯
# 這樣是創(chuàng)建空字典...
>>> set1={}
>>> set1.add('King')
Traceback (most recent call last):
File "<pyshell#1>", line 1, in <module>
set1.add('King')
AttributeError: 'dict' object has no attribute 'add'
# 使用set()才能創(chuàng)建空集合
>>> set1=set()
# add()添加的字符串,不再被拆分
>>> set1.add('King')
>>> set1
{'King'}
# 傳入list,依然是錯的
>>> set2.add([1,2,3])
Traceback (most recent call last):
File "<pyshell#7>", line 1, in <module>
set2.add([1,2,3])
TypeError: unhashable type: 'list'
# 小聰明,本質(zhì)是字符串
>>> set2.add('[1,2,3]')
>>> set2
{'p', 't', 'h', 'o', 'y', '[1,2,3]', 'n'}
- set.update()---更新集合的元素
>>> set1=set(['a','b'])
>>> set1
{'a', 'b'}
>>> set2=set([1,2,3])
>>> set2
{1, 2, 3}
>>> set2.update(set1)
>>> set2
{'a', 1, 2, 3, 'b'}
>>> set2.update([4,5,6])
>>> set2
{1, 2, 3, 4, 5, 6, 'b', 'a'}
>>> set2.update('Google')
# 多余的"o"被刪除
>>> set2
{1, 2, 3, 4, 5, 6, 'l', 'b', 'a', 'G', 'o', 'g', 'e'}
- set.pop()---刪除隨機的元素并返回該值
>>> set1=set(['[1,2,3]','h','o','n','p','t','qiwsir','u'])
>>> help(set.pop)
Help on method_descriptor:
pop(...)
Remove and return an arbitrary set element.
Raises KeyError if the set is empty.
>>> set1.pop()
'[1,2,3]'
>>> set1
{'n', 'u', 't', 'o', 'qiwsir', 'h', 'p'}
>>> set1.pop()
'n'
>>> set1
{'u', 't', 'o', 'qiwsir', 'h', 'p'}
>>>
- 刪除指定的元素---set.remove()
>>> help(set.remove)
Help on method_descriptor:
remove(...)
Remove an element from a set; it must be a member.
If the element is not a member, raise a KeyError.
>>> set1=set(['[1,2,3]','h','o','n','p','t','qiwsir','u'])
>>> set1.remove('qiwsir')
>>> set1
{'o', 'h', '[1,2,3]', 'u', 'p', 'n', 't'}
>>> set1.remove('King')
Traceback (most recent call last):
File "<pyshell#4>", line 1, in <module>
set1.remove('King')
KeyError: 'King'
- 類似的用法set.discard()---區(qū)別在于當(dāng)元素不存在,不會報錯
>>> 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.
>>> set1=set(['[1,2,3]','h','o','n','p','t','qiwsir','u'])
>>> set1.discard('qiwsir')
>>> set1
{'u', '[1,2,3]', 'h', 't', 'p', 'n', 'o'}
# 不會報錯
>>> set1.discard('King')
>>> set1
{'u', '[1,2,3]', 'h', 't', 'p', 'n', 'o'}
清空所有---set.clear()
>>> help(set.clear)
Help on method_descriptor:
clear(...)
Remove all elements from this set.
>>> set1=set(['[1,2,3]','h','o','n','p','t','qiwsir','u'])
>>> set1.clear()
>>> set1
set()
>>> print(set1)
set()
4.使用set()創(chuàng)建的集合,都是可以原地修改的,或者說"非哈希"
想創(chuàng)建不可修改的集合---frozenset()
>>> set1=frozenset([1,2,3])
>>> set1
frozenset({1, 2, 3})
>>> set1.add('King')
Traceback (most recent call last):
File "<pyshell#3>", line 1, in <module>
set1.add('King')
AttributeError: 'frozenset' object has no attribute 'add'