Home python Record two-dimensional array in one-dimensional Python

Record two-dimensional array in one-dimensional Python

Author

Date

Category

There is a two-dimensional array:

l = [[2], [3], [4], [5]]

How to write it in this form?:

l = [2, 3, 4, 5]

Answer 1

Here is an example code:

l = [elem [0] for elem in l]

If there is more than 1 element in the attached list:

answer = []
For i in L:
  Answer.APPEND (I)

Answer 2, Authority 400%

With any number of elements in the second dimension

[a for b in l for a in b]

Answer 3, Authority 200%

matrix = [[1, 2, 3, 4],
     [5, 6, 7, 8],
     [9, 10, 11, 12],
     [13, 14, 15, 16]]
Print (SUM (Matrix, []))

# pin

[1, 2nd, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 13, 14, 15, 16]

Answer 4

Conversion of a two-dimensional array into one-dimensional:

arr = [[2, 0, 0], [3, 0, 9], [4, 7, 6], [5, 1, 3]]
X = Len (Arr [0])
Y = Len (ARR)
LST = []
For i in Range (Y):
  For j in arr [i]:
    LST.APPEND (J)
lst

Conclusion:

[2, 0, 0, 3, 0, 9, 4, 7, 6, 5, 1, 3 ]

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions