簡體   English   中英

C#如何將列表添加到字典

[英]C# How to add a list to a dictionary

我正在嘗試使用字典制作直方圖。 我從一個巨大的字符串開始,然后將其轉換為字符串數組,然后轉換為列表。 從那里,我需要創建一個包含我的字符串列表的直方圖目錄。

我還沒有太多,因為我才剛剛開始並且才剛剛開始學習字典。

static void Main(string[] args)
        {
            string Speach;
            Speach = "I say to you today, my friends, so even though we face the difficulties of today and tomorrow, I still have a dream. It is a dream deeply rooted in the American dream. " +
            "I have a dream that one day this nation will rise up and live out the true meaning of its creed: We hold these truths to be self-evident: that all men are created equal. " +
            "I have a dream that one day on the red hills of Georgia the sons of former slaves and the sons of former slave owners will be able to sit down together at the table of brotherhood. " +
            "I have a dream that one day even the state of Mississippi, a state sweltering with the heat of injustice, sweltering with the heat of oppression, will be transformed into an oasis of freedom and justice. " +
            "I have a dream that my four little children will one day live in a nation where they will not be judged by the color of their skin but by the content of their character. " +
            "I have a dream today. I have a dream that one day, down in Alabama, with its vicious racists, with its governor having his lips dripping with the words of interposition and nullification; one day right there in Alabama, little black boys and black girls will be able to join hands with little white boys and white girls as sisters and brothers. " +
            "I have a dream today. I have a dream that one day every valley shall be exalted, every hill and mountain shall be made low, the rough places will be made plain, and the crooked places will be made straight, and the glory of the Lord shall be revealed, and all flesh shall see it together. " +
            "This is our hope. This is the faith that I go back to the South with. With this faith we will be able to hew out of the mountain of despair a stone of hope. With this faith we will be able to transform the jangling discords of our nation into a beautiful symphony of brotherhood. " +
            "With this faith we will be able to work together, to pray together, to struggle together, to go to jail together, to stand up for freedom together, knowing that we will be free one day. " +
            "This will be the day when all of God's children will be able to sing with a new meaning, My country, 'tis of thee, sweet land of liberty, of thee I sing. Land where my fathers died, land of the pilgrim's pride, from every mountainside, let freedom ring. " +
            "And if America is to be a great nation this must become true. So let freedom ring from the prodigious hilltops of New Hampshire. Let freedom ring from the mighty mountains of New York. Let freedom ring from the heightening Alleghenies of Pennsylvania! " +
            "Let freedom ring from the snowcapped Rockies of Colorado! Let freedom ring from the curvaceous slopes of California! But not only that; let freedom ring from Stone Mountain of Georgia! " +
            "Let freedom ring from Lookout Mountain of Tennessee! Let freedom ring from every hill and molehill of Mississippi. From every mountainside, let freedom ring. " +
            "And when this ha   ppens, when we allow freedom to ring, when we let it ring from every village and every hamlet, from every state and every city, we will be able to speed up that day when all of God's children, black men and white men, Jews and Gentiles, Protestants and Catholics, will be able to join hands and sing in the words of the old Negro spiritual, Free at last! free at last! thank God Almighty, we are free at last!";

            string[] SpeachSplit = Speach.Split();
            List<string> SpeachList = SpeachSplit.OfType<string>().ToList();

            Dictionary<string, int> Historgram = new Dictionary<string, int>();

//This is where I get confused??
            for (var i = 0; i < SpeachList.Count; i++)
            {
                Historgram.Add(SpeachList[i],//? );
            }

            Console.ReadLine();

我的問題是,如何在將字典與字符串鍵和整數值保持一致的同時,將字符串列表添加到字典中?

如果我正確理解您要實現的目標,則此解決方案應該有效:

string[] SpeachSplit = Speach.Split();
Dictionary<string, int> Historgram = new Dictionary<string, int>();

// loop through all words
for (var i = 0; i < SpeachSplit.Length; i++)
{
    string word = SpeachSplit[i];
    // check if your word is already in the dictionary
    if(!Historgram.ContainsKey(word))
    {
        // if it is not in the dictionary, add it to dictionary with 0 occurrences
        Historgram.Add(word, 0);
    }
    // add one to the number of occurrences
    Historgram[word]++;
}
// at this point dictionary contains words as keys and number of occurrences as value

基本上這個

  1. 按空間分割
  2. 轉換為小寫
  3. 使用正則表達式替換所有非字母數字
  4. 組和計數
  5. 項目到字典

var result = Speach.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries)
                   .Select(x => x.ToLower()))
                   .Select(x => Regex.Replace(x, "[^a-zA-Z0-9-]", ""))
                   .GroupBy(x => x)
                   .ToDictionary(x => x.Key, x => x.Count());

完整的演示在這里

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM