1233023
1233023

Reputation: 321

How to simplify an array in python

When I append elements to an empty list, it tends to overcomplicate.

I get:

A = array([[1],[1],...,[1]])

I want:

A = array([1,1...,1])

Upvotes: 2

Views: 3353

Answers (3)

Drunken_Economist
Drunken_Economist

Reputation: 78

To avoid this when you're adding elements to the list, you can use extend instead of append

Upvotes: 0

Taufiq Rahman
Taufiq Rahman

Reputation: 5714

you can try an inner loop to append to another list:

A = ([[1],[4],[5]])
b = []
for x in A:
    for i in x:
     b.append(i)        
print(b)

output:

[1, 4, 5]

Upvotes: 0

SparkAndShine
SparkAndShine

Reputation: 18045

Use numpy.ndarray.flatten,

import numpy as np

A = np.array([[1], [1], [1]])
B = A.flatten()

Upvotes: 5

Related Questions