gpt4 book ai didi

c# - 从解析的字符串构建多级数组数组/锯齿状数组

转载 作者:行者123 更新时间:2023-11-30 17:56:15 25 4
gpt4 key购买 nike

我一直在尝试扩展我的代码以合并基于某些字符串的第 3 级数组,这是我一直在尝试做的事情,但我只能根据我对代码的理解将其变成第 2 级数组。

string a = "{50,8,10} Grade 1; {70,10,45} Grade 2; {80,20,65} Grade 3: {90,100,23} Grade 4; {98,99,32} Grade 5; {100,1000,7} Grade 6";

int[][][] test =
a.Split(':')
.Select(t => Regex.Matches(t, @"(?<={).*?(?=})"))
.Cast<MatchCollection>()
.Select(m => m.Cast<Match>()
.Select(n => n.ToString().Split(',')
.Select(int.Parse))
.ToArray())
.ToArray()
.ToArray();

所以数组的每一部分看起来像这样

        //int[][][] { {50,8,10} Grade 1; {70,10,45} Grade 2; {80,20,65} Grade 3 }
// int[][] { {50,8,10},{70,10,45},{80,20,65} }
// int[] {50,8,10}

无论如何,我对编程还是很陌生,我一直在深入研究它并边学边做。如果除了使用数组之外还有更有效的方法来处理这个问题,我愿意接受建议,

最佳答案

您的代码实际上几乎是正确的。我认为只有一个 )不合适(我也稍微清理了格式)。

int[][][] test = a.Split(':')
.Select(t => Regex.Matches(t, @"(?<={).*?(?=})"))
.Cast<MatchCollection>()
.Select(m => m.Cast<Match>()
.Select(n => n.ToString().Split(',')
.Select(int.Parse)
.ToArray())
.ToArray())
.ToArray();

int[][][]虽然这不是最有效的数据结构 - 您可能需要考虑 Dictionary<string, List<int>>相反。

关于c# - 从解析的字符串构建多级数组数组/锯齿状数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14382811/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com