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.
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... - List<object> newItems = new List<object>();
-
foreach (object key in dictionary2.keys)
-
{
-
if (!dictionary1.ContainsKey(key)
-
newItems.Add(key);
-
}
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 :)
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.
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...
Sign in to post your reply or Sign up for a free account.
Similar topics |
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...
|
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?
|
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...
|
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 = {}
|
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...
| |
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:
|
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?
|
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'}
|
by: cnb |
last post by:
Are dictionaries the same as hashtables?
|
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={}
|
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: 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...
|
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: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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...
| |