C# 字典 <string, list<double>> 多键字典

C# Dictionary <string, list<double>> multiple keys dictionary

我需要将同一个密钥存储两次,所以我尝试这样做,但它不起作用。有人可以帮我如何正确地做到这一点吗?但是如果这个词只出现一次,我希望它存储一次,如果是3,4,5..还是两次。

private void Meth()
{
    foreach (var word in document)
    {
        dict1.TryGetValue(word.Key, out myValue);

        if (bigDict.ContainsKey(word.Key))
        {
            if (word.Value >= 2)
            {
                testing.Add(word.Key, myValue);
                testing.Add(word.Key, myValue);    
            }
            else
            {
                testing.Add(word.Key, myValue);
            }                                           
        }
        else
        {
            testing.Add(word.Key, 0.123);
        }
    }
}

我考虑过查找,但没有添加,所以我做了:

private Dictionary<Key, List<double>> testing = new Dictionary<Key, List<double>>();

虽然我无法将 "simply" double 添加到 List<double>

还有一个问题,我以后能不能这样用:

var somethingLikeDict = testing.OrderByDescending(word => word.Value)
        .Take(20)
        .ToDictionary(g => g.Key, g => g.Value);

我该如何解决这个问题?

@编辑 这就是我更改代码的方式,我收到一条错误消息,提示密钥在 dictionary/key.

中不存在
private Dictionary<string, List<double>> testing = new Dictionary<string, List<double>>();

private void MailProbability2()
{
    foreach (var word in document)
    {
        if (bigDict.ContainsKey(word.Key))
        {
            bigDict.TryGetValue(word.Key, out myValue);

            if (word.Value >= 2)
            {
                testing.Add(word.Key, new List<double>() { myValue, myValue});    
            }
            else
            {
                testing[word.Key].Add(myValue);
            }                                      
        }
        else
        {
            testing[word.Key].Add(0.123);
        }
    }
}

bigDict 是一个字典,其中包含我想放入最多两个值的测试字典中的值。

您的方法可能更适合数据的物理模型,例如:

public class Storage
{
     public string Key { get; set; }
     public List<double> Value { get; set; }
}

您有一个物理模型,可以让您简单地构建以下内容:

var storage = List<Storage>();

因此,无论何时向 Storage 添加值,您都会将该变量存储到 List<Storage> 中,以便您快速访问。

您只能在字典中添加每个键一次。 您可以做的是使用 List 作为值(如您所试)。 也许这段代码会对您有所帮助:

var myDict = new Dictionary<string, List<double>>();
var newKey = "newKey";
var newValue = 0.5;
if (myDict.ContainsKey(newKey))
{
    var list = myDict[newKey];
    if (list.Count < 2)
    {
        // Add if not yet two entries
        myDict[newKey].Add(newValue);
    }
}
else
{
    myDict.Add(newKey, new List<double>() { newValue });
}

在您提出新问题后编辑:

foreach (var word in document)
{
    double myValue;
    // See if the value exists in the big directory
    var valueExists = bigDict.TryGetValue(word.Key, out myValue);
    if (valueExists)
    {
        // See if the testing dict has the same key
        if (testing.ContainsKey(word.Key))
        {
            // It does, add the value to the list
            if (word.Value < 2)
            {
                testing[word.Key].Add(myValue);
            }
        }
        else
        {
            // It doesn't, add the key with the value
            testing.Add(word.Key, new List<double>() { myValue });
        }
    }
}