1.6 字典中的鍵映射多個值

1.6 字典中的鍵映射多個值

問題

實現一個字典結構,一個鍵可以對應多個值

解決方案

1.列表和集合

1.>>> d
2.{'b': [4, 5], 'a': [1, 2, 3]}
3.>>> d['b']
4.[4, 5]
5.>>> e={'a':{1,2,2,3},'b':{2,3,4,4}}
6.>>> e
7.{'b': {2, 3, 4}, 'a': {1, 2, 3}}
8.>>> d['b'][1]
9.5

2.使用defaultdict

1.>>> from collections import defaultdict
2.>>> d = defaultdict(list)
3.>>> d
4.defaultdict(<class 'list'>, {})
5.>>> d['a'].append(1)
6.>>> d['a'].append(2)
7.>>> d['a'].append(5)
8.>>> d
9.defaultdict(<class 'list'>, {'a': [1, 2, 5]})
10.>>> d2 = defaultdict(set)
11.>>> d2['a'].add(1)
12.>>> d2['a'].add(1)
13.>>> d2['a'].add(2)
14.>>> d2
15.defaultdict(<class 'set'>, {'a': {1, 2}})

知識補充

set,集合

  1. python的set與數學概念上的集合是高度匹配的. 代表一個無序,無重複的元素集.數學集合上的概念在python的set數據類型中都有體現. 比如,支持合集,交集,差集.對稱差集等

  2. 集合支持x in set,len(set),for x in set等操作. 由於集合不記錄位置信息,所以索引,切片等類序列的操作.

  3. 集合有兩種類型,常規的set和frozenset.set類型是可變的,可以通過add()和remove()進行增刪,因此,常規類型的set並沒有辦法計算hash值,所以它不可以做字典的鍵.而frozenset是不可變的,可以計算hash.可以用於做字典的鍵.

  4. 構建一個集合:{'jack','tim'}或者s=set(['jack','tim'])

    1.class set([iterable]) :
    2.
    3.class frozenset([iterable]):
    4.
    5.
    6.#Return a new set or frozenset object whose elements are taken from iterable. The elements of a set must be hashable. To represent sets of sets, the inner sets must be frozenset objects. If iterable is not specified, a new empty set is returned.
    7.
  5. Instances of set and frozenset provide the following operations:
    • len(s) 返回set長度
    • x in s/x not in s 成員操作符
    • isdisjoint(other) 兩個集合是否不相交. 不相交返回True,相交返回False
    • s1<=s2,s1是s2的子集
    • s1<s2,s1是s2的真子集
    • issubset(other)
    • issuperset(other)
    • union(other, …) Return a new set with elements from the set and all others.
      • set | other | …
    • intersection(other, …) Return a new set with elements common to the set and all others.
      • set & other & …
    • difference(other, …) Return a new set with elements in the set that are not in the others.
      • set - other - …
    • symmetric_difference(other) Return a new set with elements in either the set or other but not both.
      • set ^ other
    • copy() Return a new set with a shallow copy of s.
  6. 一個set和一個frozenset做union的時候,會返回一個frozenset
  7. 只適用於set不使用於frozenset的方法:
1.update(other, ...) 
2.set |= other | ...
3.#Update the set, adding elements from all others.
4.intersection_update(other, ...)
5.set &= other & ...
6.#Update the set, keeping only elements found in it and all others.
7.difference_update(other, ...)
8.set -= other | ...
9.#Update the set, removing elements found in others.
10.symmetric_difference_update(other)
11.set ^= other
12.#Update the set, keeping only elements found in either set, but not in both.
13.add(elem)
14.#Add element elem to the set.
15.remove(elem)
16.#Remove element elem from the set. Raises KeyError if elem is not contained in the set.
17.discard(elem)
18.#Remove element elem from the set if it is present.
19.pop()
20.#Remove and return an arbitrary element from the set. Raises KeyError if the set is empty.
21.clear()
22.#Remove all elements from the set.

defaultdict
正常的字典,如果訪問不存在的鍵,就會報KeyError,但是很多時候,我們需要字典中的每個鍵都存在默認值.

1.strings = ('puppy', 'kitten', 'puppy', 'puppy','weasel', 'puppy', 'kitten', 'puppy')
2.counts = {}
3.for kw in strings:
4. counts[kw] += 1

