了解有关字典和数组的python语法以转换为C#

时间:2018-10-31 15:03:04

标签: c# python arrays dictionary

我正在将一些代码从python转换为c#,并且停留在几行上,我正在使用的python代码是:

def getDirection(self):
    #find a new vector that is at a 90 degree angle

    #define dictionary
    d = {}
    #create an list of possible 90 degree vectors
    arrPts = [(1,0,0), (-1,0,0), (0,1,0), (0,-1,0)]
    vec = self.vec
    vec = rs.VectorUnitize(vec)

    #find the distance between the self vec
    #position and one of the 4 90 degree vectors
    #create a dictionary that matches the distance with the 90 degree vector
    for i in range(4):
        dist = rs.Distance(vec, arrPts[i])
        d[dist] = arrPts[i]
    #sort the dictionary.  This function converts it to an array
    #sort by the distances, "value"/item 0
    dSorted = sorted(d.items(), key=lambda value: value[0])

    #select the second item in the array which is one of the 90 degree vectors
    posVec = dSorted[0][1]
    return posVec

到目前为止,我已经在C#中获得了这个

private Vector3d GetDirection()
        {
            // define dictionary
            Dictionary<double, Vector3d> d = new Dictionary<double, Vector3d>();

            Vector3d[] arrPts = new Vector3d[] {
                new Vector3d(1, 0, 0),
                new Vector3d(-1, 0, 0),
                new Vector3d(0, 1, 0),
                new Vector3d(0, -1, 0),
                new Vector3d(0, 0, 1),
                new Vector3d(0, 0, -1) };

            _vec = Vec;
            _vec.Unitize();

            // find the distance between the self vec position and one of the 6 90 degree vectors
            // create a dictionary that matches the distance with the 90 degree vector

            for (int i = 0; i < arrPts.Length; i++)
            {
                double dist = Math.Sqrt(
                    ((_vec.X - arrPts[i].X) * (_vec.X - arrPts[i].X)) +
                    ((_vec.Y - arrPts[i].Y) * (_vec.Y - arrPts[i].Y)) +
                    ((_vec.Z - arrPts[i].Z) * (_vec.Z - arrPts[i].Z)));

                d.Add(dist, arrPts[i]);
            }

我不确定的行紧靠python一侧的dist = rs.Distance(vec, arrPts[i])行,其中包括以下行:

d[dist] = arrPts[i]
dSorted = sorted(d.items(), key=lambda value: value[0])
posVec = dSorted[0][1]

我可以在这些方面获得一些帮助吗?谢谢!

0 个答案:

没有答案