C# 拆分列表简化代码
C# Split lists simplify code
早上好,
我来自 Python 环境并转向 c#。
我正在将一个较宽的列表拆分为具有规定长度的较窄的列表。
有没有办法简化下面的代码?
我的猜测是它有点慢并且没有正确遵循 c# 通用编码规则。
List<object> B = new List<object>();
for(int i = 0; i < SD_Data.Count / 314; i++) {
var SD_Input = SD_Data.Skip(314 * i).Take(314 * i + 313);
B.Add(SD_Input);
}
A = B;
我发现了这个有用的方法
public static IEnumerable<IEnumerable<T>> Chunk<T > (this IEnumerable<T> source, int chunksize)
{
while (source.Any())
{
yield return source.Take(chunksize);
source = source.Skip(chunksize);
}
}
var z = Chunk(x, 10);
但它确实引发了以下错误:
Error (CS1513): } expected (line 69)
Error (CS1518): Expected class, delegate, enum, interface, or struct (line 88)
Error (CS1518): Expected class, delegate, enum, interface, or struct (line 88)
Error (CS1518): Expected class, delegate, enum, interface, or struct (line 89)
Error (CS1518): Expected class, delegate, enum, interface, or struct (line 89)
Error (CS1518): Expected class, delegate, enum, interface, or struct (line 90)
Error (CS1518): Expected class, delegate, enum, interface, or struct (line 91)
Error (CS1518): Expected class, delegate, enum, interface, or struct (line 92)
Error (CS1518): Expected class, delegate, enum, interface, or struct (line 94)
Error (CS1001): Identifier expected (line 112)
Error (CS1001): Identifier expected (line 114)
Error (CS1022): Type or namespace definition, or end-of-file expected (line 115)
我正在研究 McNeel 的 Rhinoceros 软件的 Grasshopper 界面。
提前致谢!
使用该方法的语法如下:
var B = SD_Data.Chunk(314);
块被声明为 Extension Method。
如果要以标准方式调用 Chunk 方法,请删除 this
关键字
public static IEnumerable<IEnumerable<T>> Chunk<T > (IEnumerable<T> source, int chunksize)
{
while (source.Any())
{
yield return source.Take(chunksize);
source = source.Skip(chunksize);
}
}
终于想通了
这个帖子对我帮助很大。
Working with arrays/list with C# components in Grasshopper 3D
出现此问题是因为该方法是在另一个方法(即 RunScript)中定义的。
解决方案是将其写在
// <Custom additional code>
code here
// <Custom additional code>
所以结果是:
private void RunScript(List<Point3d> SrcPts, List<string> Instrument, List<object> SD_Data, List<double> XY_Angles, List<string> Octave, ref object A, ref object B)
{
B = Chunk(SD_Data, 314);
}
// <Custom additional code>
public static IEnumerable<IEnumerable<T>> Chunk<T > (IEnumerable<T> source, int chunksize)
{
while (source.Any())
{
yield return source.Take(chunksize);
source = source.Skip(chunksize);
}
}
// <Custom additional code>
感谢大家的宝贵提示。
早上好,
我来自 Python 环境并转向 c#。
我正在将一个较宽的列表拆分为具有规定长度的较窄的列表。
有没有办法简化下面的代码? 我的猜测是它有点慢并且没有正确遵循 c# 通用编码规则。
List<object> B = new List<object>();
for(int i = 0; i < SD_Data.Count / 314; i++) {
var SD_Input = SD_Data.Skip(314 * i).Take(314 * i + 313);
B.Add(SD_Input);
}
A = B;
我发现了这个有用的方法
public static IEnumerable<IEnumerable<T>> Chunk<T > (this IEnumerable<T> source, int chunksize)
{
while (source.Any())
{
yield return source.Take(chunksize);
source = source.Skip(chunksize);
}
}
var z = Chunk(x, 10);
但它确实引发了以下错误:
Error (CS1513): } expected (line 69)
Error (CS1518): Expected class, delegate, enum, interface, or struct (line 88)
Error (CS1518): Expected class, delegate, enum, interface, or struct (line 88)
Error (CS1518): Expected class, delegate, enum, interface, or struct (line 89)
Error (CS1518): Expected class, delegate, enum, interface, or struct (line 89)
Error (CS1518): Expected class, delegate, enum, interface, or struct (line 90)
Error (CS1518): Expected class, delegate, enum, interface, or struct (line 91)
Error (CS1518): Expected class, delegate, enum, interface, or struct (line 92)
Error (CS1518): Expected class, delegate, enum, interface, or struct (line 94)
Error (CS1001): Identifier expected (line 112)
Error (CS1001): Identifier expected (line 114)
Error (CS1022): Type or namespace definition, or end-of-file expected (line 115)
我正在研究 McNeel 的 Rhinoceros 软件的 Grasshopper 界面。
提前致谢!
使用该方法的语法如下:
var B = SD_Data.Chunk(314);
块被声明为 Extension Method。
如果要以标准方式调用 Chunk 方法,请删除 this
关键字
public static IEnumerable<IEnumerable<T>> Chunk<T > (IEnumerable<T> source, int chunksize)
{
while (source.Any())
{
yield return source.Take(chunksize);
source = source.Skip(chunksize);
}
}
终于想通了
这个帖子对我帮助很大。
Working with arrays/list with C# components in Grasshopper 3D
出现此问题是因为该方法是在另一个方法(即 RunScript)中定义的。 解决方案是将其写在
// <Custom additional code>
code here
// <Custom additional code>
所以结果是:
private void RunScript(List<Point3d> SrcPts, List<string> Instrument, List<object> SD_Data, List<double> XY_Angles, List<string> Octave, ref object A, ref object B)
{
B = Chunk(SD_Data, 314);
}
// <Custom additional code>
public static IEnumerable<IEnumerable<T>> Chunk<T > (IEnumerable<T> source, int chunksize)
{
while (source.Any())
{
yield return source.Take(chunksize);
source = source.Skip(chunksize);
}
}
// <Custom additional code>
感谢大家的宝贵提示。