【问题标题】:How to translate generators in this algorithm from Python to C#?如何将此算法中的生成器从 Python 转换为 C#?
【发布时间】:2022-10-24 02:44:47
【问题描述】:

我正在将 Python 代码翻译成 C#。任何人都可以帮忙吗?

    def transpose_to_hilbert_integer(self, x:list) -> int: # x is a list of ints
    """Restore a hilbert integer (`h`) from its transpose (`x`).

    Args:
        x (list): transpose of h
            (n components with values between 0 and 2**p-1)

    Returns:
        h (int): integer distance along hilbert curve
    """
    x_bit_str = [HilbertsCurve._binary_repr(x[i], self.p) for i in range(self.n)]
    print(x_bit_str)
    h = int(''.join([y[i] for i in range(self.p) for y in x_bit_str]), 2)
    return h

翻译成这样:

public int Transpose_to_hilbert_integer(List<int> x)
    {
        List<string> x_bit_str = new List<string>();
        for (int i = 0; i < this.n; i++)
        {
            x_bit_str.Add(HilbertsCurve._binary_repr(x[i], this.p));
        }

// second part of the code. Make h.
        return h;
    }

陷入双重列表理解

【问题讨论】:

    标签: python c# python-3.x algorithm


    【解决方案1】:

    我做到了,我在 C# 中使用了普通的嵌套 for 循环。双嵌套。并且 += 连接而不是 .join() 到一个空字符串。

    【讨论】:

      猜你喜欢
      • 2016-01-31
      • 2019-11-29
      • 1970-01-01
      • 2016-08-09
      • 1970-01-01
      • 2015-05-30
      • 2013-09-23
      • 2020-10-27
      • 2019-07-23
      相关资源
      最近更新 更多