I need to change the output of a variable in Python

Asked

Viewed 41 times

0

My variable a, imported from a file .mat, has as output:

array([[0],[1],[2],[3],[4],[5]]).

I need the output of the variable a be it: array([0, 1, 2, 3, 4, 5])

How do I make this correction/change in Python?

  • 1

    You can put a testable example because when you speak array in python is ambiguous, because you can’t tell if you’re talking thereof, thereof or thereof. Depending on what the problem may be solved in reading.

1 answer

0


Add the following line:

a = [i[0] for i in a]

You have a list of lists. This compression will take the first element of each list and put in a new list.

If there can be more than one item in each list... type a = [[0, 1, 2],[3, 4],[5]], you might want something more generic like:

a = [i for j in a for i in j]

  • Thank you very much!!! Problem solved!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.