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

Iteratate Dictionay of List

Hi all,
I have a dictionary like this:

Dictionary<List<string>, List<string>>

How can I iterate in it?

Thanks very much.
--
Luigi

Oct 13 '08 #1
12 1446
Well, what do you want to iterate? Each key (a List<string>) could have
multiple valies - do you just want the values?

With C# 3.0 / .NET 3.5, SelectMany is an option, but I guess (last post)
that this isn't an option. So you'll just need a few nested foreach loops.

Marc
Oct 13 '08 #2
"Marc Gravell" wrote:
Well, what do you want to iterate? Each key (a List<string>) could have
multiple valies - do you just want the values?

With C# 3.0 / .NET 3.5, SelectMany is an option, but I guess (last post)
that this isn't an option. So you'll just need a few nested foreach loops.
Yes, I'm using C# 2.0, so many useful options are prohibited for me.
What I'd like is to obtain the collection of strings for every string of the
first List in my dictionary.

Luigi
Oct 13 '08 #3
Yes, I'm using C# 2.0, so many useful options are prohibited for me.
What I'd like is to obtain the collection of strings for every string of the
first List in my dictionary.
Can you show that with a simple example?

i.e.
"if the data was as bloe, then I want {your bit here}

item 1: key (list) "a", "b"
value (list) "c", "d", "e"
item 2: key (list) "f"
value (list) "g", "h"

Marc
Oct 13 '08 #4
"Marc Gravell" wrote:
Yes, I'm using C# 2.0, so many useful options are prohibited for me.
What I'd like is to obtain the collection of strings for every string of the
first List in my dictionary.

Can you show that with a simple example?

i.e.
"if the data was as bloe, then I want {your bit here}

item 1: key (list) "a", "b"
value (list) "c", "d", "e"
item 2: key (list) "f"
value (list) "g", "h"
I can't use the ContainsKey method of my Dictionary passing a List<string>
that is contained.

L

Oct 13 '08 #5
See below; I expect the problem is that you are passing a *different*
list that has the same data. By default, the lists are compared for
object (reference) equality. It is possible to override this behavior by
performing a custom comparison (so that two lists with the same contents
are treated as equal). Let me know if you need this.

Marc

class Program
{
static void Main()
{
Dictionary<List<string>, List<string>lookup = new
Dictionary<List<string>, List<string>>();
lookup.Add(Wrap("a", "b"), Wrap("c", "d", "e"));
lookup.Add(Wrap("f"), Wrap("g", "h"));

List<stringfirstKey = null;
foreach (KeyValuePair<List<string>, List<string>pair in lookup)
{
if (firstKey == null) firstKey = pair.Key;
Write(pair.Key, "Key");
Write(pair.Value, "Value");
}

if (lookup.ContainsKey(firstKey))
{
List<stringvalue = lookup[firstKey];
Write(value, "Value By Key");
}
}
static void Write<T>(List<Tlist, string caption)
{
Console.WriteLine(caption);
foreach (T t in list)
{
Console.WriteLine("\t{0}", t);
}
}
static List<TWrap<T>(params T[] args)
{
return new List<T>(args);
}
}
Oct 13 '08 #6
To treat different lists (with the same contents) as equal, you need to
provide a custom IEqualityComparer<Tto the dictionary's constructor.

The following should suffice, used with:

... lookup = new Dictionary<List<string>,
List<string>>(ListComparer<string>.Default);

Marc

class ListComparer<T: IEqualityComparer<List<T>>
{
private readonly IEqualityComparer<TitemComparer;
public static readonly ListComparer<TDefault = new ListComparer<T>();
public ListComparer() : this(null) { }
public ListComparer(IEqualityComparer<TitemComparer)
{
this.itemComparer = itemComparer ?? EqualityComparer<T>.Default;
}

bool IEqualityComparer<List<T>>.Equals(List<Tx, List<Ty)
{
if (ReferenceEquals(x, y)) return true;
int len;
if (x == null || y == null || (len = x.Count) != y.Count)
return false;
for (int i = 0; i < len; i++)
{
if (!itemComparer.Equals(x[i], y[i])) return false;
}
return true;
}

int IEqualityComparer<List<T>>.GetHashCode(List<Tlist)
{
if (list == null) return 0;
int agg = 3;
foreach (T item in list)
{
agg *= 13;
agg += 7 * itemComparer.GetHashCode(item);
}
return agg;
}
}
Oct 13 '08 #7
Are you sure you don't want something like

Dictionary<string, Dictionary<string, string>>

Your data structure seems quite useless to me. What are you intending to
store within it, and how do you intend to use that data?
--
Pete
====
http://mrpmorris.blogspot.com
http://www.capableobjects.com

Oct 13 '08 #8
"Marc Gravell" wrote:
See below; I expect the problem is that you are passing a *different*
list that has the same data. By default, the lists are compared for
object (reference) equality. It is possible to override this behavior by
performing a custom comparison (so that two lists with the same contents
are treated as equal). Let me know if you need this.

Marc

