Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/visual-studio-2010/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 通过值差连接两个字典_C#_Linq - Fatal编程技术网

C# 通过值差连接两个字典

C# 通过值差连接两个字典,c#,linq,C#,Linq,我有这两本字典: Dictionary<char, double> analyzed_symbols = new Dictionary<char, double>(); Dictionary<char, double> decode_symbols = new Dictionary<char, double>(); 我想我应该使用LINQ来实现这个目的,但我不知道如何正确地编写查询 数据样本: analyzed_symbols = [a, 173

我有这两本字典:

Dictionary<char, double> analyzed_symbols = new Dictionary<char, double>();
Dictionary<char, double> decode_symbols = new Dictionary<char, double>();
我想我应该使用LINQ来实现这个目的,但我不知道如何正确地编写查询

数据样本:

analyzed_symbols = [a, 173], [b, 1522], [z, 99]
decode_symbols = [в, 100], [д, 185], [e, 1622]
对于这些DICT,输出数据应如下所示:

Dictionary<char, char> replace_symbols = new Dictionary<char, char>();
Math.Min(Math.Abs(analyzed_symbols[key] - decode_symbols[key]))
replace_symbols = [z, в], [b, е], [a, д]

我发现这非常接近我需要的,但不完全是。Snowy在那里询问一个接近值,但我需要对两个字典做同样的事情。

好的,我试试。我分为几个查询,因为这样更可读

//sorting values of the dictionaries to easily get closest
var analyzedSortedValues = analyzed_symbols.Values.OrderBy(k => k);
var decodeSortedValues = decode_symbols.Values.OrderBy(k => k);

//creating pairs of the closest values. Here I use iterator index i to skip
//some values that have been used already (is it correct?)
var t = analyzedSortedValues.Select((k, i) => new { a = k, d = decodeSortedValues.Skip(i).Any() ? decodeSortedValues.Skip(i).First() : -1 });

//printing results by getting appropriate keys from corresponding dictionaries       
foreach (var item in t)
{
    Console.WriteLine("[{0}, {1}]", analyzed_symbols.FirstOrDefault(kvp => kvp.Value == item.a).Key, decode_symbols.FirstOrDefault(kvp => kvp.Value == item.d).Key);
}

我不太确定如何通过LINQ实现,但这里是您想要实现的直接版本

private static Dictionary<char, char> BuildReplacementDictionary(Dictionary<char, double> analyzedSymbols,
                                                                 Dictionary<char, double> decodeSymbols)
{
    Dictionary<char, char> replaceSymbols = new Dictionary<char, char>(analyzedSymbols.Count);

    foreach (KeyValuePair<char, double> analyzedKvp in analyzedSymbols)
    {
        double bestMatchValue = double.MaxValue;

        foreach (KeyValuePair<char, double> decodeKvp in decodeSymbols)
        {
            var testValue = Math.Abs(analyzedKvp.Value - decodeKvp.Value);
            if (testValue <= bestMatchValue)
            {
                bestMatchValue = testValue;
                replaceSymbols[analyzedKvp.Key] = decodeKvp.Key;
            }
        }
    }
    return replaceSymbols;
}
private static Dictionary buildreplacement Dictionary(Dictionary-analyzedSymbols,
字典(符号)
{
字典替换符号=新字典(analyzedSymbols.Count);
foreach(分析符号中的KeyValuePair analyzedKvp)
{
double bestMatchValue=double.MaxValue;
foreach(解码符号中的KeyValuePair decodeKvp)
{
var testValue=Math.Abs(analyzedKvp.Value-decodeKvp.Value);
如果(testValue这是我对它的看法:

var analyzed_symbols = new Dictionary<char, double>(){ {'a', 173}, {'b', 1522}, {'z', 99} };
var decode_symbols = new Dictionary<char, double>(){ {'в', 100}, {'д', 185}, {'e', 1622} };

var q = from a in analyzed_symbols
        from d in decode_symbols
        let tmp = new { A = a.Key, D = d.Key, Value = Math.Abs(a.Value - d.Value) }
        group tmp by tmp.A into g
        select new
        {
            Key = g.Key,
            Value = g.OrderBy (x => x.Value).Select (x => x.D).First()
        };

var replace_symbols = q.ToDictionary (x => x.Key, x => x.Value);
var analysisted_symbols=newdictionary(){{'a',173},{'b',1522},{'z',99};
var decode_symbols=newdictionary(){{'of',100},{'η',185},{'e',1622};
var q=从分析的符号中的a开始
从d开始解码_符号
设tmp=new{A=A.Key,D=D.Key,Value=Math.Abs(A.Value-D.Value)}
将tmp按tmp.A分组为g
选择新的
{
键=g键,
Value=g.OrderBy(x=>x.Value)。选择(x=>x.D)。First()
};
var replace_symbols=q.ToDictionary(x=>x.Key,x=>x.Value);

你能提供一些样本数据吗?@SergeyBerezovskiy头盔适合你1:1。(好像它是第一次在你的图片上设计的:-))。将示例数据添加到question.Zip/Union是您的选项。我不明白您想要什么。@DmitryMikhaylov您能为您的示例数据提供预期的输出吗?也许我遗漏了一些东西,但我认为您的代码在每次得到小于bestMatchValue的testValue时都会将值添加到ReplaceSymbles。是的,您遗漏的是它会替换而不是添加。所以它只会覆盖
replaceSymbles[analyzedKvp.value]
的当前值,如果
testValue
的值小于
bestMatchValue