Reputation: 962
What do these two statements mean in Python?
distances[(clust[i].id,clust[j].id)]=distance(clust[i].vec,clust[j].vec)
d=distances[(clust[i].id,clust[j].id)]
I am guessing that the first statement assigns clust[i].id
and clust[j].id
keys of the distances map to the result of the distance(..)
function. However, I am confused since lists are represented using []
and dictionaries using {}
in Python. What's the correct answer?
Upvotes: 4
Views: 345
Reputation: 2046
distances[(clust[i].id,clust[j].id)]=distance(clust[i].vec,clust[j].vec)
distances
is a dictionary where keys are tuples of probably integers and the value is the distance measured between them by the distance
function.
in the second line:
d=distances[(clust[i].id,clust[j].id)]
the d
variable is just assigned to that distance, accessing the dictionary value just assigned.
other answers provide the summary of what's a dictionary.
Upvotes: 4
Reputation: 23083
Hopefully this will make it clear:
>>> a = {}
>>> a[1] = 2
>>> a[(1, 2)] = 3
>>> a
{(1, 2): 3, 1: 2}
Upvotes: 2
Reputation: 798496
Dictionary literals use {}
. Indexing operations use []
, regardless of type.
Upvotes: 6