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

LINQ counting unique strings in a list and storing the values in a tuple

我有一个 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);

不能 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()});

结果:

所以有点像(原谅我的命名约定,它是一个 hack)

    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));

你可以这样做:

    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