有没有一种方法可以将迭代器嵌套在for循环中

时间:2019-06-05 09:44:52

标签: python loops

所以我有一个设置了一些选项的迭代器,我想在一个简单的while或for循环中嵌套它,但似乎不允许这样做,这是我的代码。谢谢您的提前帮助。

struct Distance
{
    public void SetFeet(int feet) { Value = feet; }
    public void SetMiles(float miles) { Value = (int)(miles * 5280f); }
    public int GetFeet() { return Value; }
    public float GetMiles() { return Value / 5280f; }
    private int Value;
}

class Distances
{
    public Distance Dist1;//here
    public Distance Dist2;//and here
}

class Program
{
    static void Main(string[] args)
    {
        Distances distances = new Distances();
        distances.Dist1.SetFeet(1000);
        distances.Dist2.SetFeet(2000);

        Console.WriteLine("Dist1: {0}, Dist2: {1}", distances.Dist1.GetMiles(),
            distances.Dist2.GetMiles());

        Console.ReadLine();
    }
}

1 个答案:

答案 0 :(得分:2)

按现状,num_iterswhile循环中没有改变。

您可以使用for循环:

for _ in range(num_iters):
    with np.nditer(X, order='C', flags=['external_loop'], op_flags=['readwrite']) as it:
        for x in it:
            theta[0] = theta[0] - (alpha * (x[0] * theta[0] + x[1] * theta[1] - x[2]))/m
            theta[1] = theta[1] - ((alpha * (x[0] * theta[0] + x[1] * theta[1] - x[2]))*x[1])/m

您可以改用while循环,但需要自己增加计数器。