(1) 用frozenset 才可以用集合當作Key
Example:
itemsets=[{'B','C'},{'D'}]
count[{'B','C'}]+=1 # Error : "unhashable type: 'set'"
in this case, we can use frozenset to make 'set' as a key
count[frozenset({'B','C'})]=1
(2) 用frozenset 才可以做集合包含集合
S=set()
e={'A','B','C'}
S.add (e) ==> 想要表示 S={{'A'},{'B'},{'C'}} --> TypeError: unhashable type: 'set'
S.add (frozenset(e)) -->OK S={frozenset({'A', 'B', 'C'})}
沒有留言 :
張貼留言