LINQ查询以查找重复次数最多的多维数组的字符串

我们

我编写了一个函数,该函数为我提供了一个具有多个正则表达式字符串的Match多维数组。(FileCheck [] [])

  1. FileCheck [0] //此字符串[]包含所有文件名
  2. FileCheck [1] //此字符串[]为0或1,取决于找到的正则表达式匹配项。
  3. FileCheck [2] //此字符串[]包含第一个找到的Regex的索引。

        foreach (string File in InputFolder)
        {
            int j = 0;
            FileCheck[0][k] = Path.GetFileName(File);
            Console.WriteLine(FileCheck[0][k]);
            foreach (Regex Filemask in Filemasks)
            {
                if (string.IsNullOrEmpty(FileCheck[1][k]) || FileCheck[1][k] == "0")
                {
                    if (Filemask.IsMatch(FileCheck[0][k]))
                    {
                        FileCheck[1][k] = "1";
                        FileCheck[2][k] = j.ToString(); // This is the Index of the Regex thats Valid
                    }
                    else
                    {
                        FileCheck[1][k] = "0";
                    }
                    j++;
                }
                Console.WriteLine(FileCheck[1][k]);
            }
            k++;
        }
        Console.ReadLine();
    
        // I need the Index of the Regex with the most valid hits
    

我正在尝试编写一个函数,该函数为我提供了重复次数最多的RegexIndex字符串。这是我尝试过的,但是没有用:((我只得到重复次数最多的字符串的计数,而不是字符串本身的计数)

        // I need the Index of the Regex with the most valid hits
        var LINQ = Enumerable.Range(0, FileCheck[0].GetLength(0))
            .Where(x => FileCheck[1][x] == "1")
            .GroupBy(x => FileCheck[2][x])
            .OrderByDescending(x => x.Count())
            .First().ToList();
        Console.WriteLine(LINQ[1]);

示例数据

        string[][] FileCheck = new string[3][];
        FileCheck[0] = new string[]{ "1.csv", "TestValid1.txt", "TestValid2.txt", "2.xml", "TestAlsoValid.xml", "TestValid3.txt"};
        FileCheck[1] = new string[]{ "0","1","1","0","1","1"};
        FileCheck[2] = new string[]{ null, "3", "3", null,"1","2"};

在此示例中,我需要作为Linq查询的结果:

 string result = "3";
我的名字

使用您当前的代码,用“ Key”替换“ ToList()”将达到目的。

var LINQ = Enumerable.Range(0, FileCheck[0].GetLength(0))
            .Where(x => FileCheck[1][x] == "1")
            .GroupBy(x => FileCheck[2][x])
            .OrderByDescending(x => x.Count())
            .First().Key;

由于未找到的值的索引为null,因此您也可以过滤掉null值并跳过FileCheck [1]数组。例如:

var maxOccurringIndex = FileCheck[2].Where(ind => ind != null)
        .GroupBy(ind=>ind)
        .OrderByDescending(x => x.Count())
        .First().Key;

但是,仅是建议,您可以使用类而不是嵌套数组,例如:

class FileCheckInfo
{
    public string File{get;set;}
    public bool Match => Index.HasValue;
    public int? Index{get;set;}

    public override string ToString() => $"{File} [{(Match ? Index.ToString() : "no match")}]";
}

假设InputFolder是一个可枚举的字符串和Filemasks一个可枚举的“ Regex”,则一个数组可以填充为:

FileCheckInfo[] FileCheck = InputFolder.Select(f=>
    new FileCheckInfo{
        File = f, 
        Index = Filemasks.Select((rx,ind) => new {ind, IsMatch = rx.IsMatch(f)}).FirstOrDefault(r=>r.IsMatch)?.ind
        }).ToArray();

获得最大的发生将是几乎相同的:

var maxOccurringIndex = FileCheck.Where(f=>f.Match).GroupBy(f=>f.Index).OrderByDescending(gr=>gr.Count()).First().Key;

编辑PS,以上全部假设您需要重用结果,如果您只需要查找最大出现次数,则最好采用Martin建议的方法!如果目标仅是获得最大的出现次数,则可以使用:

var maxOccurringIndex = Filemasks.Select((rx,ind) => new {ind, Count = InputFolder.Count(f=>rx.IsMatch(f))})
        .OrderByDescending(m=>m.Count).FirstOrDefault()?.ind;

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

查找字符串中重复次数最多的单词

查找数组中重复次数最多的算法

从包含python中句子的字符串中找到重复次数最多的单词

如何找到字符串中重复次数最多的单词?

查找矩阵中重复次数最多的行

查找没有地图的重复次数最多的整数

获取 Linq 中重复次数最多的记录数

查找子字符串在字符串中连续出现的次数最多

获取数组中重复次数最多的元素,如果重复次数最多,请按字母顺序依次获取结果

查找字符串中出现次数最多的字符

查找字符串中出现次数最多的字符

查找哪个字符出现在字符串中的次数最多

在数组中查找重复项,在 java 中填充重复次数最多的第二个数组

使用powershell在.txt文件中查找出现次数最多的字符串

在 ArrayList 中查找出现次数最多的字符串

SQL选择列值重复次数最多

获取表中重复次数最多的名称

[LUA]获取表中重复次数最多的整数

列表中重复次数最多的元素

找出名字重复次数最多的城市

仅显示在 seaborn 上重复次数最多的元素

如何在字符串中查找重复字符,并且重复次数应为 2

给定字符串中出现次数最多的词

搜索出现次数最多的字符串

在字符串上查找重复的单词并计算重复次数

如何使用python查找字符串中重复次数第二多的字符

查找字符串中出现次数最多的字母,然后仅打印字母,而不是计数

python中字符串变量的重复次数

包含一组中出现次数最多的字符串的最短字符串