admin管理员组文章数量:1401158
So I'm trying to convert this Python function into Javascript. I'm new to Python, so some of the syntax is difficult for me to cipher. Here's both the original code and my attempt at JS-conversion. I know I've interpreted something wrong, because what I have now is an infinite loop.
Python:
graph = [[0,1,0,0,1,0],[1,0,1,0,1,0],[0,1,0,1,0,0],[0,0,1,0,1,1],[1,1,0,1,0,0],[0,0,0,1,0,0]]
def N(vertex):
c = 0
l = []
for i in graph[vertex]:
if i is 1 :
l.append(c)
c+=1
return l
def bronk(r,p,x):
if len(p) == 0 and len(x) == 0:
print r
return
for vertex in p[:]:
r_new = r[::]
r_new.append(vertex)
p_new = [val for val in p if val in N(vertex)] #this and
x_new = [val for val in x if val in N(vertex)] #this part was particularly difficult to understand
bronk(r_new,p_new,x_new)
p.remove(vertex)
x.append(vertex)
bronk([], [0,1,2,3,4,5], [])
And here's my attempt at its conversion to JS:
'use strict';
const graph = [[0,1,0,0,1,0],[1,0,1,0,1,0],[0,1,0,1,0,0],[0,0,1,0,1,1],[1,1,0,1,0,0],[0,0,0,1,0,0],];
function N(vertex){
let c = 0;
const l = [];
for (let i in graph[vertex]){
if (i){
l.push(c);
c++;
}
}
return l;
}
function bronk(r,p,x){
if (p.length == 0 && x.length == 0){
console.log(r);
return;
}
for (let vertex in p.slice(0)){
const r_new = r.slice(0);
r_new.push(vertex);
const p_new=p.filter(val=>~~N(vertex).indexOf(val)); //here´s my best guess...
const x_new=x.filter(val=>~~N(vertex).indexOf(val));
bronk(r_new, p_new, x_new);
p=p.splice(vertex,1);
x.push(vertex);
}
}
bronk([], [0,1,2,3,4,5], []);
I got the Python code from this question.
Edit: I'm working in an ES6 environment.
So I'm trying to convert this Python function into Javascript. I'm new to Python, so some of the syntax is difficult for me to cipher. Here's both the original code and my attempt at JS-conversion. I know I've interpreted something wrong, because what I have now is an infinite loop.
Python:
graph = [[0,1,0,0,1,0],[1,0,1,0,1,0],[0,1,0,1,0,0],[0,0,1,0,1,1],[1,1,0,1,0,0],[0,0,0,1,0,0]]
def N(vertex):
c = 0
l = []
for i in graph[vertex]:
if i is 1 :
l.append(c)
c+=1
return l
def bronk(r,p,x):
if len(p) == 0 and len(x) == 0:
print r
return
for vertex in p[:]:
r_new = r[::]
r_new.append(vertex)
p_new = [val for val in p if val in N(vertex)] #this and
x_new = [val for val in x if val in N(vertex)] #this part was particularly difficult to understand
bronk(r_new,p_new,x_new)
p.remove(vertex)
x.append(vertex)
bronk([], [0,1,2,3,4,5], [])
And here's my attempt at its conversion to JS:
'use strict';
const graph = [[0,1,0,0,1,0],[1,0,1,0,1,0],[0,1,0,1,0,0],[0,0,1,0,1,1],[1,1,0,1,0,0],[0,0,0,1,0,0],];
function N(vertex){
let c = 0;
const l = [];
for (let i in graph[vertex]){
if (i){
l.push(c);
c++;
}
}
return l;
}
function bronk(r,p,x){
if (p.length == 0 && x.length == 0){
console.log(r);
return;
}
for (let vertex in p.slice(0)){
const r_new = r.slice(0);
r_new.push(vertex);
const p_new=p.filter(val=>~~N(vertex).indexOf(val)); //here´s my best guess...
const x_new=x.filter(val=>~~N(vertex).indexOf(val));
bronk(r_new, p_new, x_new);
p=p.splice(vertex,1);
x.push(vertex);
}
}
bronk([], [0,1,2,3,4,5], []);
I got the Python code from this question.
Edit: I'm working in an ES6 environment.
Share Improve this question edited May 23, 2017 at 12:13 CommunityBot 11 silver badge asked Dec 10, 2016 at 11:34 OkkuOkku 7,8544 gold badges32 silver badges43 bronze badges2 Answers
Reset to default 4They're both List prehensions in python
The closest you can get to a list prehension in python in Javascript (Without ES6, babel and its relations) is to use Array.Map (Similar to python's map)
Example in python
>>> l = [2, 4, 6, 8, 10, 12]
>>> [int(i / 2) for i in l]
[1, 2, 3, 4, 5, 6]
In Javascript:
l = [2, 4, 6, 8, 10, 12]
l.map(function(i){ return i / 2 });
[1, 2, 3, 4, 5, 6]
With Arrow functions in ES6, you can get rid of the function(){}
l.map(x => x/2)
[2, 4, 6, 8, 10, 12]
So your code should look like this
const p_new = p.map(function(i){ if(i in N(vertex)){ return i } });
const x_new = x.map(function(i){ if(i in N(vertex)){ return i } });
It would be better to use:
p.filter(val => graph[vertex][val])
As this cuts out the useless Array creation that N does.
Also ~~
doesn't properly convert -1
to false
and 0
.. n
to true
. Use !!~
instead.
本文标签: javascriptPython for loop inside brackets syntaxStack Overflow
版权声明:本文标题:javascript - Python for loop inside brackets syntax - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744253757a2597375.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论