473,480 Members | 1,872 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Comparing two Dictionaries

29 New Member
I have this list of devices and I store it in the Dictionary. When the program loaded, the Items of the dictionary will be displayed in the listView. I created a thread that will refresh the list when a new device is connected to a usb port. Now, how could I compare the items currently stored in the dictionary to the new items saved in another Dictionary?

For example:

Dictionary1 = {device1, device2} -this dictionary holds the items displayed in the listView.

When the program is loaded, the thread will start and will poll for the new device.

Dictionary2 = {device1, device2, device3} -consider that device3 is the new connected device.

Now, what is the simpliest way to compare the two dictionaries without affecting the process being run by the device1 and device2 of Dictionary1?

I also want device3 to automatically displayed in the ListView when the thread detect it.
Feb 6 '12 #1
3 10452
GaryTexmo
1,501 Recognized Expert Top Contributor
For the purposes of comparison, imho just compare the keys. If you're only interested in comparing dictionary1 to dictionary2, you could do something like...

Expand|Select|Wrap|Line Numbers
  1. List<object> newItems = new List<object>();
  2. foreach (object key in dictionary2.keys)
  3. {
  4.   if (!dictionary1.ContainsKey(key)
  5.     newItems.Add(key);
  6. }
That should populate newItems with all the keys that dictionary2 has but dictionary1 doesn't have. You might have to do another pass if you also want the items that dictionary1 has but dictionary2 doesn't have.

Also this is comparing keys, as a Dictionary object is essentially a list of KeyValuePair objects. If you wanted to compare values you could change the code to do that.

As for comparing them without having an impact on the process, you'll probably want to do the compare in another thread, and make sure you use the lock statement to ensure you're not reading and writing to the dictionaries at the same time. Perhaps when you detect differences, you can trigger an event from your processing thread to your GUI thread to have your ListView update?

There are several ways you could go about this, that's just one suggestion. Hopefully it gives you some ideas :)
Feb 6 '12 #2
mylixes
29 New Member
Thank you so much for the explanation. I already tried that. Im just kinda confused with what I have encountered.

Actually I want my program to do this:

I have a listView that will display a list of devices. each devices may have different interfaces.

For example:
Device1 - Device1.0
- Device1.0
- Device1.0

Device2 - Device2.1
- Device2.1
- Device2.1

The program can detect similar device name. they only differ in theie interface index.
So, given the example above, device1 and device2 are just similar device having the same device name but their interfaces have different index. The index may vary according to the number of similar device connected to the PC.

Now, I want to display the devices' name to the listView and I want it to be sorted.
I want to display only the device name exclusing their interfaces. So, for the example above, I want to display the like this:

Row 1: Device1
Row 2: Device2

I displayed two similar devices since the PC detected two similar devices.
I actually done this part.

But my problem now is that, my program run a thread that will automatically detect a new device connected to the PC.

So if the thread detected the new device, I store it to another Dictionary. By the way, the values displayed in the ListView are alose stored in the Dictionary.

say for an instance, I have Dict1 that holds values of the ListView and I have Dict2 which will hold the values of the listView and the new detected device.

I want to compare if the two dictionaries are equal. I tried comparing their keys and values I managed to do that. But I am quite confused if the scenario is like this:

dict1 = {[key = 0], [value = "device1"
[key = 1], [value = "device2"}

dict2 = {[key = 0], [value = "device3"
[key = 1], [value = "device1"
[key = 2], [value = "device2"}

Since, it is possible that the new device can be store at any dictionary index because of alphabetical order.

Any help will be highly appreciated.
Feb 7 '12 #3
GaryTexmo
1,501 Recognized Expert Top Contributor
I see your confusion... I ask you this, why are you using an integer as your key in your dictionary? You're effectively turning your dictionary into a List and losing all the advantages of having a key. In fact, why use a dictionary at all, why not just use a List?

In either case you mentioned you tried my example code as well as comparing by value. I don't know how you did this but were unable to merge the dictionaries. Looking at the example you provided, it's quite clear that dict2 does not contain the value "device3" and thus it would be detected as a missing item.

Using almost the exact code I posted originally, but using Values instead of Keys I'm able to detect that device3 is not present in dict1. I freely admit that I may not be understanding you correctly but your last bit there with the example data is pretty clear. Perhaps you should post the code you're trying to use...
Feb 7 '12 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

6
2634
by: Narendra C. Tulpule | last post by:
Hi, if you know the Python internals, here is a newbie question for you. If I have a list with 100 elements, each element being a long string, is it more efficient to maintain it as a dictionary...
14
8636
by: Kill Bill | last post by:
type(i) == "<type 'float'>" this always returns false. How come? type(i)returns <type 'float'> if i is a float so why isn't == working?
0
1351
by: Till Plewe | last post by:
Is there a way to speed up killing python from within a python program? Sometimes shutting down takes more than 10 times as much time as the actual running of the program. The programs are...
8
2606
by: Frohnhofer, James | last post by:
My initial problem was to initialize a bunch of dictionaries at the start of a function. I did not want to do def fn(): a = {} b = {} c = {} . . . z = {}
210
10261
by: Christoph Zwerschke | last post by:
This is probably a FAQ, but I dare to ask it nevertheless since I haven't found a satisfying answer yet: Why isn't there an "ordered dictionary" class at least in the standard list? Time and again...
1
1473
by: François Pinard | last post by:
Hi, people. I noticed today that dictionaries seem to support `==' comparison. (Retrospectively, it is strange that I never needed it before! :-) Yet, before relying on this, I seeked for...
19
3805
by: Ole Nielsby | last post by:
How does the GetHashCode() of an array object behave? Does it combine the GetHashCode() of its elements, or does it create a sync block for the object? I want to use readonly arrays as...
5
2064
by: brad | last post by:
I want to compare two dicts that should have identical info just in a different data structure. The first dict's contents look like this. It is authoritative... I know for sure it has the correct...
14
1790
by: cnb | last post by:
Are dictionaries the same as hashtables?
4
5947
by: Gilles Ganault | last post by:
Hello I fill two dictionaries with the same number of keys, and then need to compare the value for each key, eg. #Pour chaque APE, comparaison societe.ape.nombre et verif.ape.nombre import...
0
6908
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
1
6737
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...
0
5336
agi2029
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,...
1
4776
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4481
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...
0
2995
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...
0
2984
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
563
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
179
bsmnconsultancy
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...

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.