小编Sam*_*rad的帖子

如何在不使用循环的情况下生成循环数字序列?

我想生成一个循环的数字序列,如:我试过的[A B C A B C]任意长度N:

import numpy as np
def cyclic(N):
    x = np.array([1.0,2.0,3.0]) # The main sequence
    y = np.tile(x,N//3) # Repeats the sequence N//3 times 
    return y
Run Code Online (Sandbox Code Playgroud)

但我的代码的问题是,如果我输入任何不能被三个可分割的整数,那么结果的长度(N)将比我想象的要小.我知道这是一个非常新的问题,但我真的被卡住了

python numpy vectorization

4
推荐指数
1
解决办法
739
查看次数

如何用repr重建对象?

除了最后一部分,我的代码工作正常。我想用repr函数重新创建对象,但显然不起作用。我在这里和网络上都尝试过一切,但我仍然感到困惑。有没有办法做到这一点,如果语法是什么?

class Modulo(object):

    def __init__(self, grondtal, waarde = 0):
        self.grondtal = grondtal
        self.waarde = waarde % grondtal

    def __call__(self, m):
        return Modulo(self.grondtal, m)

    def __add__(self, other):
        return Modulo(self.grondtal, self.waarde + other.waarde)

    def __sub__(self, other):
        return Modulo(self.grondtal, self.waarde - other.waarde)

    def __mul__(self, other):
        return Modulo(self.grondtal, self.waarde * other.waarde)

    def __eq__(self, other):
        return self.waarde == other.waarde and self.grondtal == other.grondtal

    def __ne__(self, other):
        return not self.__eq__(other)

    def __str__(self):
        return  '[%s %% %s]' % (str(self.grondtal), str(self.waarde))

    def __repr__(self):
        return '%s' %Modulo(self.grondtal, self.waarde)
Run Code Online (Sandbox Code Playgroud)

python repr representation

2
推荐指数
1
解决办法
159
查看次数

标签 统计

python ×2

numpy ×1

repr ×1

representation ×1

vectorization ×1