简体   繁体   中英

Flatten a numpy array

I am solving a ODE as follows:

import numpy as np
import scipy as sp
import math
from math import *
from scipy.integrate import odeint
import matplotlib.pyplot as plt

def g(y, x):
    y0 = y[0]
    return x   #formula##

# Initial conditions on y, y' at x=0
init = 0   #value##
# First integrate from 0 to 100
xplotval=np.linspace(4,8,4)  #linspacefunction
print(xplotval)

I am getting output as:

[[ 7.         ]
 [ 5.76455273 ]
 [ 5.41898906 ]
 [ 6.49185668 ]]

I'd like to output a single dimensional array as follows:

[7., 5.76455273, 5.41898906, 6.49185668]

How can I?

Maybe you want flatten :

print(xplotval.flatten())

Unless you actually want the transposed vector , which you would get with numpy.transpose :

print(np.transpose(xplotval))

您可以简单地使用列表理解,例如:

oneD = [l[0] for l in xplotval]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM