473,799 Members | 2,834 Online
Bytes | Software Development & Data Engineering Community
+ 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 10515
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
2658
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 (with a key = a string from the list and value = None) for the purpose of insertion and removal? Basically, if Python really implements lists as linked lists but dictionaries as hash tables, it may well be that hashing a key takes negligible time...
14
8661
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
1367
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 fairly simple (searching/organizing large boardgame databases) but use a lot of memory (1-6GB). The memory is mostly used for simple structures like trees or relations. Typically there will be a few large dictionaries and many small...
8
2624
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
10566
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 I am missing that feature. Maybe there is something wrong with my programming style, but I rather think it is generally useful. I fully agree with the following posting where somebody complains why so very basic and useful things are not part...
1
1493
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 confirmation in the Python manuals, and did not succeed in finding it. In particular: http://www.python.org/doc/2.3.5/lib/typesmapping.html is silent on the subject. As for:
19
3844
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 dictionary keys, based on their content, not their identity. Is this feasible using the arrays directly, or do I need to wrap them in a struct that handles GetHashCode and Equal? If so, is such a wrapper present in the standard class library?
5
2083
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 key value pairs: {'001' : '01'} The second dict's contents are like this with a tuple instead of a string for the key: {('This is one', '001'): '01'}
14
1826
by: cnb | last post by:
Are dictionaries the same as hashtables?
4
5970
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 apsw #============ dic1={}
0
9687
marktang
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...
1
10231
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,...
0
10027
tracyyun
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...
0
9073
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, 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...
1
7565
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6805
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();...
0
5463
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...
0
5585
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2938
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 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...

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.