我有一个这样的数组:
<div>
<div id="main">
<h1>Hello</h1>
<h2>Div 1: I DON'T WANT NAVBAR HERE!!!</h2>
</div>
<div class="container-fluid">
<nav class="navbar navbar-default navbar-fixed-top">
<div class="container-fluid">
<div class="navbar-header">
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1" aria-expanded="false">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
</div>
<div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
<ul class="nav navbar-nav">
<li><a href="#home">HOME <span class="sr-only">(current)</span></a></li>
<li><a href="#about">ABOUT</a></li>
<li><a href="#third">LINK</a></li>
</ul>
</div>
</div>
</nav>
<div id="home">
<h1>DIV 2 NAVBAR MUST APPEAR FROM HERE!!</h1>
</div>
<div id="about">
<div class="row">
<div class="col-lg-offset-1 col-md-10">
<h2 class="title">ABOUT</h2>
<div class="overflow">
<p>Lorem ipsum goes here.</p>
</div>
</div>
</div>
</div>
<div id="third">
<h2 class="title">SECOND</h2>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
</div>
</div>
</div>
我想将上面的数组转换如下:
[ array([[2,3,4,5,6,10]]) array([[7,3,9,1,2,3]]) array([[3,7,34,345,22,1]]) ]
答案 0 :(得分:1)
使用np.vstack
:
import numpy as np
a = [ np.array([[2,3,4,5,6,10]]), np.array([[7,3,9,1,2,3]]), np.array([[3,7,34,345,22,1]]) ]
np.vstack(a)
# array([[ 2, 3, 4, 5, 6, 10],
# [ 7, 3, 9, 1, 2, 3],
# [ 3, 7, 34, 345, 22, 1]])
正如@imaluengo在评论中指出:如果你想拥有一个3d数组,你需要在你的数组中添加另一个空维度:
res = np.vstack(a)
res3d = res[None, ...] # option 1 - ellipsis
res3d = res[None, :, :] # option 2
res3d = np.expand_dims(res, 0) # option 3 - using np.expand_dims
您的输出看起来像一个列表,因此您之后可以使用.tolist()
- 但是您会放弃numpy数组的优势。