473,371 Members | 1,452 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,371 software developers and data experts.

Check between List and Dictionary

Hi all,
having a List<stringand Dictionary<int, stringhow can I check that every
string in the list is also in the Dictionary (and viceversa)?
(and raise an exception when not).

Thanks in advance
--
Luigi

Sep 22 '08 #1
5 2262
Luigi was thinking very hard :
Hi all,
having a List<stringand Dictionary<int, stringhow can I check that every
string in the list is also in the Dictionary (and viceversa)?
(and raise an exception when not).

Thanks in advance
Something like:

1) check that the Counts are equal
2) foreach item in the first list, check (Contains/ContainsValue) that
is is present in the second list

This will fail if duplicate entries are allowed

or:
1) loop through all items in list1, check if it exists in list2 (if all
exists, then list1 is a subset of list2, but list2 might contain extra
items)
2) loop through all items in list2, check if it exists in list1 (if all
exists, then the lists are identical)
Hans Kesting
Sep 22 '08 #2
It depends on the size; one quick'n'dirty option would be to get the
two sets sorted the same, and use SequenceEqual:

var listQry = list.OrderBy(s=>s);
var lookupQry = lookup.Values.OrderBy(s=>s);

bool areEqual = lookupQry.SequenceEqual(listQry);

For larger volumes, then rather than sorting both, I would be tempted
to create a hashtable from the list, and just enumerate over the
dictionary Values checking. It is a sham you don't need to check the
dictionary keys (rather than values), as this would be a lot more
direct and efficient... are you sure the dictionary is the right way
around?

Marc
Sep 22 '08 #3
"Hans Kesting" wrote:
Something like:

1) check that the Counts are equal
2) foreach item in the first list, check (Contains/ContainsValue) that
is is present in the second list

This will fail if duplicate entries are allowed

or:
1) loop through all items in list1, check if it exists in list2 (if all
exists, then list1 is a subset of list2, but list2 might contain extra
items)
2) loop through all items in list2, check if it exists in list1 (if all
exists, then the lists are identical)
Hi Hans,
something like this?

foreach(string columnName in columnNamesList)
{
if (!tavolaColumnsDictionary.ContainsValue(columnName ))
throw new NotMatchingColumnsVoceTavola();
}

Thanks.

Luigi
Sep 22 '08 #4
Luigi <ci****************@inwind.itwrote:
having a List<stringand Dictionary<int, stringhow can I check that every
string in the list is also in the Dictionary (and viceversa)?
(and raise an exception when not).
What version of .NET are you using? If you're using .NET 3.5 I suggest
you first check that the counts are equal (to give an "early out")
build a HashSet<stringfrom the List<stringand then iterate through
the dictionary values, checking that each value is in the set.

--
Jon Skeet - <sk***@pobox.com>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon.skeet
C# in Depth: http://csharpindepth.com
Sep 22 '08 #5
OD
Hi all,
having a List<stringand Dictionary<int, stringhow can I check that every
string in the list is also in the Dictionary (and viceversa)?
(and raise an exception when not).

Thanks in advance
If lists are not too bigs you can use Linq to Object.
There're plenty of ways to get the results.
Here's one "sliced" and commented to be readable (shortests solutions
exist but it's just to show you a sample ) :
static void Main(string[] args)
{

var list = new List<string{"one", "two", "three"};
var dic = new Dictionary<int, string{{1, "one"}, {2,
"two"}, {3, "three"}, {4, "four"}};

// create a List<stringform the dictionnary
var q = (from KeyValuePair<int, stringk in dic select
k.Value).ToList();
// add List<stringitems to this query
q.AddRange(list);
// group all strings
var q2 = from s in q group s by s into z select z;
// select group where count is not equal to 2
q2 = from k in q2 where k.Count() != 2 select k;

Console.WriteLine("--- non matching items---");

foreach (var s in q2)
{
Console.WriteLine("item: "+s.Key + " - count: " +
s.Count());
}

// if q2 count differs from zero, then the sources are not
matching
Console.WriteLine(q2.Count()==0?"Match":"No match");
Console.ReadLine();

}

--
OD___
www.e-naxos.com
Sep 22 '08 #6

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

Similar topics

5
by: micklee74 | last post by:
hi in my code, i use dict(a) to make to "a" into a dictionary , "a" comes from user input, so my program does not know in the first place. Then say , it becomes a = { '-A' : 'value1' , '-B' :...
6
by: buzzweetman | last post by:
Many times I have a Dictionary<string, SomeTypeand need to get the list of keys out of it as a List<string>, to pass to a another method that expects a List<string>. I often do the following: ...
3
by: erik gartz | last post by:
Hello, I'm new to python and I'm having difficulty understanding the following code. Why doesn't the variable a contain , ] instead. Doesn't {} allocate new memory for the dictionary each time?...
7
by: Sehboo | last post by:
We have several generic List objects in our project. Some of them have about 1000 items in them. Everytime we have to find something, we have to do a for loop. There is one method which does the...
7
by: Andrew Robinson | last post by:
I have a method that needs to return either a Dictionary<k,vor a List<v> depending on input parameters and options to the method. 1. Is there any way to convert from a dictionary to a list...
22
by: SmokeWilliams | last post by:
Hi, I am working on a Spell checker for my richtext editor. I cannot use any open source, and must develop everything myself. I need a RegExp pattern to split text into a word array. I have...
2
by: Assimalyst | last post by:
Hi I have a Dictionary<string, List<string>>, which i have successfully filled. My problem is I need to create a filter expression using all possible permutations of its contents. i.e. the...
20
by: Seongsu Lee | last post by:
Hi, I have a dictionary with million keys. Each value in the dictionary has a list with up to thousand integers. Follow is a simple example with 5 keys. dict = {1: , 2: , 900000: , 900001:...
6
by: dudeja.rajat | last post by:
Hi, How to check if something is a list or a dictionary or just a string? Eg: for item in self.__libVerDict.itervalues(): self.cbAnalysisLibVersion(END, item) where __libVerDict is a...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.