473,320 Members | 1,857 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Dictionary with keys only. Anything ?


Sometimes I define a
System.Collections.Generic.Dictionary(Of Key, ...)

but actually I only use the Key part. Is there any collection which I
can use
which is similar to a Dictionary, but without the value part ?
I mean just a list of unique key Object , with a O(1) retrieval
operation ?

-Pam

Feb 13 '07 #1
4 2625
All collections have value parts. If your keys are strings, use
System.Collections.Specialized.StringCollection. Otherwise, try

dim Valueless as new Dictionary(of KeyType, KeyType)
Valueless.add(key, key)

At least this way you don't have two objects floating around memory - just
one with two references.

Mike Ober.

"pamela fluente" <pa***********@libero.itwrote in message
news:11**********************@v45g2000cwv.googlegr oups.com...
>
Sometimes I define a
System.Collections.Generic.Dictionary(Of Key, ...)

but actually I only use the Key part. Is there any collection which I
can use
which is similar to a Dictionary, but without the value part ?
I mean just a list of unique key Object , with a O(1) retrieval
operation ?

-Pam



Feb 13 '07 #2
On Feb 13, 12:03 am, "pamela fluente" <pamelaflue...@libero.itwrote:
Sometimes I define a
System.Collections.Generic.Dictionary(Of Key, ...)

but actually I only use the Key part. Is there any collection which I
can use
which is similar to a Dictionary, but without the value part ?
I mean just a list of unique key Object , with a O(1) retrieval
operation ?

-Pam
I'm particularly fond of
System.Collections.ObjectModel.KeyedCollection(Of Key, Item). It's
usefull when the item is also the key, or the key is a property of the
item itself. Unfortunately its an abstract class, which means you must
inherit it, because there's a GetKeyForItem method that needs
implementation. Indexed access is O(1), while keyed access is near
O(1) -- or so the docs say. Besides, it has the definite advantage (to
me) that it keeps the insertion order (which a Dictionary won't).

HTH.

Regards,

Branco.


Feb 13 '07 #3
On 13 Feb, 04:50, "Michael D. Ober" <obermd.@.alum.mit.edu.nospam>
wrote:
All collections have value parts. If your keys are strings, use
System.Collections.Specialized.StringCollection.
The times I need keys only are often those where the object is also
used as a key (through an appropriate comparer).

Otherwise, try
>
dim Valueless as new Dictionary(of KeyType, KeyType)
Valueless.add(key, key)
Yes that's what I am doing. Or else I do Valueless.add(key, Nothing)
(is this better?) which anyway seems quite awkward to me.
>
At least this way you don't have two objects floating around memory - just
one with two references.

Mike Ober.
Feb 13 '07 #4
On 13 Feb, 06:45, "Branco Medeiros" <branco.medei...@gmail.comwrote:
On Feb 13, 12:03 am, "pamela fluente" <pamelaflue...@libero.itwrote:
Sometimes I define a
System.Collections.Generic.Dictionary(Of Key, ...)
but actually I only use the Key part. Is there any collection which I
can use
which is similar to a Dictionary, but without the value part ?
I mean just a list of unique key Object , with a O(1) retrieval
operation ?
-Pam

I'm particularly fond of
System.Collections.ObjectModel.KeyedCollection(Of Key, Item). It's
usefull when the item is also the key, or the key is a property of the
item itself. Unfortunately its an abstract class, which means you must
inherit it, because there's a GetKeyForItem method that needs
implementation. Indexed access is O(1), while keyed access is near
O(1) -- or so the docs say. Besides, it has the definite advantage (to
me) that it keeps the insertion order (which a Dictionary won't).
Hmm I might give it a try. Isn't strange that there isn't a collection
of keys only ? I think it could be nice to have it
>
HTH.

Regards,

Branco.

Feb 13 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: none | last post by:
or is it just me? I am having a problem with using a dictionary as an attribute of a class. This happens in python 1.5.2 and 2.2.2 which I am accessing through pythonwin builds 150 and 148...
6
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...
57
by: Egor Bolonev | last post by:
why functions created with lambda forms cannot contain statements? how to get unnamed function with statements?
125
by: Raymond Hettinger | last post by:
I would like to get everyone's thoughts on two new dictionary methods: def count(self, value, qty=1): try: self += qty except KeyError: self = qty def appendlist(self, key, *values): try:
90
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...
12
by: rudysanford | last post by:
I just started messing with programming and started with Python. Part of my first project deals with translating numerical values to letters. I would like to be able to do the reverse as well,...
14
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...
18
by: Marko.Cain.23 | last post by:
Hi, I create a dictionary like this myDict = {} and I add entry like this: myDict = 1 but how can I empty the whole dictionary? Thank you.
8
by: Bob Altman | last post by:
Hi all, I'm trying to do something that should be really easy, but I can't think of an obvious way to do it. I have a dictionary whose value is a "value type" (as opposed to a reference type --...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.