小编che*_*ang的帖子

如何将二维数组更改为一维数组

我正在尝试将二维数组更改为一维数组,我的代码如下:

x = np.array([[1, 2, 4], [3, 4], [1,2,3,4,5,6,7]])
x = x.flatten()
Run Code Online (Sandbox Code Playgroud)

但是,我发现展平函数在以下情况下效果很好

x = np.array([[1, 2], [3, 4]])
Run Code Online (Sandbox Code Playgroud)

但它不起作用

x = np.array([[1, 2, 4], [3, 4], [1,2,3,4,5,6,7]])
Run Code Online (Sandbox Code Playgroud)

谁能帮我改变

np.array([[1, 2, 4], [3, 4], [1,2,3,4,5,6,7]]) 
Run Code Online (Sandbox Code Playgroud)

np.array([[1, 2, 4, 3, 4, 1,2,3,4,5,6,7])
Run Code Online (Sandbox Code Playgroud)

谢谢

python arrays numpy

6
推荐指数
2
解决办法
2万
查看次数

如何在 python 中覆盖 __mul__ 函数

我用 python 写了一个这样的类

class Vector(object):
def __init__(self, coordinates):
    try:
        if not coordinates:
            raise ValueError
        self.coordinates = tuple(coordinates)
        self.dimension = len(coordinates)

    except ValueError:
        raise ValueError('The coordinates must be nonempty')

    except TypeError:
        raise TypeError('The coordinates must be an iterable')

def __add__(self,v):
    v1 = np.array(self.coordinates)
    v2 = np.array(v.coordinates)
    result = v1 + v2
    return result.tolist()

def __sub__(self, other):
    v1 = np.array(self.coordinates)
    v2 = np.array(other.coordinates)
    result = v1 - v2
    return result.tolist()

def __mul__(self, other):
    return other * np.array(self.coordinates)

def multiply(self,other):
    v = Decimal(str(other)) …
Run Code Online (Sandbox Code Playgroud)

python overwrite operation

3
推荐指数
1
解决办法
1750
查看次数

标签 统计

python ×2

arrays ×1

numpy ×1

operation ×1

overwrite ×1