0

I have a python dictionary some_dict with key (of type PosixPath) and value (of type tensor) that looks something like this:

{PosixPath('dataset_small/test/1db4b7f3f65739960cb553b8778982e7e6129e0d.tif'): tensor(0.0053),
PosixPath('dataset_small/test/b1762960a3b92ecc055ce06897378f42bf337c82.tif'): tensor(0.0498),
PosixPath('dataset_small/test/61416795264535918f705f2c93bfd532d23ee8da.tif'): tensor(0.0223),
...

How do I index through this dictionary?

I've tried some_dict['dataset_small/test/' + id + '.tif'] but got the following error:

KeyError                                  
Traceback (most recent call last)
<ipython-input-69-d7a2e0e6db0f> in <module>()
----> 1 pred_list_cor = [pred_dict['dataset_small/test/' + id + '.tif'] for id in sample_list]

<ipython-input-69-d7a2e0e6db0f> in <listcomp>(.0)
----> 1 pred_list_cor = [pred_dict['dataset_small/test/' + id + '.tif'] for id in sample_list]

KeyError: 'dataset_small/test/0b2ea2a822ad23fdb1b5dd26653da899fbd2c0d5.tif'```.
lowysng
  • 35
  • 1
  • 3

2 Answers2

1

This is an interesting question and I never tried before. I was trying to access the key directly as the previous answer advised using : value = some_dict[key] , but doesn't work for me and I found that you need to define 2 methods in your class to make it work : hash and eq Thanks to this : https://stackoverflow.com/a/4901847/1855988

" An object is hashable if it has a hash value which never changes during its lifetime (it needs a hash() method), and can be compared to other objects (it needs an eq() method). Hashable objects which compare equal must have the same hash value. " https://docs.python.org/3/glossary.html#term-hashable

UPDATE: I just notice that PosixPath is a predefined module. I thought it is a new class that you created :). I guess the answer is still useful!

Code :

class PosixPath:
    def __init__(self, text):
        self.text = text

    def __hash__(self):
        return hash((self.text))

    def __eq__(self, other):
        return (self.text) == (other.text)

class tensor:
    def __init__(self, value):
        self.value = value    

dic = {PosixPath('dataset_small/test/1db4b7f3f65739960cb553b8778982e7e6129e0d.tif'): tensor(0.0053)}


search = "dataset_small/test/1db4b7f3f65739960cb553b8778982e7e6129e0d.tif"
new = PosixPath(search)

dic[new].value

Result :

0.0053
J.K
  • 1,178
  • 10
  • 13
0

You have to convert string to PosixPath before you use it as key

key = PosixPath("dataset_small/test/0b2ea2a822ad23fdb1b5dd26653da899fbd2c0d5.tif")

value = some_dict[key] 

pred_list_cor = [pred_dict[PosixPath('dataset_small/test/' + id + '.tif')] for id in sample_list]

Example code for test

from pathlib import PosixPath

data = {
    PosixPath('dataset_small/test/1db4b7f3f65739960cb553b8778982e7e6129e0d.tif'): 0.0053,
    PosixPath('dataset_small/test/b1762960a3b92ecc055ce06897378f42bf337c82.tif'): 0.0498,
    PosixPath('dataset_small/test/61416795264535918f705f2c93bfd532d23ee8da.tif'): 0.0223
}

print( data[PosixPath('dataset_small/test/61416795264535918f705f2c93bfd532d23ee8da.tif')] )
# 0.0223
furas
  • 134,197
  • 12
  • 106
  • 148