class Program
{
static void Main()
{
Dictionary<List<string>, List<string>lookup = new
Dictionary<List<string>, List<string>>();
lookup.Add(Wrap("a", "b"), Wrap("c", "d", "e"));
lookup.Add(Wrap("f"), Wrap("g", "h"));

List<stringfirstKey = null;
foreach (KeyValuePair<List<string>, List<string>pair in lookup)
{
if (firstKey == null) firstKey = pair.Key;
Write(pair.Key, "Key");
Write(pair.Value, "Value");
}

if (lookup.ContainsKey(firstKey))
{
List<stringvalue = lookup[firstKey];
Write(value, "Value By Key");
}
}
static void Write<T>(List<Tlist, string caption)
{
Console.WriteLine(caption);
foreach (T t in list)
{
Console.WriteLine("\t{0}", t);
}
}
static List<TWrap<T>(params T[] args)
{
return new List<T>(args);
}
}
Thank you Marc, very interesting.
Now I'm trying to using this code in my project.

Luigi
Oct 13 '08 #9
In the first string I have a name of check.
In the inner dictionary I have a list of objects that have to match together.
For example

"check1" is the key of the outer dictionary.

then I have "value1", "value2" is the key of the inner dic, and
"innervalue1", "innervalue2", "innervalue3" are the values.

Luigi
Oct 13 '08 #10
Another question Marc.
How can I write a methods that returns me the composition of the inner
dictionary?
For example, for the first KeyValuePair of the first List<stringI'd like
to obtain the List<stringvalue of the inner dictionary.

Luigi
Oct 13 '08 #11
Looks more like you want this...

Dictionary<string, Dictionary<string, List<string>>>
Take a look at this....
using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication7
{
class Program
{
static void Main(string[] args)
{
Dictionary<string, Dictionary<string, List<string>>checks = new
Dictionary<string, Dictionary<string, List<string>>>();

for (int checkNumber = 0; checkNumber < 10; checkNumber++)
{
checks.Add("Check" + checkNumber.ToString(), CreateCheck());
}

foreach (var currentCheck in checks)
{
Console.WriteLine("CurrentCheck: " + currentCheck.Key);
foreach (var currentValue in currentCheck.Value)
{
Console.WriteLine(" Current value: " + currentValue.Key);
foreach (var currentInnerValue in currentValue.Value)
Console.WriteLine(" Current inner value: " + currentInnerValue);
Console.ReadLine();
}
}

}

private static Dictionary<string, List<string>CreateCheck()
{
var result = new Dictionary<string, List<string>>();
for (int valueNumber = 0; valueNumber < 10; valueNumber++)
{
result.Add("Value" + valueNumber.ToString(), CreateInnerValues());
}
return result;
}

private static List<stringCreateInnerValues()
{
return new List<string>(new string[] { "One", "Two", "Three" });
}

}
}

Oct 13 '08 #12
Having this code:

Dictionary<string, Dictionary<string[], string[]>outerCheck =
new Dictionary<string, Dictionary<string[], string[]>>();

Dictionary<string[], string[]innerCheck = new Dictionary<string[],
string[]>();
string[] vociReference1 = {"Tava|a", "Tavb|b"};
string[] vociBalancing1 = {"Tavm|m", "Tavn|n"};

innerCheck.Add(vociReference1,vociBalancing1);

outerCheck.Add("Check1", innerCheck);

Dictionary<string[], string[]innerCheckTest = new Dictionary<string[],
string[]>();
innerCheckTest = outerCheck["Check1"];

Dictionary<string[], string[]>.KeyCollection keys = innerCheckTest.Keys;

string[] myKeys;

I can't populate this array.
What's wrong?

L
Oct 13 '08 #13

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

Similar topics

6
by: massimo | last post by:
Hey, I wrote this program which should take the numbers entered and sort them out. It doesn¹t matter what order, if decreasing or increasing. I guess I'm confused in the sorting part. Anyone...
10
by: Kent | last post by:
Hi! I want to store data (of enemys in a game) as a linked list, each node will look something like the following: struct node { double x,y; // x and y position coordinates struct enemy...
24
by: Robin Cole | last post by:
I'd like a code review if anyone has the time. The code implements a basic skip list library for generic use. I use the following header for debug macros: /* public.h - Public declarations and...
4
by: JS | last post by:
I have a file called test.c. There I create a pointer to a pcb struct: struct pcb {   void *(*start_routine) (void *);   void *arg;   jmp_buf state;   int    stack; }; ...
3
by: chellappa | last post by:
hi this simple sorting , but it not running...please correect error for sorting using pointer or linked list sorting , i did value sorting in linkedlist please correct error #include<stdio.h>...
0
by: drewy2k12 | last post by:
Heres the story, I have to create a doubly linked list for class, and i have no clue on how to do it, i can barely create a single linked list. It has to have both a head and a tail pointer, and...
10
by: AZRebelCowgirl73 | last post by:
This is what I have so far: My program! import java.util.*; import java.lang.*; import java.io.*; import ch06.lists.*; public class UIandDB {
0
by: Atos | last post by:
SINGLE-LINKED LIST Let's start with the simplest kind of linked list : the single-linked list which only has one link per node. That node except from the data it contains, which might be...
12
by: kalyan | last post by:
Hi, I am using Linux + SysV Shared memory (sorry, but my question is all about offset + pointers and not about linux/IPC) and hence use offset's instead on pointers to store the linked list in...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
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...

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.