开发者

Translating Python into JavaScript — Lists?

开发者 https://www.devze.com 2023-01-07 18:53 出处:网络
octopusList = {\"first\": [\"red\", \"white\"], \"second\": [\"green\", \"blue\", \"red\"], \"third\": [\"green\", \"blue\", \"red\"]}
octopusList = {"first": ["red", "white"],
            "second": ["green", "blue", "red"],
            "third": ["green", "blue", "red"]}
squidList = ["first", "second", "third"]

for i in range(1):
    squid = random.choice(squidList)
    octopus = random.choice开发者_如何学Go(octopusList[squid])

print squid + " " + octopus

Can anyone help me write this in JavaScript? I've got most of my program written into JavaScript but specifically how to get a list with lists in it written in JavaScript has me puzzled. I'm new to programming in general, so thanks for putting up with my questions. :D


First of all, I'd like to say that that for i in range(1): line is useless. It'll only execute the contents once, and you're not using i.

Anyway, the code you posted should work fine with a few tweaks in JavaScript. First you'll need to reimplement random.choice. You could use this:

function randomChoice(list) {
    return list[Math.floor(Math.random()*list.length)];
}

Now after that, it's simple:

var octopusList = {
    "first": ["red", "white"],
    "second": ["green", "blue", "red"],
    "third": ["green", "blue", "red"]
};
var squidList = ["first", "second", "third"];

var squid = randomChoice(squidList);
var octopus = randomChoice(octopusList[squid]);

// You could use alert instead of console.log if you want.
console.log(squid + " " + octopus);


js> octopusList = {"first": ["red", "white"],
               "second": ["green", "blue", "red"],
               "third": ["green", "blue", "red"]}

js> squidList = ["first", "second", "third"]
first,second,third

js> squid = squidList[Math.floor(Math.random() * squidList.length)]
third

js> oct_squid = octopusList[squid]
green,blue,red

js> octopus = oct_squid[Math.floor(Math.random() * oct_squid.length)]
blue


...specifically how to get a list with lists in it written in Javascript has me puzzled.

You can ( also ) create a list in with list in it in JavaScript like this:

var listWithList = [["a,b,c"],["d,"e","f"], ["h","i","j"]]

Because when you code in JavaScript

o = { "first" : ["red","green"],
      "second": ["blue","white"]}

You're actually creating a JavaScript object with two properties first and second whose values are a list ( or array ) with to elements each. This works just fine as you can see in icktoofay answer

Since that's a JavaScript object you could use this syntax to retrieve them

listOne = o.first;
listTwo = o.second;


Consider using the json module to translate data structures from Python to JSON format (which is valid Javascript) -- and viceversa, if you ever need to. For example:

>>> octopusList = {"first": ["red", "white"],
...             "second": ["green", "blue", "red"],
...             "third": ["green", "blue", "red"]}
>>> print json.dumps(octopusList)
{"second": ["green", "blue", "red"], "third": ["green", "blue", "red"], "first": ["red", "white"]}
>>> 

As you see, in this case the "translation" is just about an identity (the change of ordering in the dictionary entries [[in Python]] / object attributes [[in Javascript]] is irrelevant, as neither Python's dicts nor JS's objects have any concept of "ordering";-).

0

精彩评论

暂无评论...
验证码 换一张
取 消