将 Python 翻译成 JavaScript ߞ清单?

发布于 2024-09-09 19:37:22 字数 457 浏览 5 评论 0原文

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(octopusList[squid])

print squid + " " + octopus

谁能帮我用 JavaScript 写这个?我的大部分程序都是用 JavaScript 编写的,但具体如何获取包含用 JavaScript 编写的列表的列表让我感到困惑。一般来说,我对编程很陌生,所以感谢您忍受我的问题。 :D

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(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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

筑梦 2024-09-16 19:37:22

首先,我想说的是 for i in range(1): 行是没有用的。它只会执行一次内容,并且您没有使用 i

无论如何,您发布的代码在 JavaScript 中进行一些调整后应该可以正常工作。首先,您需要重新实现random.choice。你可以使用这个:

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

之后,就很简单了:

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);

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);
公布 2024-09-16 19:37:22
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
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
彼岸花似海 2024-09-16 19:37:22

...特别是如何获取包含用 Javascript 编写的列表的列表让我感到困惑。

您也可以在 JavaScript 中创建一个列表,如下所示:

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

因为当您编写代码时JavaScript

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

您实际上正在创建一个具有两个属性 firstsecond 的 JavaScript 对象,其值是一个列表(或数组),每个属性都有 2 个元素。正如您在 icktoofay 答案中看到的那样,这工作得很好,

因为这是一个 JavaScript 对象,您可以使用此语法来检索它们

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

...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;
紫﹏色ふ单纯 2024-09-16 19:37:22

考虑使用 json 模块来转换数据结构Python 到 JSON 格式(这是有效的 Javascript)——反之亦然,如果您需要的话。例如:

>>> 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"]}
>>> 

如您所见,在这种情况下,“翻译”只是一个身份(字典条目 [[in Python]] / 对象属性 [[in Javascript]] 中顺序的更改是无关紧要的,因为 Python 的字典都不是JS 的对象也没有任何“排序”的概念;-)。

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";-).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文