日期:2014-05-18  浏览次数:20857 次

求一个统计元素程序
C# code


     A="|AA|BB|CC|DD|"
     B="|AA|FF|"
     C="|BB|DD|EE|"
     D="|CC|"
     E="|BB|DD|"




求一个程序能统计出A,B,C,D,E里各个元素出现了几次?
像这样展示出来:
AA(2) BB(3) CC(2) DD(3) EE(1) FF(1)

程序要注意各个元素是以 | 隔开的,可能元素里面的内容是没有规律的。

------解决方案--------------------
感觉和在下初学Linq的例子很像。
不过1L的感觉有点画蛇添足了,LZ要的只是一个字符串,没必要新建个匿名类哈。

在下稍微改了下:
C# code

            string A = "|AA|BB|CC|DD|";
            string B = "|AA|FF|";
            string C = "|BB|DD|EE|";
            string D = "|CC|";
            string E = "|BB|DD|";
            var result = A.Split('|').Concat(B.Split('|')).Concat(C.Split('|')).Concat(D.Split('|')).Concat(E.Split('|')).Where(a => string.IsNullOrEmpty(a) == false).GroupBy(a => a).Select(a => a.Key + "(" + a.Count() + ")");

            foreach (var str in result)
                Console.Write(str + " ");
            Console.WriteLine();

------解决方案--------------------
PS:每次看到
string.IsNullOrEmpty(a) == false
这种写法都有种说不清道不明的冲动……

又来写个一行的:
C# code

            string A = "|AA|BB|CC|DD|";
            string B = "|AA|FF|";
            string C = "|BB|DD|EE|";
            string D = "|CC|";
            string E = "|BB|DD|";
            (A + B + C + D + E).Split('|').GroupBy(s => s).ToList().ForEach(g => Console.Write(!string.IsNullOrEmpty(g.Key) ? g.Key + "(" + g.Count() + ") " : ""));
            Console.WriteLine();

------解决方案--------------------
var query=Regex.Split((A+B+C+D+E).Trim('|'),"\\|+").GroupBy(s=>s).Select(g=>string.Format("{0} : {1}",g.Key,g.Count()));
------解决方案--------------------
C# code

            string A = "|AA|BB|CC|DD|";
            string B = "|AA|FF|";
            string C = "|BB|DD|EE|";
            string D = "|CC|";
            string E = "|BB|DD|";
            var query = (A + B + C + D + E).Replace(@"||", @"|").Trim('|').Split('|').GroupBy(word => word).Select(word => new { word = word.Key, Count = word.Count() });
            query.ToList().ForEach(word => Console.WriteLine(word.word + "(" + word.Count + ")"));
 /*
 AA(2)
 BB(3)
 CC(2)
 DD(3) 
 EE(1) 
 FF(1)
*/

------解决方案--------------------
C# code
string A = "|AA|BB|CC|DD|";
                string B = "|AA|FF|";
                string C = "|BB|DD|EE|";
                string D = "|CC|";
                string E = "|BB|DD|";
                string result = string.Empty;
                Dictionary<string, int> dictionary = new Dictionary<string, int>();
                List<string> list = new List<string>() {A,B,C,D,E };
                foreach (string item in list)
                {
                    string temp_Str = item.Trim('|');
                    string[] string_arr = temp_Str.Split('|');
                    foreach (string temp_item in string_arr)
                    {
                        if (dictionary.ContainsKey(temp_item))
                        {
                            dictionary[temp_item] = dictionary[temp_item]+1;
                        }
                        else
                        {
                            dictionary.Add(temp_item,1);
                        }
                    }
                }