0

I want to convert my 6 line logic into single line. So basically i want to reduce the dimentionality of the vector. numpy.x.reshape is not an option, as i wanted to do it using simple python

for example i have

t = [[[0.304987, 0.284468], [0.928274, 0.966849]], [[0.712916, 0.721612], [0.104858, 0.123942]]]

I want to convert it as

[[0.304987, 0.284468, 0.928274, 0.966849], [0.712916, 0.721612, 0.104858, 0.123942]]

so i did this as

X = []
for i in t:
 ii = []
 for exp in i:
   ii.extend(exp)
 X.append(ii)

I want to make it in one liner.

ggupta
  • 675
  • 1
  • 10
  • 27

1 Answers1

1

Concatenating iterables is called "chaining", and you can do that in a comprehension like this: item for iterable in iterables for item in iterable. So:

>>> [[j for exp in i for j in exp] for i in t]
[[0.304987, 0.284468, 0.928274, 0.966849], [0.712916, 0.721612, 0.104858, 0.123942]]

See also How to make a flat list out of list of lists?

wjandrea
  • 28,235
  • 9
  • 60
  • 81