0

The title kind of says it all. I have this (excerpt):

import numpy as np
import matplotlib.pyplot as plt

number_of_particles=1000

phi = np.arccos(1-2*np.random.uniform(0.0,1.,(number_of_particles,1)))
vc=2*pi
mux=-vc*np.sin(phi)

and I get out

[[-4.91272413]
 [-5.30620302]
 [-5.22400513]
 [-5.5243784 ]
 [-5.65050497]...]

which is correct, but I want it to be in the format

[-4.91272413 -5.30620302 -5.22400513 -5.5243784 -5.65050497....]

Feel like there should be a simple solution, but I couldn't find it.

Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
Mckenzie
  • 25
  • 1
  • 1
  • 8
  • 1
    It seems like you want to flatten a list of lists. – Ouroborus Mar 11 '19 at 05:46
  • @ouroborus Oh yes, I think you're right. I found something similar here: https://stackoverflow.com/questions/11264684/flatten-list-of-lists/11264799 – Mckenzie Mar 11 '19 at 05:51
  • Does this answer your question? [How do I make a flat list out of a list of lists?](https://stackoverflow.com/questions/952914/how-do-i-make-a-flat-list-out-of-a-list-of-lists) – Karl Knechtel Sep 06 '22 at 04:51

4 Answers4

0

Suppose your array is represented by the variable arr. You can do,

l = ''
for i in arr:
   l = l+i+' '
arr = [l]
Abhilash V
  • 69
  • 3
0

Use this command:

 new_mux = [i[0] for i in mux]

But I need it in an array, so then I add this

new_mux=np.array(new_mux)

and I get the desired output.

Mckenzie
  • 25
  • 1
  • 1
  • 8
0

There's a method transpose in numpy's array object

 mux.transpose()[0]
a.l.
  • 1,085
  • 12
  • 29
0

(I just noticed that this is a very old question, but since I have typed up this answer, and I believe it is simpler and more efficient than the existing ones, I'll post it...)

Notice that when you do

np.random.uniform(0.0,1.,(number_of_particles, 1))

you are creating a two-dimensional array with number_of_particles rows and one column. If you want a one-dimensional array throughout, you could do

np.random.uniform(0.0,1.,(number_of_particles,))

instead.

If you want to keep things 2d, but reshape mux for some reason, you can... well, reshape it:

mux_1d = mux.reshape(-1)

-1 here means "reshape it to one axis (because there’s just one number) and figure out automatically home many elements there should be along that axis (because the number is -1)."

Ture Pålsson
  • 6,088
  • 2
  • 12
  • 15