473,385 Members | 1,901 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,385 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 2263
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
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
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,...
0
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...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.