Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to unravel array?

I need to generate a list for scipy.optimize.minimize's boundry condition, it should look like this:

bonds = [(0., 0.99),(-30, 30),(-30, 30),(0., 30),(0., 30),(-0.99, 0.99),
        (0., 0.99),(-30, 30),(-30, 30),(0., 30),(0., 30),(-0.99, 0.99),
        (0., 0.99),(-30, 30),(-30, 30),(0., 30),(0., 30),(-0.99, 0.99),]

I'm wondering if there is any elegant way of doing it?

I tried:

bonds = [[(0., 0.99),(-30, 30),(-30, 30),(0., 30),(0., 30),(-0.99, 0.99)] for i in range(3)]

But this generates

[[(0.0, 0.99), (-30, 30), (-30, 30), (0.0, 30), (0.0, 30), (-0.99, 0.99)],
 [(0.0, 0.99), (-30, 30), (-30, 30), (0.0, 30), (0.0, 30), (-0.99, 0.99)],
 [(0.0, 0.99), (-30, 30), (-30, 30), (0.0, 30), (0.0, 30), (-0.99, 0.99)]]

How can I remove the inner [], to unravel the inner arrays into a single one? Or is there any other good way of doing it?

like image 557
cqcn1991 Avatar asked Dec 28 '15 07:12

cqcn1991


2 Answers

you can do:

bonds = [(0., 0.99),(-30, 30),(-30, 30),(0., 30),(0., 30),(-0.99, 0.99)] * 3
like image 177
LinnTroll Avatar answered Sep 30 '22 13:09

LinnTroll


[i for sublist in bonds for i in sublist]

like image 25
vesche Avatar answered Sep 30 '22 13:09

vesche