我正在尝试将二维数组更改为一维数组,我的代码如下:
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 写了一个这样的类
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)