Koray Tugay
Koray Tugay

Reputation: 23800

How can I initialize dictionary in Python where key is an object?

I am trying to rephrase the implementation found here. This is what I have so far:

import csv
import math
import random

training_set_ratio = 0.67

training_set = []
test_set = []


class IrisFlower:
    def __init__(self, petal_length, petal_width, sepal_length, sepal_width, flower_type):
        self.petal_length = petal_length
        self.petal_width = petal_width
        self.sepal_length = sepal_length
        self.sepal_width = sepal_width
        self.flower_type = flower_type

    def __hash__(self) -> int:
        return hash((self.petal_length, self.petal_width, self.sepal_length, self.sepal_width))

    def __eq__(self, other):
        return (self.petal_length, self.petal_width, self.sepal_length, self.sepal_width) \
               == (other.petal_length, other.petal_width, other.sepal_length, other.sepal_width)


def load_data():
    with open('dataset.csv') as csvfile:
        rows = csv.reader(csvfile, delimiter=',')
        for row in rows:
            iris_flower = IrisFlower(float(row[0]), float(row[1]), float(row[2]), float(row[3]), row[4])
            if random.random() < training_set_ratio:
                training_set.append(iris_flower)
            else:
                test_set.append(iris_flower)


def euclidean_distance(flower_one: IrisFlower, flower_two: IrisFlower):
    distance = 0.0
    distance = distance + math.pow(flower_one.petal_length - flower_two.petal_length, 2)
    distance = distance + math.pow(flower_one.petal_width - flower_two.petal_width, 2)
    distance = distance + math.pow(flower_one.sepal_length - flower_two.sepal_length, 2)
    distance = distance + math.pow(flower_one.sepal_width - flower_two.sepal_width, 2)
    return distance


def get_neighbors(test_flower: IrisFlower):
    distances = []
    for training_flower in training_set:
        dist = euclidean_distance(test_flower, training_flower)
        d = dict()
        d[training_flower] = dist
        print(d)
    return


load_data()
get_neighbors(test_set[0])

Currently, print statements in the following code block:

def get_neighbors(test_flower: IrisFlower):
    distances = []
    for training_flower in training_set:
        dist = euclidean_distance(test_flower, training_flower)
        d = dict()
        d[training_flower] = dist
        print(d)
    return

will have outputs similar to

{<__main__.IrisFlower object at 0x107774fd0>: 0.25999999999999945}

which is ok. But I do not want to create the dictionary first, and then append the key value, as in:

d = dict()
d[training_flower] = dist

So this is what I am trying:

d = dict(training_flower = dist)

However, it does not seem like the dist method is using the instance, but rather a String, because what I see printed is as follows:

{'training_flower': 23.409999999999997}
{'training_flower': 16.689999999999998}

How do I create the dictionary by using the object as key in one statement?

Upvotes: 1

Views: 102

Answers (3)

Maxim Kukhtenkov
Maxim Kukhtenkov

Reputation: 724

In your snippet, where you write d = dict(training_flower=dist), "training_flower" is a keyword argument for dict function and not an object. It is equivalent to writing d = {'training_flower': dist}. The only way to create a dictionary with an object as a key is to use the latter syntax:

d = {training_flower: dist}

Upvotes: 3

Stephen Rauch
Stephen Rauch

Reputation: 49794

To directly create a dict with a key which is not a valid keyword, use the {} syntax like:

Code:

d = {training_flower: 'a_value'}

Test Code:

training_flower = 'a key'
d = {training_flower: 'a_value'}
print(d)

Results:

{'a key': 'a_value'}

Upvotes: 2

Jay Calamari
Jay Calamari

Reputation: 653

to initialize a dictionary with an object as a key, (edit: and the string in Stephen's example is an object anyway)

class Flower:

    def __repr__(self):
        return 'i am flower'


flower1 = Flower()
d = {flower1: 4}
print(d)

outputs

{i am flower: 4}

this is my first post here, and I know I'm late, sorry if it's a duplicate solution. just to show it works with an object.

would upvote Stephen's answer but I can't yet.

Upvotes: 1

Related Questions