Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/326.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将Python翻译成JavaScript—;名单?_Javascript_Python_Translation - Fatal编程技术网

将Python翻译成JavaScript—;名单?

将Python翻译成JavaScript—;名单?,javascript,python,translation,Javascript,Python,Translation,有人能帮我用JavaScript写这个吗?我的大部分程序都是用JavaScript编写的,但具体地说,如何获得一个列表,其中的列表是用JavaScript编写的,这让我感到困惑。我对编程基本上是新手,所以谢谢你接受我的问题D首先,我想说,对于范围(1)中的I:行是无用的。它只执行一次内容,而您没有使用i 无论如何,您发布的代码应该可以在JavaScript中进行一些调整。首先,您需要重新实现random.choice。你可以用这个: octopusList = {"first": ["red",

有人能帮我用JavaScript写这个吗?我的大部分程序都是用JavaScript编写的,但具体地说,如何获得一个列表,其中的列表是用JavaScript编写的,这让我感到困惑。我对编程基本上是新手,所以谢谢你接受我的问题D

首先,我想说,
对于范围(1)中的I:
行是无用的。它只执行一次内容,而您没有使用
i

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

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
在那之后,事情很简单:

function randomChoice(list) {
    return list[Math.floor(Math.random()*list.length)];
}
…特别是如何获得一个列表,其中的列表是用Javascript编写的,这让我感到困惑

您还可以(也可以)在中创建一个列表,其中包含以下JavaScript格式的列表:

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
因为当你用JavaScript编写代码时

var listWithList = [["a,b,c"],["d,"e","f"], ["h","i","j"]]
实际上,您正在创建一个JavaScript对象,它有两个属性
first
second
,其值是一个列表(或数组),每个属性都有to元素。正如您在icktoofay答案中所看到的那样,这很好

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

o = { "first" : ["red","green"],
      "second": ["blue","white"]}
考虑使用该模块将数据结构从Python转换为JSON格式(这是有效的Javascript)——如果需要,还可以使用viceversa。例如:

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

正如您所看到的,在这种情况下,“翻译”只是一个标识(字典条目[[在Python]]]/对象属性[[在Javascript]]中的顺序变化是不相关的,因为Python的dicts和JS的对象都没有任何“顺序”的概念;-).

值得注意的是,这个答案是面向是否已经翻译成JavaScript的。Python中的
{“a”:“b”,…}
语法指定了一个字典,而不仅仅是一个普通对象。另外,要访问Python中的元素,可以使用
myList[“first”]
myList[“second”]
,等等。最后,如果删除
var
部分::)是的,这是因为这个答案中的所有代码都是javascript:P而不是Python:P我正在更新它
Math.random()
可能返回1,在这种情况下,
randomChoice()
访问的索引不正确。@orip::“返回[0,1]范围内的浮点伪随机数,即从0(包括)到但不包括1(排除),然后可以缩放到所需的范围。”
listOne = o.first;
listTwo = o.second;
>>> 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"]}
>>>