這是一段統計字符出現次數的代碼,但是在python中這段代碼是不可以運行的,會報KeyError.所以可以做一個改進

1.strings = ('puppy', 'kitten', 'puppy', 'puppy','weasel', 'puppy', 'kitten', 'puppy')
2.counts = {}
3.for kw in strings:
4. if kw not in counts:
5. counts[kw] = 1
6. else:
7. counts[kw] += 1

或者可以使用default dict:

1.strings = ('puppy', 'kitten', 'puppy', 'puppy','weasel', 'puppy', 'kitten', 'puppy')
2.counts = {}
3.for kw in strings:
4. counts.setdefault(kw, 0)
5. counts[kw] += 1

setdefault()接受兩個參數,一個鍵的名稱,一個默認值. 還有一種比較好的表達方式,但是不容易理解

1.strings = ('puppy', 'kitten', 'puppy', 'puppy','weasel', 'puppy', 'kitten', 'puppy')
2.counts = {}
3.for kw in strings:
4. counts[kw] = counts.setdefault(kw, 0) + 1

python提供了一個專門的類來處理defaultdict: collections.defaultdict

  1. 初始化,defaultdict()接受一個類型作爲初始化參數.
1.>>> from collections import defaultdict
2.>>> dd=defaultdict(list)
3.>>> dd
4.defaultdict(<class 'list'>, {})
  1. 當訪問的鍵不存在的時候,會自動初始化一個默認值.
1.>>> dd['tim']
2.[]
3.>>> dd['jerry']=26
4.>>> dd
5.defaultdict(<class 'list'>, {'jerry': 26, 'tim': []})
6.>>> dd['jerry']=27
7.>>> dd
8.defaultdict(<class 'list'>, {'jerry': 27, 'tim': []})
9.>>> dd['tim'].append(22)
10.>>> dd
11.defaultdict(<class 'list'>, {'jerry': 27, 'tim': [22]})
12.>>>

這種形式的默認值只有在通過dict[key]或者dict.getitem(key)訪問的時候纔有效

  1. defaultdict除了接受類型作爲參數外,也可以以不帶參數的可調用函數作爲參數.
1.>>> from collections import defaultdict
2.>>> def zero():
3.... return 0
4....
5.>>> dd = defaultdict(zero)
6.>>> dd
7.defaultdict(<function zero at 0xb7ed2684>, {})
8.>>> dd['foo']
9.0
10.>>> dd
11.defaultdict(<function zero at 0xb7ed2684>, {'foo': 0})

來解決最初的單詞統計問題

1.from collections import defaultdict
2.strings = ('puppy', 'kitten', 'puppy', 'puppy','weasel', 'puppy', 'kitten', 'puppy')
3.counts = defaultdict(lambda: 0) # 使用lambda來定義簡單的函數
4.for s in strings:
5. counts[s] += 1
  1. defaultdict類是如何實現的?
    關鍵在__missing__()方法,當是用__getitem__()方法獲取不到值的時候,會調用__missing__()方法獲取默認值.並將該鍵添加到字典中.

在dict的派生子類中,如果訪問的鍵不存在時,d[key]會調用__missing__()方法來獲取默認值. 但是在dict中,並未實現這一方法.

1.>>> class Missing(dict):
2. def __missing__(self,key):
3. return 'missing'
4.>>> d=Missing()
5.>>> d['1']
6.'missing'

通過上面這個例子就可以看出來.__missing__()函數確實生效了.
另外,還可以進一步修改以達到defaultdict的效果:

