我有反映X,Y,Z坐标属性的单独值列表。
List<double> PointX;
List<double> PointY;
List<double> PointZ;
是否可以创建“3D列表”?这样我就会
Point = (PointX,PointY,PointZ)
答案 0 :(得分:3)
您可以使用嵌套的Zip
调用从每个列表中获取值:
var points =
PointX.Zip(PointY.Zip(PointZ, (y,z) => new {y, z})
, (x, yz) => new Point(x, yz.y, yz.z));
答案 1 :(得分:1)
关于
的内容public class Point3D {
public double X {get; set;}
public double Y {get; set;}
public double Z {get; set;}
}
List<Point3D> list = new List<Point3D>();
for (int i = 0; i < PointX.Count; i++) {
list.Add(new Point3D { X = PointX[i], Y = PointY[i], Z = PointZ[i] });
}
答案 2 :(得分:1)
您可以使用元组列表,例如
var list = new List<Tuple<double, double, double>>(){new Tuple<double, double, double>(3, 4, 5)};
我也喜欢rboe的解决方案,但我会考虑使Point3D类不可变;
public class Point3D
{
public double X { get; private set; }
public double Y { get; private set; }
public double Z { get; private set; }
public Point3D(double x, double y, double z)
{
X = x;
Y = y;
Z = z;
}
}
答案 3 :(得分:0)
var points = PointX.Zip(PointY, (x, y) => new { X = x, Y = y })
.Zip(PointZ, (xy, z) => new { X = xy.X, Y = xy.Y, Z = z });
var point = points.First();
double px = point.X;
double py = point.Y;
double pz = point.Z;
您可以使用Zip
将3个集合加入匿名类型。在这个例子中,我将x,y压缩成一个匿名类型,然后我用z集合压缩该类型,以返回一个匿名的集合,表示x,y和z。
答案 4 :(得分:0)
基于Zip声音的答案相当不错。但是,您也可以为List创建扩展方法。
类似的东西:
public static List<double> Xs(this List<Point3D> pts) {
List<double> xs = new List<double>(pts.Count);
foreach (Point3D pt in pts) {
xs.Add(pt.X);
}
return xs;
}
public static List<double> Ys(this List<Point3D> pts) {
List<double> ys = new List<double>(pts.Count);
foreach (Point3D pt in pts) {
ys.Add(pt.Y);
}
return ys;
}
public static List<double> Zs(this List<Point3D> pts) {
List<double> zs = new List<double>(pts.Count);
foreach (Point3D pt in pts) {
zs.Add(pt.Z);
}
return zs;
}