LINQ计算列表中的唯一字符串并将值存储在元组中

时间:2015-07-31 10:39:31

标签: c# linq tuples

我有list<strings[]> doc并且我想将他们的计数存储到Tuple<string[], int[]>(唯一字,每个列表条目中n个字的数量)。到目前为止,我只获得了独特的字符串

public class Program
{
    public static void Main(string[] args)
    {
        List<string[]> doc = new List<string[]>();
        string[] a = { "That", "is", "a", "cat" };
        string[] b = { "That", "bat", "flew","over","the", "cat" };
        doc.Add(a);
        doc.Add(b);

        string[] strs  = doc
            .SelectMany(array => array)
            .Distinct()
            .ToArray();

        foreach (string val in strs)  {
            Console.WriteLine(val);
        }
    }
}

所以输出应该是

string[] a = { "That","is","a","cat","bat","flew","over"," the" };
int[] i_one = { 1,1,1,1,0,0,0,0 };
int[] i_two ={ 1,0,0,1,1,1,1,1 };

List<Tuple<string[],int[]> ret =  new List<string[],int[]>();

var b = new Tuple<string[],int[]>(a, i_one);
var c = new Tuple<string[],int[]>(a, i_two);
ret.Add(b);
ret.Add(c);

3 个答案:

答案 0 :(得分:6)

所以像(原谅我的命名惯例是一个黑客)

    string[] a = { "That", "is", "a", "cat" };
    string[] b = { "That", "bat", "flew", "over", "the", "cat" };

    var c = a.Union(b).Distinct();
    var a1 = (from ch in c select a.Count(r => r == ch));
     var b1 = (from ch in c select b.Count(r => r == ch));

答案 1 :(得分:2)

不是100%肯定这会完全解决你的问题,但我使用LinqPad敲了这个,但你的最终结果很难实现:

List<string[]> doc = new List<string[]>();
string[] a = { "That", "is", "a", "cat" };
string[] b = { "That", "bat", "flew","over","the", "cat" };
doc.Add(a);
doc.Add(b);

doc.SelectMany(array => array)
.GroupBy(x => x)
.Select(x=> new {Value = x.Key, Count = x.Count()});

结果:

enter image description here

答案 2 :(得分:1)

你可以这样做:

    var lines = new List<string[]>
    {
        new[] { "That", "is", "is", "a", "cat" },
        new[] { "That", "bat", "flew", "over", "the", "flew", "cat" }
    };

    var distinctWords = lines.SelectMany(strings => strings).Distinct().ToArray();

    var result = (
        from line in lines
        let lineWords = line.ToArray()
        let counts = distinctWords.Select(distinctWord => lineWords.Count(word => word == distinctWord)).ToArray()
        select new Tuple<string[], int[]>(distinctWords, counts)
    ).ToList();

请参阅demo