存储到Dictionary< string,List< string>>中的多个列表 [英] multiple lists stored to Dictionary<string, List<string>>

查看:92
本文介绍了存储到Dictionary< string,List< string>>中的多个列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将一本英文字典(在这个特定字典中约有109,000个字)存储到一个Dictionary>数据结构中,我很难弄清楚如何做到这一点。我目前的做法是将单词的第一个字符作为键值存储,然后将该单词存储在列表(wordlist)中。当钥匙从a更改为b时,(或b到c等)是我被卡住的地方,因为无法弄清楚该列表的功能。这是我的努力。任何帮助都非常感激。

I am trying to store an English dictionary (about 109,000 words in this particular dictionary) to a Dictionary> data structure and I'm having difficulty figuring out how to do it. My current approach idea is to store first character of the word as the key value and then store the word in a list ("wordlist"). When the key changes from 'a' to 'b', (or 'b' to 'c', etc.) is where I'm stuck, as am having trouble figuring out what to do with the list. Here's my effort to this point. Any help is highly appreciated.

public Dictionary<char, IEnumerable<string>> populateWordDictionary()
    {
        Dictionary<char, IEnumerable<string>> wordDictionary = new Dictionary<char, IEnumerable<string>>();
        //List<string> wordList;
        connect = new SqlConnection(connectionString);
        SqlCommand find = new SqlCommand("Select * FROM English order by Word", connect);

        // starting with first record, store each word into the List<string> wordlist
        SqlDataReader dr = null;
        try
        {
            connect.Open();
            dr = find.ExecuteReader();
            char key;
            char keyStartingPosition = 'a';
            List<string> wordList = new List<string>();
            while (dr.Read())
            {
                // if a word is present
                if (!dr.IsDBNull(0))
                {
                    // set Dictionary key value to the first letter in the word being evaluated
                    key = Convert.ToChar(dr[1].ToString().Substring(0, 1));

                    // if the key value is not the same as the starting position, clear the list
                    // i.e., we've advanced to the next letter in the alphabet
                    if (key != keyStartingPosition)
                    {   
                        // add the current list to the dictionary
                        wordDictionary.Add(key, wordList);
                        // advance key starting position to the new key value
                        keyStartingPosition = key;
                        // and clear current content of wordList
                        wordList.Clear();
                    }

                    // if the first letter of the word list hasn't advanced in the alphebet
                    // simply store the word to the current list.
                    if (key == keyStartingPosition)
                    {
                        wordList.Add(dr[1].ToString());
                    }
                }
            }
        }
        catch (Exception ex)
        {

        }
        finally
        {
            connect.Close();
        }

        return wordDictionary;

    }


推荐答案

方法很好,除了 wordList.Clear()部分:您保持重新使用相同的列表,并将其副本插入所有键。结果,所有的密钥最后都是与最后一个密钥相同的单词列表。

Your general approach is fine, except for the wordList.Clear() part: you keep re-using the same list, and insert its copy at all keys. As the result, all keys end up with the same list of words as the last key.

要解决此问题,请将 wordList.Clear )

wordList = new List<string>();

并在 wordDictionary.Add line:

if (key != keyStartingPosition)
{   
    wordList = new List<string>();
    // add the current list to the dictionary
    wordDictionary.Add(key, wordList);
    // advance key starting position to the new key value
    keyStartingPosition = key;
}

另请注意,由于添加发生在给定信中遇到第一个字,您需要将 keyStartingPosition ='a'替换为 keyStartingPosition ='@'或不能启动真实的另一个字母字。

Also note that since the addition happens upon encountering the first word in a given letter, you need to replace keyStartingPosition = 'a' with keyStartingPosition = '@' or another letter that cannot start a real word.

这篇关于存储到Dictionary&lt; string,List&lt; string&gt;&gt;中的多个列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