I'm walking a data structure and would like to build a dict mapping X->Y, where X is a field in the data structure I'm walking and Y is a field in the data structure I'm building on the fly. X is an unhashable type.
相关问题
- Delete Messages from a Topic in Apache Kafka
- how to define constructor for Python's new Nam
- Jackson Deserialization not calling deserialize on
- streaming md5sum of contents of a large remote tar
- How to maintain order of key-value in DataFrame sa
The purpose of Java's IdentityHashMap is to simulate dynamic field. Since Python language already supports dynamic attributes directly, you don't need the map, just assign Y to an X's attribute
You can just use a regular Python
dict
for this if you wrap your unhashable objects in another object. Specifically, something like this:Then just use it like
some_dict[Wrapper(unhashable_object)]
.This is a more useful approach than just using
id(o)
as the key if you also need to be able to access the object itself afterwards (askey.o
, obviously). If you don't (and garbage collection isn't an issue), just use that.Trivially:
Use the
id
ofx
as the dictionary key