我是node.js和javascript的初学者.
我想在html代码中包含外部javascript文件.这是html代码,"index.html":
<script src="simple.js"></script>
Run Code Online (Sandbox Code Playgroud)
而且,这是javascript代码,"simple.js":
document.write('Hello');
Run Code Online (Sandbox Code Playgroud)
当我直接在网络浏览器(例如谷歌浏览器)上打开"index.html"时,它可以工作.(屏幕上应显示"Hello"消息.)
但是,当我尝试通过node.js http服务器打开"index.html"时,它不起作用.这是node.js文件,"app.js":
var app = require('http').createServer(handler)
, fs = require('fs')
app.listen(8000);
function handler (req, res) {
fs.readFile(__dirname + '/index.html',
function (err, data) {
if (err) {
res.writeHead(500);
return res.end('Error loading index.html');
}
res.writeHead(200);
res.end(data);
});
}
Run Code Online (Sandbox Code Playgroud)
("index.html","simple.js"和"app.js"在同一目录中.)我启动了http服务器.(通过"bash $ node app.js")之后,我尝试连接"localhost:8000".但是,"Hello"消息不会出现.
我认为"index.html"未能在http服务器上包含"simple.js".
我应该怎么做?
输入:
输出:
一个布尔numpy数组,其他值为索引0的值为1.
例:
输入: array_length=10, indexes={2,5,6}
输出:
[0,0,1,0,0,1,1,0,0,0]
Run Code Online (Sandbox Code Playgroud)
这是一个简单的实现:
def indexes2booleanvec(size, indexes):
v = numpy.zeros(size)
for index in indexes:
v[index] = 1.0
return v
Run Code Online (Sandbox Code Playgroud)
是否有更优雅的方式来实现这个?