【问题标题】:Counting the occurrences of every duplicate words in a string using dictionary in c# [closed]使用c#中的字典计算字符串中每个重复单词的出现次数[关闭]
【发布时间】:2014-06-11 03:32:45
【问题描述】:

编辑:我详细阐述了我的问题 ..solutions are here for one fix duplicate word ..我被问及每个重复的单词

我是新手...可能不是一个好问题。 ......

这是字符串

string str = "this this is is a a string"

在面试中,我被要求将每个重复关键字的计数存储在通用字典中,然后按顺序显示它们

例如“is”关键字的出现次数为2

类似链接
Find the most occurrence of a character in string C#?这是关于寻找角色

Finding occurrences of words in text which are in a list words 这是在 python

Remove occurrences of duplicate words in a stringjavascript 中的 this

How to use the string.match method to find multiple occurrences of the same word in a string? 不相关

..请建议

【问题讨论】:

  • 字典包含一个键和一个值。您将需要拆分字符串,并保存字典中每个单词的计数。如果您遇到特定问题,请将您的代码与您遇到的问题一起发布。
  • 你的问题很好。你只是没有表现出任何努力去弄清楚。您基本上是在要求 SO 为您编写代码。
  • 我简化了我的问题......

标签: c# string dictionary


【解决方案1】:

使用 LINQ 非常简单:

string str = "this is is a string";
string[] words = str.Split(new[] { " " }, StringSplitOptions.RemoveEmptyEntries);

(您也可以像@markieo 在他的回答中那样使用Regex.Split(str, @"\W+")。不同之处在于它还会检测被引号和其他标点符号包围的单词。感谢@JonB 在 cmets 中指出这一点。)

Dictionary<string, int> statistics = words
    .GroupBy(word => word)
    .ToDictionary(
        kvp => kvp.Key, // the word itself is the key
        kvp => kvp.Count()); // number of occurences is the value
int isCount = statistics["is"]; // returns 2

编辑:

我正在发布代码以满足您的增强要求。但是对于未来,只需发布​​另一个问题,而不是修改已回答的问题!

// retrieving all duplicate words
string[] duplicates = statistics
    .Where(kvp => kvp.Value > 1)
    .Select(kvp => kvp.Key)
    .ToArray();

// counting all duplicates and formatting it into a list in the desired output format
string output = String.Join(
    "\n", 
    statistics
        .Where(kvp => kvp.Value > 1)
        .Select(kvp => 
            String.Format(
                "count(\"{0}\") = {1}", 
                kvp.Key, 
                kvp.Value))
        .ToArray() // this line is only needed on older versions of .NET framework
);

【讨论】:

  • "这个怎么样?这包含三个单词'this',但你的代码不会捕捉到那个"
  • OP 没有明确指定要求。 thisthis-this 呢?任务是构建一个通用字典,它是否应该包含(并计算)所有子字符串?
  • 无论如何Regex.Split 都可以用于此目的。我会更新我的答案。
  • “这个”和“这个”。不是两个不同的词。我认为这个问题已经足够清楚了。在空格上拆分很容易(为您的编辑+1!)
  • @JonB 这是一个有效的观点,乔恩。我在更新的答案中包含了这方面。
【解决方案2】:

试试上面的:

    string str = "this this is is a a string";
    private int count(string key)
    {
        string[] ar = str.Split(new[] { " " }, StringSplitOptions.RemoveEmptyEntries);
        Dictionary<int, string> d = new Dictionary<int, string>();

        for (int i = 0; i < ar.Length; i++)
            d.Add(i, ar[i]);
       return d.Where(x => x.Value == key).ToList().Count;
    }

函数调用:

count(str, "is");

【讨论】:

  • 每个重复的单词怎么样..我需要对每个重复的单词进行硬编码吗? .我编辑了我的问题
  • @user3487944 因为你已经改变了你的问题,我现在也会改变我的答案。这篇文章是针对原始问题的。
  • 所以你的意思是我必须通过硬编码重复的单词来计算它的出现..
  • @user3487944 正是您在问题“count("this") =2 count("is") =2 count("a") =2" 中提出的问题。
  • NO .No ..这只是一个示例 ..如果有更多重复的单词 ..
【解决方案3】:

您可以使用此代码获取字符串中所有单词的数组。

static string[] SplitWords(string s)
{
     return Regex.Split(s, @"\W+");
}

然后您可以使用 foreach 循环来计算单词出现在数组中的所有时间。 像这样:

int count = 0;
foreach(string s in SplitWords("This is is a string"){
    if(s == "is"){
    count++;
    }
}

int count 是单词出现在字符串中的次数。

来源: http://www.dotnetperls.com/split

【讨论】:

  • 每个重复的单词怎么样..我需要对每个重复的单词进行硬编码吗? .我编辑了我的问题
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-02-01
  • 2021-12-27
  • 1970-01-01
  • 2019-07-31
  • 2016-06-28
  • 2017-09-04
相关资源
最近更新 更多