Hi,
I have a collection of objects that I am currently trying to manage.
Each object is initialized with a dictionary. The dictionaries have the
same keys but may have different values. The initialization process is
time consuming, so I don't want to create an object if I have already
instantiated one using a dictionary with the 'same' items.
I would like to collect the objects in a dictionary with the objects as
values and some identifier or hash of the initializing dictionary as the
keys.
For instance:
idicts = [{'k1':val1,'k2' :val2},
{'k1':val3,'k2' :val4},
{'k1':val1,'k2' :val2}]
for idict in idicts:
dhash = somehashingfunc tion(idict)
if not objectstoragedi ct.has_key(dhas h):
newobj = MyClass(idict)
objectstoragedi ct[dhash] = newobj
Any ideas for 'somehashingfun ction'?
The values in idict may be strings, integers, or instances of other
classes I have created.
Presently, I am considering something like
def somehashingfunc tion(rdict):
import sha
dkeys = rdict.keys()
dkeys.sort()
rvals = [repr(rdict[key]) for key in dkeys]
rstr = '_'.join(rvals)
dhash = sha.new(rstr).h exdigest()
return dhash
I'd appreciate any suggestions or comments.
Thanks.
-g 1 1674
gyro wrote: Hi, I have a collection of objects that I am currently trying to manage. Each object is initialized with a dictionary. The dictionaries have the same keys but may have different values. The initialization process is time consuming, so I don't want to create an object if I have already instantiated one using a dictionary with the 'same' items.
I would like to collect the objects in a dictionary with the objects as values and some identifier or hash of the initializing dictionary as the keys.
For instance:
idicts = [{'k1':val1,'k2' :val2}, {'k1':val3,'k2' :val4}, {'k1':val1,'k2' :val2}]
for idict in idicts: dhash = somehashingfunc tion(idict) if not objectstoragedi ct.has_key(dhas h): newobj = MyClass(idict) objectstoragedi ct[dhash] = newobj
Any ideas for 'somehashingfun ction'? The values in idict may be strings, integers, or instances of other classes I have created.
Presently, I am considering something like
def somehashingfunc tion(rdict): import sha dkeys = rdict.keys() dkeys.sort() rvals = [repr(rdict[key]) for key in dkeys] rstr = '_'.join(rvals) dhash = sha.new(rstr).h exdigest() return dhash
I'd appreciate any suggestions or comments.
Thanks.
-g
Actually, it seems that the problem might be bypassed by other design,
but you know it better. If you really need it, you approach seems
resonable. However, sometimes you can store data as a tuple or list and
map keys to inidices---in this case you can use standard comparision
operators. A word of caution: if store mutable objects, there might be
some problems.
hth,
anton. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Jan-Erik Meyer-Lütgens |
last post by:
In the Python Language Reference, I found the following
statements about using objects as dictionary keys:
1. "__hash__() should return a 32-bit integer."
2. "The only required property is that objects which
compare equal have the same hash value."
3. "If a class does not define a __cmp__() method it
should not define a __hash__() operation either."
|
by: Elbert Lev |
last post by:
Hi!
Here is the problem:
I have a dictionary. Keys are strings. How to make dictionary lookup
case insensitive?
In other words:
If dict = {'First":"Bob", "Last":"Tom"}, dict should return
"Bob"
|
by: Egor Bolonev |
last post by:
why functions created with lambda forms cannot contain statements?
how to get unnamed function with statements?
|
by: Christoph Zwerschke |
last post by:
Ok, the answer is easy: For historical reasons - built-in sets exist
only since Python 2.4.
Anyway, I was thinking about whether it would be possible and desirable
to change the old behavior in future Python versions and let dict.keys()
and dict.values() both return sets instead of lists.
If d is a dict, code like:
for x in d.keys():
|
by: vatamane |
last post by:
This has been bothering me for a while. Just want to find out if it
just me or perhaps others have thought of this too: Why shouldn't the
keyset of a dictionary be represented as a set instead of a list? I
know that sets were introduced a lot later and lists/dictionaries were
used instead but I think "the only correct way" now is for the
dictionary keys and values to be sets. Presently {1:0,2:0,3:0}.keys()
will produce but it could also...
| |
by: kdotsky |
last post by:
Hello,
I am using some very large dictionaries with keys that are long strings
(urls). For a large dictionary these keys start to take up a
significant amount of memory. I do not need access to these keys -- I
only need to be able to retrieve the value associated with a certain
key, so I do not want to have the keys stored in memory. Could I just
hash() the url strings first and use the resulting integer as the key?
I think what I'm...
|
by: Sam Loxton |
last post by:
Hi,
I am fairly new to the python language and am trying to sort a nested
Dictionary of a Dictionary which I wish to sort by value. The dictionary
does not have to be restructured as I only need it sorted in this way
for printing purposes.
The following is an example of my Dictionary printed with 'print
dictionary.items()', where '2329513' is the key of the first hash, 'ops'
is the key of the second hash and '50.0' is the value of the...
|
by: kim.nolsoee |
last post by:
Hi
I want to use the Dictionary Classs that will load my own class
called KeyClass used as TKey.
Here is the code:
public class Dictionary
{
public static void Main()
|
by: Dave Young |
last post by:
I'm trying to replicate the behaviour of the Commerce.Dictionary object that
was found in Commerce Server 3.0
By using a hashtable or creating a custom class that implements IDictionary,
you have to add elements by using the following syntax.
DictionaryObject.Add("key","value");
However, the old Commerce Server Dictionary object used the following
syntax.
|
by: koara |
last post by:
What is the best to go about using a large set (or dictionary) that
doesn't fit into main memory? What is Python's (2.5 let's say)
overhead for storing int in the set, and how much for storing int ->
int mapping in the dict?
Please recommend a module that allows persistent set/dict storage +
fast query that best fits my problem, and as lightweight as possible.
For queries, the hit ratio is about 10%. Fast updates would be nice,
but i...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
| |
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |