Click here to Skip to main content
15,867,453 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
There are two sorted dictionaries (based on value sorted key, value):

C#
for (int i = 0; i < mean_segment.Length; i++)
               {
                   cover_mean_dict.Add(i, mean_segment[i]);
               }
cover_mean_dict = cover_mean_dict.OrderBy(x => x.Value).ToDictionary(x => x.Key, x => x.Value);


               for (int i = 0; i < mean_watermark_segment.Length; i++)
               {
                   watermark_mean_dict.Add(i, mean_watermark_segment[i]);
                }
 watermark_mean_dict = watermark_mean_dict.OrderBy(x => x.Value).ToDictionary(x => x.Key, x => x.Value);



Now I need to map, first key of "cover_mean_dict<>" with first key of "watermark_mean_dict<>" !

cover_mean_dict<>
C#
0  -1886680
8  -923898
4  -904331
2  -726029
6  128219
7  483292
3  823588
5  1266530
1  1589283



Watermark_mean_dict
C#
7  -1831827
5  -916255
4  -553024
1  311866
8  1155816
2  1450021
6  1595960
0  1827328
3  1907207


Now I want a new structure which should be;
0 7
8 5
4 4
2 1
........

please any one suggest any idea..

Thanks in Advance !
Posted
Updated 1-Dec-15 22:27pm
v3
Comments
Kornfeld Eliyahu Peter 2-Dec-15 4:37am    
And there are exactly the same number of items in both list?
Kasthuri Gunabalasingam 2-Dec-15 4:46am    
yesh.. same number of items !
Kornfeld Eliyahu Peter 2-Dec-15 4:49am    
So! Go with index...
for(int i = 0; int < maxLen; i++)
{
// take ith position from cover_mean_dict
// take ith position from Watermark_mean_dict
}
Matt T Heffron 2-Dec-15 12:34pm    
The .OrderBy(...) before doing .ToDictionary() accomplishes nothing. The Dictionary class does not preserve the insertion order.

Try this:
C#
var d1List = cover_mean_dict.Keys.ToList();
var d2List = Watermark_mean_dict.Keys.ToList();

// returns a 'System.Collections.Generic.IEnumerable<<>f__AnonymousType0<int,int>>'
var match = d1List.Select((itm1, ndx) => new {itm1, itm2 = d2List[ndx]});

// test
foreach (var pair in match)
{
    Console.WriteLine("{0}, {1}", pair.itm1, pair.itm2);
}
However, keep in mind that the .Net generic Dictionary, and its Keys, and Values, Collections are not guaranteed to have any particular order ! So, as I see it, this use of Dictionaries may lead to problems. I suggest you use an OrderedDictionary [^], or another data-structure intended to be ordered, and inherently "indexable."
 
Share this answer
 
Comments
Kasthuri Gunabalasingam 2-Dec-15 13:01pm    
Thank you! It is working :)
Kornfeld Eliyahu Peter is right in that you could use the index to access each value, although accessing a Dictionary by the index is not as simple as using [i], you need to use ElementAt - remember a Dictionary is designed to be accessed via its Key

For example:
C#
const string formatOutput = "Loop {0} : Cover Mean: {1}, {2}; Water Mark: {3}, {4}";

for (var i = 0; i < coverMeanDict.Count; i++)
{
    Console.WriteLine(formatOutput, i,
      coverMeanDict.Keys.ElementAt(i), coverMeanDict.Values.ElementAt(i),
      Watermark_mean_dict.Keys.ElementAt(i), Watermark_mean_dict.Values.ElementAt(i));
}

Alternatively you can convert the Dictionaries to Lists (of KeyValuePairs) and use explicit indexing on the list. For example:
C#
var coverMeanList = coverMeanDict.ToList();
var WaterMarkList = Watermark_mean_dict.ToList();


for (var j = 0; j < coverMeanList.Count; j++)
{
    Console.WriteLine(formatOutput, j,
        coverMeanList[j].Key, coverMeanList[j].Value,
        WaterMarkList[j].Key, WaterMarkList[j].Value);
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900