Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6167943
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T22:36:04+00:00 2026-05-23T22:36:04+00:00

I have a dictionary of objects: # I have thousands of objects in my

  • 0

I have a dictionary of objects:

# I have thousands of objects in my real world scenario
dic = {'k1':obj1, 'k2':obj2, 'k3':obj3, ...}
# keys are string
# objs are MyObject

Edit: Sorry for letting doubt in the question. Here is the exact class and the like() function:

class MyObject(object):
    def __init__(self, period, dimensions):
        self.id = None
        self.period = period # period is etree.Element
        self.dimensions = dict() # id -> lxml.XMLElements
        for dim in dimensions:
            # there must be only one child: the typed dimension
            self.dimensions[dim.get('dimension')] = dim[0]
        self._hash = None

    def __eq__(self, other):
        return isinstance(other, MyObject)
            and self.period == other.period
            and self.dimensions == other.dimensions

    def like(self, other):
        return (other is not None \
            and self.period == other.period \
           and self.dimensions.keys() == other.dimensions.keys())

I wonder how I can have the best implementation for finding objects in dictionary dic that are similar to a given value val. Something equivalent to:

def find_keys(dic, val):
    return [v for v in dic if v.like(val))

However this method is too slow, because I have thousands of iterations over find-keys() and thousands objects in the dictionary.

Right now, I have implemented a __hash__(self) on these objects, and added the key as a property:

    def __hash__(self):
        if self._hash is None:
            self._hash = hash(self.periodtype) ^ \
                hash(tuple(sorted(self.dimensions.values())))
        return self._hash

Then, I have built a lookup dictionary that is

hash_dic = { hash(obj1): [obj1], hash(obj2): [obj2, obj3] }

And this new search method is much faster:

def find_keys_fast(dic, val):
    prefetched=hash_dic[hash(val)]
    return [x.key for x in prefetched if x.like(val)]

Since __hash__ is a native function internally used by Sets and Dictionaries, is there anything faster or more elegant I could do?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T22:36:04+00:00Added an answer on May 23, 2026 at 10:36 pm

    Now that we can see the implementation of like, a quite simple approach seems feasible—far simpler than my other answer, for one. Define a new signature method on MyObject:

    def signature(self):
        return (self.period, frozenset(self.dimensions.keys()))
    

    And then iterate through the objects:

    import collections
    sig_keys = collections.defaultdict(set)
    for k, obj in dic.iteritems():
        sig_keys[obj.signature()].add(k)
    

    With that, sig_keys.values() gives all the sets of identifiers for objects which are alike. Lists of objects could instead be directly constructed, if that would better:

    sig_objs = collections.defaultdict(list)
    for obj in dic.itervalues():
        sig_objs[obj.signature()].append(obj)
    

    If you want, you could define __hash__ to return hash(self.signature()) or the equivalent.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a dictionary of objects: dic = {'k1':obj1, 'k2':obj2, 'k3':obj3, ...} class MyObject:
I have such dictionary Dictionary<string, object> , dictionary holds string keys and objects as
I have a global dictionary with Class objects and NSString keys. Every custom subclass
Say I have 3 Dictionary<string, string> objects. All 3 have the same key like
I have a large dictionary mapping keys (which are strings) to objects. I pickled
I have a dictionary with objects as keys. How can I check if specific
I have a dictionary whose keys are NSStrings and whose objects are NSArray. Here's
I have a dictionary of compound objects made up of a string, a list,
I have dictionary of about 20,000 objects The key is a string representation of
Let's say I have a dictionary full of objects for keys that may or

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.