我有:
tuple1 = token1, token2
tuple2 = token2, token1
for tuple in [tuple1, tuple2]:
if tuple in dict:
dict[tuple] += 1
else:
dict[tuple] = 1
Run Code Online (Sandbox Code Playgroud)
但是,元组1和元组2都得到相同的计数.什么是一种方法来散列一组2件事,这样的秩序很重要?
mgi*_*son 22
散列时会考虑订单:
>>> hash((1,2))
1299869600
>>> hash((2,1))
1499606158
Run Code Online (Sandbox Code Playgroud)
这假定对象本身具有唯一的哈希值.即使它们不这样做,在字典中使用它时仍然可以正常(只要对象本身不等于它们的__eq__方法所定义):
>>> t1 = 'a',hash('a')
>>> [hash(x) for x in t1] #both elements in the tuple have same hash value since `int` hash to themselves in cpython
[-468864544, -468864544]
>>> t2 = hash('a'),'a'
>>> hash(t1)
1486610051
>>> hash(t2)
1486610051
>>> d = {t1:1,t2:2} #This is OK. dict's don't fail when there is a hash collision
>>> d
{('a', -468864544): 1, (-468864544, 'a'): 2}
>>> d[t1]+=7
>>> d[t1]
8
>>> d[t1]+=7
>>> d[t1]
15
>>> d[t2] #didn't touch d[t2] as expected.
2
Run Code Online (Sandbox Code Playgroud)
请注意,由于哈希冲突,这个dict可能比没有哈希冲突的另一个dict效率低:)
他们获得相同计数的原因是您的代码显式增加两者token1,token2并同时token2,token1计数.如果不这样做,计数将不会保持同步:
In [16]: import collections
In [17]: d = collections.defaultdict(int)
In [18]: d[1,2] += 1
In [19]: d[1,2]
Out[19]: 1
In [20]: d[2,1]
Out[20]: 0
Run Code Online (Sandbox Code Playgroud)