1.>>> class Defaulting(dict):
2. def __missing__(self,key):
3. self[key]='default'
4. return 'default'
5.>>> dd=Defaulting()
6.>>> dd['1']
7.'default'
8.>>> dd
9.{'1': 'default'}
%231.6%20%u5B57%u5178%u4E2D%u7684%u952E%u6620%u5C04%u591A%u4E2A%u503C%0A@%28python%29%5Bcookbook%7C%u6570%u636E%u7ED3%u6784%u4E0E%u7B97%u6CD5%5D%0A%0A%23%23%u95EE%u9898%0A%u5B9E%u73B0%u4E00%u4E2A%u5B57%u5178%u7ED3%u6784%2C%u4E00%u4E2A%u952E%u53EF%u4EE5%u5BF9%u5E94%u591A%u4E2A%u503C%0A%0A%23%23%u89E3%u51B3%u65B9%u6848%0A**1.%u5217%u8868%u548C%u96C6%u5408**%0A%60%60%60python%0A%3E%3E%3E%20d%0A%7B%27b%27%3A%20%5B4%2C%205%5D%2C%20%27a%27%3A%20%5B1%2C%202%2C%203%5D%7D%0A%3E%3E%3E%20d%5B%27b%27%5D%0A%5B4%2C%205%5D%0A%3E%3E%3E%20e%3D%7B%27a%27%3A%7B1%2C2%2C2%2C3%7D%2C%27b%27%3A%7B2%2C3%2C4%2C4%7D%7D%0A%3E%3E%3E%20e%0A%7B%27b%27%3A%20%7B2%2C%203%2C%204%7D%2C%20%27a%27%3A%20%7B1%2C%202%2C%203%7D%7D%0A%3E%3E%3E%20d%5B%27b%27%5D%5B1%5D%0A5%0A%60%60%60%0A**2.%u4F7F%u7528defaultdict**%0A%60%60%60python%0A%3E%3E%3E%20from%20collections%20import%20defaultdict%0A%3E%3E%3E%20d%20%3D%20defaultdict%28list%29%0A%3E%3E%3E%20d%0Adefaultdict%28%3Cclass%20%27list%27%3E%2C%20%7B%7D%29%0A%3E%3E%3E%20d%5B%27a%27%5D.append%281%29%0A%3E%3E%3E%20d%5B%27a%27%5D.append%282%29%0A%3E%3E%3E%20d%5B%27a%27%5D.append%285%29%0A%3E%3E%3E%20d%0Adefaultdict%28%3Cclass%20%27list%27%3E%2C%20%7B%27a%27%3A%20%5B1%2C%202%2C%205%5D%7D%29%0A%3E%3E%3E%20d2%20%3D%20defaultdict%28set%29%0A%3E%3E%3E%20d2%5B%27a%27%5D.add%281%29%0A%3E%3E%3E%20d2%5B%27a%27%5D.add%281%29%0A%3E%3E%3E%20d2%5B%27a%27%5D.add%282%29%0A%3E%3E%3E%20d2%0Adefaultdict%28%3Cclass%20%27set%27%3E%2C%20%7B%27a%27%3A%20%7B1%2C%202%7D%7D%29%0A%60%60%60%0A%23%23%u77E5%u8BC6%u8865%u5145%0A**set%2C%u96C6%u5408**%0A1.%20python%u7684set%u4E0E%u6570%u5B66%u6982%u5FF5%u4E0A%u7684%u96C6%u5408%u662F%u9AD8%u5EA6%u5339%u914D%u7684.%20%u4EE3%u8868%u4E00%u4E2A%u65E0%u5E8F%2C%u65E0%u91CD%u590D%u7684%u5143%u7D20%u96C6.%u6570%u5B66%u96C6%u5408%u4E0A%u7684%u6982%u5FF5%u5728python%u7684set%u6570%u636E%u7C7B%u578B%u4E2D%u90FD%u6709%u4F53%u73B0.%20%u6BD4%u5982%2C%u652F%u6301%u5408%u96C6%2C%u4EA4%u96C6%2C%u5DEE%u96C6.%u5BF9%u79F0%u5DEE%u96C6%u7B49%0A%0A2.%20%u96C6%u5408%u652F%u6301x%20in%20set%2Clen%28set%29%2Cfor%20x%20in%20set%u7B49%u64CD%u4F5C.%20%u7531%u4E8E%u96C6%u5408%u4E0D%u8BB0%u5F55%u4F4D%u7F6E%u4FE1%u606F%2C%u6240%u4EE5%u7D22%u5F15%2C%u5207%u7247%u7B49%u7C7B%u5E8F%u5217%u7684%u64CD%u4F5C.%20%0A%0A3.%20%u96C6%u5408%u6709%u4E24%u79CD%u7C7B%u578B%2C%u5E38%u89C4%u7684set%u548Cfrozenset.set%u7C7B%u578B%u662F%u53EF%u53D8%u7684%2C%u53EF%u4EE5%u901A%u8FC7add%28%29%u548Cremove%28%29%u8FDB%u884C%u589E%u5220%2C%u56E0%u6B64%2C%u5E38%u89C4%u7C7B%u578B%u7684set%u5E76%u6CA1%u6709%u529E%u6CD5%u8BA1%u7B97hash%u503C%2C%u6240%u4EE5%u5B83%u4E0D%u53EF%u4EE5%u505A%u5B57%u5178%u7684%u952E.%u800Cfrozenset%u662F%u4E0D%u53EF%u53D8%u7684%2C%u53EF%u4EE5%u8BA1%u7B97hash.%u53EF%u4EE5%u7528%u4E8E%u505A%u5B57%u5178%u7684%u952E.%0A%0A4.%20%u6784%u5EFA%u4E00%u4E2A%u96C6%u5408%3A%60%7B%27jack%27%2C%27tim%27%7D%60%u6216%u8005%60s%3Dset%28%5B%27jack%27%2C%27tim%27%5D%29%60%0A%09%60%60%60python%0A%09class%20set%28%5Biterable%5D%29%20%3A%0A%0A%09class%20frozenset%28%5Biterable%5D%29%3A%20%0A%0A%09%23Return%20a%20new%20set%20or%20frozenset%20object%20whose%20elements%20are%20taken%20from%20iterable.%20The%20elements%20of%20a%20set%20must%20be%20hashable.%20To%20represent%20sets%20of%20sets%2C%20the%20inner%20sets%20must%20be%20frozenset%20objects.%20If%20iterable%20is%20not%20specified%2C%20a%20new%20empty%20set%20is%20returned.%0A%09%60%60%60%0A5.%20Instances%20of%20set%20and%20frozenset%20provide%20the%20following%20operations%3A%0A%09-%20len%28s%29%20%u8FD4%u56DEset%u957F%u5EA6%0A%09-%20x%20in%20s/x%20not%20in%20s%20%u6210%u5458%u64CD%u4F5C%u7B26%0A%09-%20isdisjoint%28other%29%20%20%u4E24%u4E2A%u96C6%u5408%u662F%u5426%u4E0D%u76F8%u4EA4.%20%u4E0D%u76F8%u4EA4%u8FD4%u56DETrue%2C%u76F8%u4EA4%u8FD4%u56DEFalse%0A%09-%20s1%3C%3Ds2%2Cs1%u662Fs2%u7684%u5B50%u96C6%0A%09-%20s1%3Cs2%2Cs1%u662Fs2%u7684%u771F%u5B50%u96C6%0A%09-%20issubset%28other%29%20%0A%09-%20issuperset%28other%29%20%0A%09-%20union%28other%2C%20...%29%20Return%20a%20new%20set%20with%20elements%20from%20the%20set%20and%20all%20others.%0A%09%09-%20set%20%7C%20other%20%7C%20...%20%0A%09-%20intersection%28other%2C%20...%29%20Return%20a%20new%20set%20with%20elements%20common%20to%20the%20set%20and%20all%20others.%0A%09%09-%20set%20%26%20other%20%26%20...%20%0A%09-%20difference%28other%2C%20...%29%20Return%20a%20new%20set%20with%20elements%20in%20the%20set%20that%20are%20not%20in%20the%20others.%0A%09%09-%20set%20-%20other%20-%20...%20%0A%09-%20symmetric_difference%28other%29%20Return%20a%20new%20set%20with%20elements%20in%20either%20the%20set%20or%20other%20but%20not%20both.%0A%09%09-%20set%20%5E%20other%20%0A%09-%20copy%28%29%20Return%20a%20new%20set%20with%20a%20shallow%20copy%20of%20s.%0A6.%20%u4E00%u4E2Aset%u548C%u4E00%u4E2Afrozenset%u505Aunion%u7684%u65F6%u5019%2C%u4F1A%u8FD4%u56DE%u4E00%u4E2Afrozenset%0A7.%20%u53EA%u9002%u7528%u4E8Eset%u4E0D%u4F7F%u7528%u4E8Efrozenset%u7684%u65B9%u6CD5%3A%0A%60%60%60python%0Aupdate%28other%2C%20...%29%20%0Aset%20%7C%3D%20other%20%7C%20...%20%0A%23Update%20the%20set%2C%20adding%20elements%20from%20all%20others.%0Aintersection_update%28other%2C%20...%29%20%0Aset%20%26%3D%20other%20%26%20...%20%0A%23Update%20the%20set%2C%20keeping%20only%20elements%20found%20in%20it%20and%20all%20others.%0Adifference_update%28other%2C%20...%29%20%0Aset%20-%3D%20other%20%7C%20...%20%0A%23Update%20the%20set%2C%20removing%20elements%20found%20in%20others.%0Asymmetric_difference_update%28other%29%20%0Aset%20%5E%3D%20other%20%0A%23Update%20the%20set%2C%20keeping%20only%20elements%20found%20in%20either%20set%2C%20but%20not%20in%20both.%0Aadd%28elem%29%20%0A%23Add%20element%20elem%20to%20the%20set.%0Aremove%28elem%29%20%0A%23Remove%20element%20elem%20from%20the%20set.%20Raises%20KeyError%20if%20elem%20is%20not%20contained%20in%20the%20set.%0Adiscard%28elem%29%20%0A%23Remove%20element%20elem%20from%20the%20set%20if%20it%20is%20present.%0Apop%28%29%20%0A%23Remove%20and%20return%20an%20arbitrary%20element%20from%20the%20set.%20Raises%20KeyError%20if%20the%20set%20is%20empty.%0Aclear%28%29%20%0A%23Remove%20all%20elements%20from%20the%20set.%0A%60%60%60%0A%0A**defaultdict**%0A%u6B63%u5E38%u7684%u5B57%u5178%2C%u5982%u679C%u8BBF%u95EE%u4E0D%u5B58%u5728%u7684%u952E%2C%u5C31%u4F1A%u62A5KeyError%2C%u4F46%u662F%u5F88%u591A%u65F6%u5019%2C%u6211%u4EEC%u9700%u8981%u5B57%u5178%u4E2D%u7684%u6BCF%u4E2A%u952E%u90FD%u5B58%u5728%u9ED8%u8BA4%u503C.%20%0A%60%60%60python%0Astrings%20%3D%20%28%27puppy%27%2C%20%27kitten%27%2C%20%27puppy%27%2C%20%27puppy%27%2C%27weasel%27%2C%20%27puppy%27%2C%20%27kitten%27%2C%20%27puppy%27%29%0Acounts%20%3D%20%7B%7D%0Afor%20kw%20in%20strings%3A%0A%20%20%20%20counts%5Bkw%5D%20+%3D%201%0A%60%60%60%0A%u8FD9%u662F%u4E00%u6BB5%u7EDF%u8BA1%u5B57%u7B26%u51FA%u73B0%u6B21%u6570%u7684%u4EE3%u7801%2C%u4F46%u662F%u5728python%u4E2D%u8FD9%u6BB5%u4EE3%u7801%u662F%u4E0D%u53EF%u4EE5%u8FD0%u884C%u7684%2C%u4F1A%u62A5KeyError.%u6240%u4EE5%u53EF%u4EE5%u505A%u4E00%u4E2A%u6539%u8FDB%0A%60%60%60python%0Astrings%20%3D%20%28%27puppy%27%2C%20%27kitten%27%2C%20%27puppy%27%2C%20%27puppy%27%2C%27weasel%27%2C%20%27puppy%27%2C%20%27kitten%27%2C%20%27puppy%27%29%0Acounts%20%3D%20%7B%7D%0Afor%20kw%20in%20strings%3A%0A%20%20%20%20if%20kw%20not%20in%20counts%3A%0A%20%20%20%20%20%20%20%20counts%5Bkw%5D%20%3D%201%0A%20%20%20%20else%3A%0A%20%20%20%20%20%20%20%20counts%5Bkw%5D%20+%3D%201%0A%60%60%60%0A%u6216%u8005%u53EF%u4EE5%u4F7F%u7528default%20dict%3A%0A%60%60%60python%0Astrings%20%3D%20%28%27puppy%27%2C%20%27kitten%27%2C%20%27puppy%27%2C%20%27puppy%27%2C%27weasel%27%2C%20%27puppy%27%2C%20%27kitten%27%2C%20%27puppy%27%29%0Acounts%20%3D%20%7B%7D%0Afor%20kw%20in%20strings%3A%0A%20%20%20%20counts.setdefault%28kw%2C%200%29%0A%20%20%20%20counts%5Bkw%5D%20+%3D%201%0A%60%60%60%0Asetdefault%28%29%u63A5%u53D7%u4E24%u4E2A%u53C2%u6570%2C%u4E00%u4E2A%u952E%u7684%u540D%u79F0%2C%u4E00%u4E2A%u9ED8%u8BA4%u503C.%20%u8FD8%u6709%u4E00%u79CD%u6BD4%u8F83%u597D%u7684%u8868%u8FBE%u65B9%u5F0F%2C%u4F46%u662F%u4E0D%u5BB9%u6613%u7406%u89E3%0A%60%60%60python%0Astrings%20%3D%20%28%27puppy%27%2C%20%27kitten%27%2C%20%27puppy%27%2C%20%27puppy%27%2C%27weasel%27%2C%20%27puppy%27%2C%20%27kitten%27%2C%20%27puppy%27%29%0Acounts%20%3D%20%7B%7D%0Afor%20kw%20in%20strings%3A%0A%20%20%20%20counts%5Bkw%5D%20%3D%20counts.setdefault%28kw%2C%200%29%20+%201%0A%60%60%60%0Apython%u63D0%u4F9B%u4E86%u4E00%u4E2A%u4E13%u95E8%u7684%u7C7B%u6765%u5904%u7406defaultdict%3A%20%20collections.defaultdict%0A1.%20%u521D%u59CB%u5316%2Cdefaultdict%28%29%u63A5%u53D7%u4E00%u4E2A%u7C7B%u578B%u4F5C%u4E3A%u521D%u59CB%u5316%u53C2%u6570.%0A%60%60%60python%0A%3E%3E%3E%20from%20collections%20import%20defaultdict%0A%3E%3E%3E%20dd%3Ddefaultdict%28list%29%0A%3E%3E%3E%20dd%0Adefaultdict%28%3Cclass%20%27list%27%3E%2C%20%7B%7D%29%0A%60%60%60%0A2.%20%u5F53%u8BBF%u95EE%u7684%u952E%u4E0D%u5B58%u5728%u7684%u65F6%u5019%2C%u4F1A%u81EA%u52A8%u521D%u59CB%u5316%u4E00%u4E2A%u9ED8%u8BA4%u503C.%0A%60%60%60python%0A%3E%3E%3E%20dd%5B%27tim%27%5D%0A%5B%5D%0A%3E%3E%3E%20dd%5B%27jerry%27%5D%3D26%0A%3E%3E%3E%20dd%0Adefaultdict%28%3Cclass%20%27list%27%3E%2C%20%7B%27jerry%27%3A%2026%2C%20%27tim%27%3A%20%5B%5D%7D%29%0A%3E%3E%3E%20dd%5B%27jerry%27%5D%3D27%0A%3E%3E%3E%20dd%0Adefaultdict%28%3Cclass%20%27list%27%3E%2C%20%7B%27jerry%27%3A%2027%2C%20%27tim%27%3A%20%5B%5D%7D%29%0A%3E%3E%3E%20dd%5B%27tim%27%5D.append%2822%29%0A%3E%3E%3E%20dd%0Adefaultdict%28%3Cclass%20%27list%27%3E%2C%20%7B%27jerry%27%3A%2027%2C%20%27tim%27%3A%20%5B22%5D%7D%29%0A%3E%3E%3E%20%0A%60%60%60%0A%u8FD9%u79CD%u5F62%u5F0F%u7684%u9ED8%u8BA4%u503C%u53EA%u6709%u5728%u901A%u8FC7dict%5Bkey%5D%u6216%u8005dict.__getitem__%28key%29%u8BBF%u95EE%u7684%u65F6%u5019%u624D%u6709%u6548%0A3.%20defaultdict%u9664%u4E86%u63A5%u53D7%u7C7B%u578B%u4F5C%u4E3A%u53C2%u6570%u5916%2C%u4E5F%u53EF%u4EE5%u4EE5%u4E0D%u5E26%u53C2%u6570%u7684%u53EF%u8C03%u7528%u51FD%u6570%u4F5C%u4E3A%u53C2%u6570.%0A%60%60%60python%0A%3E%3E%3E%20from%20collections%20import%20defaultdict%0A%3E%3E%3E%20def%20zero%28%29%3A%0A...%20%20%20%20%20return%200%0A...%0A%3E%3E%3E%20dd%20%3D%20defaultdict%28zero%29%0A%3E%3E%3E%20dd%0Adefaultdict%28%3Cfunction%20zero%20at%200xb7ed2684%3E%2C%20%7B%7D%29%0A%3E%3E%3E%20dd%5B%27foo%27%5D%0A0%0A%3E%3E%3E%20dd%0Adefaultdict%28%3Cfunction%20zero%20at%200xb7ed2684%3E%2C%20%7B%27foo%27%3A%200%7D%29%0A%60%60%60%0A%20%u6765%u89E3%u51B3%u6700%u521D%u7684%u5355%u8BCD%u7EDF%u8BA1%u95EE%u9898%0A%0A%60%60%60python%0Afrom%20collections%20import%20defaultdict%0Astrings%20%3D%20%28%27puppy%27%2C%20%27kitten%27%2C%20%27puppy%27%2C%20%27puppy%27%2C%27weasel%27%2C%20%27puppy%27%2C%20%27kitten%27%2C%20%27puppy%27%29%0Acounts%20%3D%20defaultdict%28lambda%3A%200%29%20%20%23%20%u4F7F%u7528lambda%u6765%u5B9A%u4E49%u7B80%u5355%u7684%u51FD%u6570%0Afor%20s%20in%20strings%3A%0A%20%20%20%20counts%5Bs%5D%20+%3D%201%0A%60%60%60%0A4.%20defaultdict%u7C7B%u662F%u5982%u4F55%u5B9E%u73B0%u7684%3F%0A%u5173%u952E%u5728%60__missing__%28%29%60%u65B9%u6CD5%2C%u5F53%u662F%u7528%60__getitem__%28%29%60%u65B9%u6CD5%u83B7%u53D6%u4E0D%u5230%u503C%u7684%u65F6%u5019%2C%u4F1A%u8C03%u7528%60__missing__%28%29%60%u65B9%u6CD5%u83B7%u53D6%u9ED8%u8BA4%u503C.%u5E76%u5C06%u8BE5%u952E%u6DFB%u52A0%u5230%u5B57%u5178%u4E2D.%20%0A%0A%u5728dict%u7684%u6D3E%u751F%u5B50%u7C7B%u4E2D%2C%u5982%u679C%u8BBF%u95EE%u7684%u952E%u4E0D%u5B58%u5728%u65F6%2Cd%5Bkey%5D%u4F1A%u8C03%u7528%60__missing__%28%29%60%u65B9%u6CD5%u6765%u83B7%u53D6%u9ED8%u8BA4%u503C.%20%u4F46%u662F%u5728dict%u4E2D%2C%u5E76%u672A%u5B9E%u73B0%u8FD9%u4E00%u65B9%u6CD5.%20%0A%60%60%60python%0A%3E%3E%3E%20class%20Missing%28dict%29%3A%0A%09def%20__missing__%28self%2Ckey%29%3A%0A%09%09return%20%27missing%27%09%0A%3E%3E%3E%20d%3DMissing%28%29%0A%3E%3E%3E%20d%5B%271%27%5D%0A%27missing%27%0A%60%60%60%0A%u901A%u8FC7%u4E0A%u9762%u8FD9%u4E2A%u4F8B%u5B50%u5C31%u53EF%u4EE5%u770B%u51FA%u6765.%60__missing__%28%29%60%u51FD%u6570%u786E%u5B9E%u751F%u6548%u4E86.%20%0A%u53E6%u5916%2C%u8FD8%u53EF%u4EE5%u8FDB%u4E00%u6B65%u4FEE%u6539%u4EE5%u8FBE%u5230defaultdict%u7684%u6548%u679C%3A%0A%60%60%60python%0A%3E%3E%3E%20class%20Defaulting%28dict%29%3A%0A%09def%20__missing__%28self%2Ckey%29%3A%0A%09%09self%5Bkey%5D%3D%27default%27%0A%09%09return%20%27default%27%0A%3E%3E%3E%20dd%3DDefaulting%28%29%0A%3E%3E%3E%20dd%5B%271%27%5D%0A%27default%27%0A%3E%3E%3E%20dd%0A%7B%271%27%3A%20%27default%27%7D%0A%60%60%60%0A%0A%0A%0A%0A

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章