构建地图不允许索引集合作为地图键? [英] Constructing a map doesn't permit indexed collection as map key?

查看:21
本文介绍了构建地图不允许索引集合作为地图键?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我正在尝试为这个问题写一个答案,这看起来很有趣对我来说.什么鬼,我试试看.

So I'm trying to actually write an answer to this question which seemed like an interesting one to me. What the hell, I'll give it a shot.

这是我想出的解决方案.这对我来说似乎是正确的,应该会产生正确的结果,但会产生密码解析错误.

Here's the solution I came up with. It seems correct to me, and should yield the right result, but instead yields a cypher parse error.

所以这里的游戏是取两个集合(字母和数字),并从中构建一个映射,将正确的字母映射到正确的数字,从两个集合中依次进行.

So the game here is to take two collections (letters and numbers) and to construct a map out of them which maps the right letter to the right number, sequentially from the two collections.

我的解决方案:

with [1,2,3] as nums, ['a', 'b', 'c'] as letters
with nums, letters, range(0, length(nums)-1) as idxs
return extract(idx in idxs | { letters[idx] : nums[idx] });

我的理由是我需要迭代集合索引,以便我可以使用相同的索引同时推进两个集合".我使用 extract 来获取每个索引,然后构建嵌套映射,将适当的项目配对.

My reasoning is that I need to iterate over collection indexes so that I can use the same index to "advance both collections" at the same time. I'm using extract to get each index, then constructing the nested map, which pairs the appropriate items.

除非它失败,因为密码说:

Except that it fails because cypher says:

Invalid input '[': expected an identifier character, whitespace, '}' or ':' (line 3, column 39 (offset: 140))
"return extract(idx in idxs | { letters[idx] : nums[idx] });"
                                       ^

更新:cybersam 在此处回答了原始问题,并指出它可能不是可以动态创建地图键".所以我真正的问题是——是否准确(地图不能有动态键),如果是,为什么?

UPDATE: cybersam answered the original question here and indicated that "it may not be possible to dynamically create map keys". So my real question is -- is that accurate (maps cannot have dynamic keys) and if so, why?

推荐答案

这只是 cypher 的限制".你可以试试这个:

This is just the "limitation" of cypher. You can try this:

WITH 'a' as key, 'test' as value
RETURN {key: value}

但结果是这样的:

{"key":"test"}

Cyber​​sam 的回答中显示了正确的方法.像这样:

The correct approach is shown in the answer of Cybersam. Something like this:

WITH 'a' as key, 'test' as value, 'b' as key2, 'test2' as value2
RETURN [{key: key, value: value}, {key: key2, value: value2}]

将结果:

[{"key":"a","value":"test"},{"key":"b","value":"test2"}]

要创建动态映射键,您必须使用 java 或其他编程语言.

For creating dynamic map keys you have to use java or other programming languages.

这篇关于构建地图不允许索引集合作为地图键?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