javascript中的关联数组,使用pair / tuple作为多值键/索引

时间:2014-03-02 03:40:34

标签: javascript dictionary associative-array

在python中,我可以使用元组作为字典的键。什么是javascript中的等价物?

>>> d = {}
>>> d[(1,2)] = 5
>>> d[(2,1)] = 6
>>> d
{(1, 2): 5, (2, 1): 6}

对于那些感兴趣的人,我有两个阵列......

positions = ...

normals = ...

我需要制作第三个位置/正常对数组,但不希望有重复对。我的关联数组会让我检查一下我是否有一个现有的[(posIdx,normalIdx)]对可以重用,或者如果不这样做就创建一对。

我需要一些使用双值键索引的方法。我可以使用一个字符串,但这似乎比两个数字慢一点。

2 个答案:

答案 0 :(得分:5)

Javascript没有元组,但你可以使用数组。

>>> d = {}
>>> d[[1,2]] = 5
>>> d[[2,1]] = 6
>>> d
Object {1,2: 5, 2,1: 6}

答案 1 :(得分:0)

您可以使用地图:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map

  

对象的键是字符串和符号,而它们可以是任意键   Map的值,包括函数,对象和任何基元。

这将避免键只是数组的字符串表示的潜在问题。

File "categorize.py", line 126, in main
  classifier = train(args.train_data, args.vocab)
File "categorize.py", line 39, in train
  classifier.fit(input_fn=lambda: (training_data, training_targets), steps=2000)
File "/usr/local/lib/python3.6/site-packages/tensorflow/python/util/deprecation.py", line 280, in new_func
  return func(*args, **kwargs)
File "/usr/local/lib/python3.6/site-packages/tensorflow/contrib/learn/python/learn/estimators/estimator.py", line 426, in fit
  loss = self._train_model(input_fn=input_fn, hooks=hooks)
File "/usr/local/lib/python3.6/site-packages/tensorflow/contrib/learn/python/learn/estimators/estimator.py", line 934, in _train_model
  model_fn_ops = self._call_legacy_get_train_ops(features, labels)
File "/usr/local/lib/python3.6/site-packages/tensorflow/contrib/learn/python/learn/estimators/estimator.py", line 1003, in _call_legacy_get_train_ops
  train_ops = self._get_train_ops(features, labels)
File "/usr/local/lib/python3.6/site-packages/tensorflow/contrib/learn/python/learn/estimators/estimator.py", line 1162, in _get_train_ops
  return self._call_model_fn(features, labels, model_fn_lib.ModeKeys.TRAIN)
File "/usr/local/lib/python3.6/site-packages/tensorflow/contrib/learn/python/learn/estimators/estimator.py", line 1133, in _call_model_fn
  model_fn_results = self._model_fn(features, labels, **kwargs)
File "categorize.py", line 37, in <lambda>
  classifier = tf.contrib.learn.Estimator(model_fn=lambda features, targets, mode, params: model_fn(features, targets, mode, params, hidden_units))
File "categorize.py", line 73, in model_fn
  learning_rate=0.01)
File "/usr/local/lib/python3.6/site-packages/tensorflow/contrib/layers/python/layers/optimizers.py", line 152, in optimize_loss
  with vs.variable_scope(name, "OptimizeLoss", [loss, global_step]):
File "/usr/local/Cellar/python3/3.6.0_1/Frameworks/Python.framework/Versions/3.6/lib/python3.6/contextlib.py", line 82, in __enter__
  return next(self.gen)
File "/usr/local/lib/python3.6/site-packages/tensorflow/python/ops/variable_scope.py", line 1410, in variable_scope
  g = ops._get_graph_from_inputs(values)  # pylint: disable=protected-access
File "/usr/local/lib/python3.6/site-packages/tensorflow/python/framework/ops.py", line 3968, in _get_graph_from_inputs
  _assert_same_graph(original_graph_element, graph_element)
File "/usr/local/lib/python3.6/site-packages/tensorflow/python/framework/ops.py", line 3907, in _assert_same_graph
  "%s must be from the same graph as %s." % (item, original_item))
ValueError: Tensor("global_step:0", shape=(), dtype=int64_ref) must be from the same graph as Tensor("softmax_cross_entropy_loss/value:0", shape=(), dtype=float32).

请注意,它使用键的变量引用,而不是值。